Skip to content

Commit

Permalink
Centre text in case where the distance between baselines is larger th…
Browse files Browse the repository at this point in the history
Summary:
…an the font size (per CSS spec)

Extending upon work #7603
Closes #8851

![screen shot 2016-08-04 at 14 49 21](https://cloud.githubusercontent.com/assets/7275322/17404165/bd67bc48-5a52-11e6-9ba1-5a8524f18867.png)
![screen shot 2016-08-04 at 14 49 24](https://cloud.githubusercontent.com/assets/7275322/17404167/be5f4044-5a52-11e6-9014-391349f9c5e1.png)
Closes #9211

Differential Revision: D3706347

fbshipit-source-id: 0adfff8e8418b02f9b5d6671f5c89669e41abec3
  • Loading branch information
jacobp100 authored and Facebook Github Bot 8 committed Aug 11, 2016
1 parent c642136 commit 68d483e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion Libraries/Text/RCTShadowText.m
Expand Up @@ -339,7 +339,9 @@ - (NSAttributedString *)_attributedStringWithFontFamily:(NSString *)fontFamily
[self _addAttribute:NSFontAttributeName withValue:font toAttributedString:attributedString];
[self _addAttribute:NSKernAttributeName withValue:letterSpacing toAttributedString:attributedString];
[self _addAttribute:RCTReactTagAttributeName withValue:self.reactTag toAttributedString:attributedString];
[self _setParagraphStyleOnAttributedString:attributedString heightOfTallestSubview:heightOfTallestSubview];
[self _setParagraphStyleOnAttributedString:attributedString
fontLineHeight:font.lineHeight
heightOfTallestSubview:heightOfTallestSubview];

// create a non-mutable attributedString for use by the Text system which avoids copies down the line
_cachedAttributedString = [[NSAttributedString alloc] initWithAttributedString:attributedString];
Expand All @@ -362,6 +364,7 @@ - (void)_addAttribute:(NSString *)attribute withValue:(id)attributeValue toAttri
* varying lineHeights, we simply take the max.
*/
- (void)_setParagraphStyleOnAttributedString:(NSMutableAttributedString *)attributedString
fontLineHeight:(CGFloat)fontLineHeight
heightOfTallestSubview:(CGFloat)heightOfTallestSubview
{
// check if we have lineHeight set on self
Expand Down Expand Up @@ -431,6 +434,12 @@ - (void)_setParagraphStyleOnAttributedString:(NSMutableAttributedString *)attrib
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:(NSRange){0, attributedString.length}];

if (lineHeight > fontLineHeight) {
[attributedString addAttribute:NSBaselineOffsetAttributeName
value:@(lineHeight / 2 - fontLineHeight / 2)
range:(NSRange){0, attributedString.length}];
}
}

// Text decoration
Expand Down

0 comments on commit 68d483e

Please sign in to comment.