Высота компонента weex <text>не точна? - PullRequest
0 голосов
/ 24 декабря 2018

Текущая версия WeexSDK 0.18.0 use Я использую показ аналогичного эффекта на симуляторе и реальной машине, и текст не может быть полностью отображен, как показано ниже.

Также я добавил скрипт впокажите, как рассчитать высоту текстового компонента в исходном коде.suggestSize.height - это всегда высота, которую я хочу, но этот код выполняется только под iOS10.Значение totalHeight всегда хуже, когда у меня больше строк текста, чем в iOS10.Как мне решить эту проблему?

Эффект выглядит следующим образом:

screen shot

Исходный код:

- (CGSize)calculateTextHeightWithWidth:(CGFloat)aWidth
{

   CGFloat totalHeight = 0;
CGSize suggestSize = CGSizeZero;
NSAttributedString * attributedStringCpy = [self ctAttributedString];
if (!attributedStringCpy) {
    return CGSizeZero;
}
if (isnan(aWidth)) {
    aWidth = CGFLOAT_MAX;
}
aWidth = [attributedStringCpy boundingRectWithSize:CGSizeMake(aWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size.width;

/* Must get ceil of aWidth. Or core text may not return correct bounds.
 Maybe aWidth without ceiling triggered some critical conditions. */
aWidth = ceil(aWidth);
CTFramesetterRef ctframesetterRef = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attributedStringCpy));
suggestSize = CTFramesetterSuggestFrameSizeWithConstraints(ctframesetterRef, CFRangeMake(0, 0), NULL, CGSizeMake(aWidth, MAXFLOAT), NULL);

if (_lines == 0) {
    // If not line limit use suggestSize directly.
    CFRelease(ctframesetterRef);
    return CGSizeMake(aWidth, suggestSize.height);
}

CGMutablePathRef path = NULL;
path = CGPathCreateMutable();
    // sufficient height to draw text
CGPathAddRect(path, NULL, CGRectMake(0, 0, aWidth, suggestSize.height * 10));

CTFrameRef frameRef = NULL;
frameRef = CTFramesetterCreateFrame(ctframesetterRef, CFRangeMake(0, attributedStringCpy.length), path, NULL);
CGPathRelease(path);
CFRelease(ctframesetterRef);

if (NULL == frameRef) {
    //try to protect unexpected crash.
    return suggestSize;
}

CFArrayRef lines = CTFrameGetLines(frameRef);
CFIndex lineCount = CFArrayGetCount(lines);
CGFloat ascent = 0;
CGFloat descent = 0;
CGFloat leading = 0;

// height = ascent + descent + lineCount*leading
// ignore linespaing
NSUInteger actualLineCount = 0;
for (CFIndex lineIndex = 0; (!_lines|| lineIndex < _lines) && lineIndex < lineCount; lineIndex ++)
{
    CTLineRef lineRef = NULL;
    lineRef = (CTLineRef)CFArrayGetValueAtIndex(lines, lineIndex);
    CTLineGetTypographicBounds(lineRef, &ascent, &descent, &leading);
    totalHeight += ascent + descent;
    actualLineCount ++;
}

totalHeight = totalHeight + actualLineCount * leading;
CFRelease(frameRef);

if (WX_SYS_VERSION_LESS_THAN(@"10.0")) {
    // there is something wrong with coreText drawing text height, trying to fix this with more efficent way.
    if(actualLineCount && actualLineCount < lineCount) {
        suggestSize.height = suggestSize.height * actualLineCount / lineCount;
    }
    return CGSizeMake(aWidth, suggestSize.height);
}
return CGSizeMake(aWidth, totalHeight);
}
...