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

*[ios] fix text height when drawing text #305

Merged
merged 3 commits into from
May 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions ios/sdk/WeexSDK/Sources/Component/WXTextComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,8 @@ - (instancetype)initWithRef:(NSString *)ref

- (BOOL)useCoreText
{
if (WX_SYS_VERSION_LESS_THAN(@"10.0")) {
// there is something wrong with coreText drawing lineHeight, trying to fix this, or anyone who can help me to fix this.
return NO;
}

if ([_useCoreTextAttr isEqualToString:@"yes"]) {
if ([_useCoreTextAttr isEqualToString:@"true"]) {
return YES;
}
if ([_useCoreTextAttr isEqualToString:@"false"]) {
Expand Down Expand Up @@ -372,6 +368,7 @@ - (NSMutableAttributedString *)buildCTAttributeString {
NULL);
if (ctFont) {
[attributedString addAttribute:(id)kCTFontAttributeName value:(__bridge id)(ctFont) range:NSMakeRange(0, string.length)];
CFRelease(ctFont);
}

if(_textDecoration == WXTextDecorationUnderline){
Expand Down Expand Up @@ -416,8 +413,6 @@ - (NSMutableAttributedString *)buildCTAttributeString {
}
}

CFRelease(ctFont);

return attributedString;
}

Expand Down Expand Up @@ -485,6 +480,9 @@ - (NSAttributedString *)buildAttributeString

- (BOOL)adjustLineHeight
{
if (WX_SYS_VERSION_LESS_THAN(@"10.0")) {
return true;
}
return ![self useCoreText];
}

Expand Down Expand Up @@ -627,7 +625,6 @@ - (void)drawTextWithContext:(CGContextRef)context bounds:(CGRect)bounds padding:
CGPoint lineOrigin = lineOrigins[lineIndex];
lineOrigin.x += padding.left;
lineOrigin.y -= padding.top;
CGContextSetTextPosition(context, lineOrigin.x, lineOrigin.y);
CFArrayRef runs = CTLineGetGlyphRuns(lineRef);
[mutableLines addObject:(__bridge id _Nonnull)(lineRef)];
// lineIndex base 0
Expand All @@ -652,6 +649,7 @@ - (void)drawTextWithContext:(CGContextRef)context bounds:(CGRect)bounds padding:
}

if (needTruncation) {
CGContextSetTextPosition(context, lineOrigin.x, lineOrigin.y);
ctTruncatedLine = [self buildTruncatedLineWithRuns:runs lines:mutableLines path:cgPath];
if (ctTruncatedLine) {
CFArrayRef truncatedRuns = CTLineGetGlyphRuns(ctTruncatedLine);
Expand Down Expand Up @@ -679,9 +677,14 @@ - (void)drawTextWithRuns:(CFArrayRef)runs context:(CGContextRef)context lineOrig
for (CFIndex runIndex = 0; runIndex < CFArrayGetCount(runs); runIndex ++) {
CTRunRef run = NULL;
run = CFArrayGetValueAtIndex(runs, runIndex);
CTRunDraw(run, context, CFRangeMake(0, 0));
CFDictionaryRef attr = NULL;
attr = CTRunGetAttributes(run);
NSNumber *baselineOffset = (NSNumber*)CFDictionaryGetValue(attr, NSBaselineOffsetAttributeName);
if (baselineOffset) {
lineOrigin.y += [baselineOffset doubleValue];
}
CGContextSetTextPosition(context, lineOrigin.x, lineOrigin.y);
CTRunDraw(run, context, CFRangeMake(0, 0));
CFIndex glyphCount = CTRunGetGlyphCount(run);
if (glyphCount <= 0) continue;

Expand Down Expand Up @@ -830,6 +833,14 @@ - (CGSize)calculateTextHeightWithWidth:(CGFloat)aWidth
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 suggestSize;
}

return CGSizeMake(suggestSize.width, totalHeight);
}

Expand Down