0

The current WeexSDK version 0.18.0,I use show similar effect on the simulator and the real machine and the text can't be fully displayed as shown below.

Also I've added the script to show how to calculate the height of the text component in source code is done. suggestSize.heightis always the height I want, but this code is only executed under iOS10. The value of totalHeight is always worse when I have more text lines than iOS10. How do I solve this?

The effect is as follows:

screen shot

The source code:

- (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);
}
ZF007
  • 3,708
  • 8
  • 29
  • 48
leijmin
  • 1
  • 1
  • Show us what you did to get that. – Rakesha Shastri Dec 24 '18 at 08:39
  • English website my friend... Add a lot of context because now its ready for "unsalvageable" thick-marking. Pushed to "requires editing". I stress out " A lot of"... Follow up on the tour and read how to post questions. – ZF007 Dec 24 '18 at 08:42
  • Are you sure the height is inaccurate, or its getting cut off by the bottom tab bar? – Francis Nepomuceno Dec 27 '18 at 19:18
  • @Neps, Yes, I am pretty sure that he is not getting cut off by the bottom tab bar. I have tried viewdeg with Xcode, and the view level display is fine. Then I tried the breakpoint debugging, which was caused by the judgment in the method of calculating the text text height in the WeexSDK source code. After that I tried to upgrade my WeexSDK version to 0.20.1. Fortunately, I found that this problem does not exist. – leijmin Dec 28 '18 at 06:21
  • Sorry, I didn't provide a lot of valid information, thank you for your reminder. @ZF007 After debugging, suggestSize.height is always the height I want, but I just executed this code below iOS10. The value of totalHeight is always worse when I have more text lines than iOS10. – leijmin Dec 28 '18 at 06:36

0 Answers0