[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-496][iOS] In CoreText mode, origin of first line is incorret un…
Browse files Browse the repository at this point in the history
…der iOS9 or lower.
  • Loading branch information
神漠 committed Jul 8, 2018
1 parent 003765f commit 402feab
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions ios/sdk/WeexSDK/Sources/Component/WXTextComponent.mm
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ @implementation WXTextComponent
NSString *_textOverflow;
CGFloat _lineHeight;
CGFloat _letterSpacing;
CGFloat _fontDescender;
CGFloat _fontAscender;
BOOL _truncationLine; // support trunk tail

NSAttributedString * _ctAttributedString;
Expand Down Expand Up @@ -289,7 +291,6 @@ - (void)fillCSSStyles:(NSDictionary *)styles
_padding = flex_padding;
[self setNeedsRepaint];
}

}

- (void)fillAttributes:(NSDictionary *)attributes
Expand All @@ -312,7 +313,6 @@ - (void)setNeedsRepaint
pthread_mutex_lock(&(_ctAttributedStringMutex));
_ctAttributedString = nil;
pthread_mutex_unlock(&(_ctAttributedStringMutex));

}

#pragma mark - Subclass
Expand Down Expand Up @@ -484,6 +484,10 @@ - (NSMutableAttributedString *)buildCTAttributeString
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName,
font.pointSize,
NULL);

_fontAscender = font.ascender;
_fontDescender = font.descender;

if (ctFont) {
[attributedString addAttribute:(id)kCTFontAttributeName value:(__bridge id)(ctFont) range:NSMakeRange(0, string.length)];
CFRelease(ctFont);
Expand Down Expand Up @@ -776,6 +780,15 @@ - (void)drawTextWithContext:(CGContextRef)context bounds:(CGRect)bounds padding:
BOOL needTruncation = NO;
CTLineRef ctTruncatedLine = NULL;
CTFrameGetLineOrigins(_coreTextFrameRef, CFRangeMake(0, 0), lineOrigins);

CGFloat fixDescent = 0;
if (lineCount > 0 && _lineHeight && WX_SYS_VERSION_LESS_THAN(@"10.0")) {
CGFloat ascent, descent, leading;
CTLineRef line1 = (CTLineRef)CFArrayGetValueAtIndex(ctLines, 0);
CTLineGetTypographicBounds(line1, &ascent, &descent, &leading);
fixDescent = (descent + _fontDescender) + (ascent - _fontAscender);
}

for (CFIndex lineIndex = 0;(!_lines || _lines > lineIndex) && lineIndex < lineCount; lineIndex ++) {
CTLineRef lineRef = NULL;
lineRef = (CTLineRef)CFArrayGetValueAtIndex(ctLines, lineIndex);
Expand All @@ -784,7 +797,7 @@ - (void)drawTextWithContext:(CGContextRef)context bounds:(CGRect)bounds padding:
}
CGPoint lineOrigin = lineOrigins[lineIndex];
lineOrigin.x += padding.left;
lineOrigin.y -= padding.top;
lineOrigin.y -= padding.top + fixDescent;
CFArrayRef runs = CTLineGetGlyphRuns(lineRef);
[mutableLines addObject:(__bridge id _Nonnull)(lineRef)];
// lineIndex base 0
Expand Down

0 comments on commit 402feab

Please sign in to comment.