Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit d481ce5

Browse files
Yue-Wang-GoogleAdlai Holler
authored andcommitted
[ASTextNode] Fix ascender to include line height specified in attributed string paragraph style (#1997)
* Fix ASTextNode's ascender to also include the line height specified by paragraph style in the attributed string. * Merge conflict (original patch is for an old version)
1 parent 93a5c36 commit d481ce5

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

AsyncDisplayKit/ASTextNode.mm

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,8 @@ - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
351351

352352
CGSize size = [self _renderer].size;
353353
if (_attributedText.length > 0) {
354-
CGFloat screenScale = ASScreenScale();
355-
self.ascender = round([[_attributedText attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL] ascender] * screenScale)/screenScale;
356-
self.descender = round([[_attributedText attribute:NSFontAttributeName atIndex:_attributedText.length - 1 effectiveRange:NULL] descender] * screenScale)/screenScale;
354+
self.ascender = [[self class] ascenderWithAttributedString:_attributedText];
355+
self.descender = [[_attributedText attribute:NSFontAttributeName atIndex:_attributedText.length - 1 effectiveRange:NULL] descender];
357356
if (_renderer.currentScaleFactor > 0 && _renderer.currentScaleFactor < 1.0) {
358357
// while not perfect, this is a good estimate of what the ascender of the scaled font will be.
359358
self.ascender *= _renderer.currentScaleFactor;
@@ -365,6 +364,21 @@ - (CGSize)calculateSizeThatFits:(CGSize)constrainedSize
365364

366365
#pragma mark - Modifying User Text
367366

367+
// Returns the ascender of the first character in attributedString by also including the line height if specified in paragraph style.
368+
+ (CGFloat)ascenderWithAttributedString:(NSAttributedString *)attributedString
369+
{
370+
UIFont *font = [attributedString attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
371+
NSParagraphStyle *paragraphStyle = [attributedString attribute:NSParagraphStyleAttributeName atIndex:0 effectiveRange:NULL];
372+
if (!paragraphStyle) {
373+
return font.ascender;
374+
}
375+
CGFloat lineHeight = MAX(font.lineHeight, paragraphStyle.minimumLineHeight);
376+
if (paragraphStyle.maximumLineHeight > 0) {
377+
lineHeight = MIN(lineHeight, paragraphStyle.maximumLineHeight);
378+
}
379+
return lineHeight + font.descender;
380+
}
381+
368382
- (void)setAttributedText:(NSAttributedString *)attributedText
369383
{
370384

@@ -392,9 +406,8 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
392406

393407
NSUInteger length = attributedText.length;
394408
if (length > 0) {
395-
CGFloat screenScale = ASScreenScale();
396-
self.ascender = round([[attributedText attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL] ascender] * screenScale)/screenScale;
397-
self.descender = round([[attributedText attribute:NSFontAttributeName atIndex:length - 1 effectiveRange:NULL] descender] * screenScale)/screenScale;
409+
self.ascender = [[self class] ascenderWithAttributedString:attributedText];
410+
self.descender = [[attributedText attribute:NSFontAttributeName atIndex:attributedText.length - 1 effectiveRange:NULL] descender];
398411
}
399412

400413
// Tell the display node superclasses that the cached layout is incorrect now

0 commit comments

Comments
 (0)