wqyfavor closed pull request #1289: [WEEX-446,447][iOS] Fix core text layout bug. URL: https://github.com/apache/incubator-weex/pull/1289
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.mm b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.mm index 13f470a253..714b2edd45 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.mm +++ b/ios/sdk/WeexSDK/Sources/Component/WXTextComponent.mm @@ -971,9 +971,19 @@ - (CGSize)calculateTextHeightWithWidth:(CGFloat)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 @@ -982,15 +992,14 @@ - (CGSize)calculateTextHeightWithWidth:(CGFloat)aWidth CTFrameRef frameRef = NULL; frameRef = CTFramesetterCreateFrame(ctframesetterRef, CFRangeMake(0, attributedStringCpy.length), path, NULL); CGPathRelease(path); + CFRelease(ctframesetterRef); - CFArrayRef lines = NULL; if (NULL == frameRef) { //try to protect unexpected crash. return suggestSize; } - CFRelease(ctframesetterRef); - ctframesetterRef = NULL; - lines = CTFrameGetLines(frameRef); + + CFArrayRef lines = CTFrameGetLines(frameRef); CFIndex lineCount = CFArrayGetCount(lines); CGFloat ascent = 0; CGFloat descent = 0; @@ -1010,7 +1019,6 @@ - (CGSize)calculateTextHeightWithWidth:(CGFloat)aWidth totalHeight = totalHeight + actualLineCount * leading; CFRelease(frameRef); - frameRef = NULL; 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. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services