Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): fix nested text layout error #929

Merged
merged 3 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions ios/sdk/component/text/HippyShadowText.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern NSString *const HippyShadowViewAttributeName;
NSAttributedString *_cachedAttributedString;
CGFloat _effectiveLetterSpacing;
BOOL _textAlignSet;
CGFloat _maximumFontLineHeight;
}

@property (nonatomic, strong) UIColor *color;
Expand Down
27 changes: 20 additions & 7 deletions ios/sdk/component/text/HippyShadowText.mm
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,19 @@ - (void)applyLayoutToChildren:(__unused MTTNodeRef)node
if (isnan(width) || isnan(height)) {
HippyLogError(@"Views nested within a <Text> must have a width and height");
}
UIFont *font = [textStorage attribute:NSFontAttributeName atIndex:range.location effectiveRange:nil];

/**
* For RichText, a view, which is top aligment by default, should be center alignment to text,
*/

CGRect glyphRect = [layoutManager boundingRectForGlyphRange:range inTextContainer:textContainer];
CGRect usedOriginRect = [layoutManager lineFragmentRectForGlyphAtIndex:range.location effectiveRange:nil];
CGFloat lineHeight = usedOriginRect.size.height;
CGFloat Roundedheight = x5RoundPixelValue(height);
CGFloat originY = usedOriginRect.origin.y + (lineHeight - Roundedheight) / 2;
CGRect childFrame = { { x5RoundPixelValue(glyphRect.origin.x),
x5RoundPixelValue(glyphRect.origin.y + glyphRect.size.height - height + font.descender) },
{ x5RoundPixelValue(width), x5RoundPixelValue(height) } };

x5RoundPixelValue(originY) },
{ x5RoundPixelValue(width), Roundedheight } };
NSRange truncatedGlyphRange = [layoutManager truncatedGlyphRangeInLineFragmentForGlyphAtIndex:range.location];
BOOL childIsTruncated = NSIntersectionRange(range, truncatedGlyphRange).length != 0;

Expand Down Expand Up @@ -460,7 +467,9 @@ - (void)_setParagraphStyleOnAttributedString:(NSMutableAttributedString *)attrib
fontLineHeight:(CGFloat)fontLineHeight
heightOfTallestSubview:(CGFloat)heightOfTallestSubview {
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];

if (fabs(_lineHeight - 0) < DBL_EPSILON) {
_lineHeight = fontLineHeight;
}
// check if we have lineHeight set on self
__block BOOL hasParagraphStyle = NO;
if (_lineHeight || _textAlignSet) {
Expand Down Expand Up @@ -515,12 +524,16 @@ - (void)_setParagraphStyleOnAttributedString:(NSMutableAttributedString *)attrib
paragraphStyle.maximumLineHeight = maxHeight;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:(NSRange) { 0, attributedString.length }];

/**
* for keeping text ertical center, we need to set baseline offset
*/
if (lineHeight > fontLineHeight) {
[attributedString addAttribute:NSBaselineOffsetAttributeName value:@(newLineHeight / 2 - maximumFontLineHeight / 2)
CGFloat baselineOffset = newLineHeight / 2 - maximumFontLineHeight / 2;
[attributedString addAttribute:NSBaselineOffsetAttributeName value:@(baselineOffset)
range:(NSRange) { 0, attributedString.length }];
}
}

_maximumFontLineHeight = maximumFontLineHeight;
// Text decoration
if (_textDecorationLine == HippyTextDecorationLineTypeUnderline || _textDecorationLine == HippyTextDecorationLineTypeUnderlineStrikethrough) {
[self _addAttribute:NSUnderlineStyleAttributeName withValue:@(_textDecorationStyle) toAttributedString:attributedString];
Expand Down