[go: nahoru, domu]

Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[WEEX-446,447][iOS] Fix core text layout bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
神漠 authored and cxfeng1 committed Jun 27, 2018
1 parent 709d780 commit aa77c9f
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions ios/sdk/WeexSDK/Sources/Component/WXTextComponent.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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.
Expand Down

0 comments on commit aa77c9f

Please sign in to comment.