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

Commit aa77c9f

Browse files
神漠cxfeng1
神漠
authored andcommitted
[WEEX-446,447][iOS] Fix core text layout bug.
1 parent 709d780 commit aa77c9f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

ios/sdk/WeexSDK/Sources/Component/WXTextComponent.mm

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -971,9 +971,19 @@ - (CGSize)calculateTextHeightWithWidth:(CGFloat)aWidth
971971
aWidth = CGFLOAT_MAX;
972972
}
973973
aWidth = [attributedStringCpy boundingRectWithSize:CGSizeMake(aWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil].size.width;
974+
975+
/* Must get ceil of aWidth. Or core text may not return correct bounds.
976+
Maybe aWidth without ceiling triggered some critical conditions. */
977+
aWidth = ceil(aWidth);
974978
CTFramesetterRef ctframesetterRef = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)(attributedStringCpy));
975979
suggestSize = CTFramesetterSuggestFrameSizeWithConstraints(ctframesetterRef, CFRangeMake(0, 0), NULL, CGSizeMake(aWidth, MAXFLOAT), NULL);
976980

981+
if (_lines == 0) {
982+
// If not line limit use suggestSize directly.
983+
CFRelease(ctframesetterRef);
984+
return CGSizeMake(aWidth, suggestSize.height);
985+
}
986+
977987
CGMutablePathRef path = NULL;
978988
path = CGPathCreateMutable();
979989
// sufficient height to draw text
@@ -982,15 +992,14 @@ - (CGSize)calculateTextHeightWithWidth:(CGFloat)aWidth
982992
CTFrameRef frameRef = NULL;
983993
frameRef = CTFramesetterCreateFrame(ctframesetterRef, CFRangeMake(0, attributedStringCpy.length), path, NULL);
984994
CGPathRelease(path);
995+
CFRelease(ctframesetterRef);
985996

986-
CFArrayRef lines = NULL;
987997
if (NULL == frameRef) {
988998
//try to protect unexpected crash.
989999
return suggestSize;
9901000
}
991-
CFRelease(ctframesetterRef);
992-
ctframesetterRef = NULL;
993-
lines = CTFrameGetLines(frameRef);
1001+
1002+
CFArrayRef lines = CTFrameGetLines(frameRef);
9941003
CFIndex lineCount = CFArrayGetCount(lines);
9951004
CGFloat ascent = 0;
9961005
CGFloat descent = 0;
@@ -1010,7 +1019,6 @@ - (CGSize)calculateTextHeightWithWidth:(CGFloat)aWidth
10101019

10111020
totalHeight = totalHeight + actualLineCount * leading;
10121021
CFRelease(frameRef);
1013-
frameRef = NULL;
10141022

10151023
if (WX_SYS_VERSION_LESS_THAN(@"10.0")) {
10161024
// there is something wrong with coreText drawing text height, trying to fix this with more efficent way.

0 commit comments

Comments
 (0)