mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-29 00:41:07 +08:00
Add letterSpacing style property for Text
Summary: Fixes #457 Closes https://github.com/facebook/react-native/pull/482 Github Author: Vladimir Kurchatkin <vladimir.kurchatkin@gmail.com> Test Plan: Imported from GitHub, without a `Test Plan:` line.
This commit is contained in:
committed by
Christopher Chedeau
parent
02d875869a
commit
a142ed50ff
@@ -40,11 +40,15 @@ static css_dim_t RCTMeasure(void *context, float width)
|
||||
|
||||
css_dim_t result;
|
||||
result.dimensions[CSS_WIDTH] = RCTCeilPixelValue(computedSize.width);
|
||||
if (shadowText.effectiveLetterSpacing < 0) {
|
||||
result.dimensions[CSS_WIDTH] -= shadowText.effectiveLetterSpacing;
|
||||
}
|
||||
result.dimensions[CSS_HEIGHT] = RCTCeilPixelValue(computedSize.height);
|
||||
return result;
|
||||
}
|
||||
|
||||
@implementation RCTShadowText {
|
||||
@implementation RCTShadowText
|
||||
{
|
||||
NSLayoutManager *_layoutManager;
|
||||
NSTextContainer *_textContainer;
|
||||
NSAttributedString *_cachedAttributedString;
|
||||
@@ -55,6 +59,7 @@ static css_dim_t RCTMeasure(void *context, float width)
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
_fontSize = NAN;
|
||||
_letterSpacing = NAN;
|
||||
_isHighlighted = NO;
|
||||
|
||||
_textContainer = [[NSTextContainer alloc] init];
|
||||
@@ -71,22 +76,24 @@ static css_dim_t RCTMeasure(void *context, float width)
|
||||
- (NSAttributedString *)attributedString
|
||||
{
|
||||
return [self _attributedStringWithFontFamily:nil
|
||||
fontSize:0
|
||||
fontSize:nil
|
||||
fontWeight:nil
|
||||
fontStyle:nil];
|
||||
fontStyle:nil
|
||||
letterSpacing:nil];
|
||||
}
|
||||
|
||||
- (NSAttributedString *)_attributedStringWithFontFamily:(NSString *)fontFamily
|
||||
fontSize:(CGFloat)fontSize
|
||||
fontSize:(NSNumber *)fontSize
|
||||
fontWeight:(NSString *)fontWeight
|
||||
fontStyle:(NSString *)fontStyle
|
||||
letterSpacing:(NSNumber *)letterSpacing
|
||||
{
|
||||
if (![self isTextDirty] && _cachedAttributedString) {
|
||||
return _cachedAttributedString;
|
||||
}
|
||||
|
||||
if (_fontSize && !isnan(_fontSize)) {
|
||||
fontSize = _fontSize;
|
||||
fontSize = @(_fontSize);
|
||||
}
|
||||
if (_fontWeight) {
|
||||
fontWeight = _fontWeight;
|
||||
@@ -97,12 +104,17 @@ static css_dim_t RCTMeasure(void *context, float width)
|
||||
if (_fontFamily) {
|
||||
fontFamily = _fontFamily;
|
||||
}
|
||||
if (!isnan(_letterSpacing)) {
|
||||
letterSpacing = @(_letterSpacing);
|
||||
}
|
||||
|
||||
_effectiveLetterSpacing = letterSpacing.doubleValue;
|
||||
|
||||
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
|
||||
for (RCTShadowView *child in [self reactSubviews]) {
|
||||
if ([child isKindOfClass:[RCTShadowText class]]) {
|
||||
RCTShadowText *shadowText = (RCTShadowText *)child;
|
||||
[attributedString appendAttributedString:[shadowText _attributedStringWithFontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight fontStyle:fontStyle]];
|
||||
[attributedString appendAttributedString:[shadowText _attributedStringWithFontFamily:fontFamily fontSize:fontSize fontWeight:fontWeight fontStyle:fontStyle letterSpacing:letterSpacing]];
|
||||
} else if ([child isKindOfClass:[RCTShadowRawText class]]) {
|
||||
RCTShadowRawText *shadowRawText = (RCTShadowRawText *)child;
|
||||
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:[shadowRawText text] ?: @""]];
|
||||
@@ -123,8 +135,9 @@ static css_dim_t RCTMeasure(void *context, float width)
|
||||
[self _addAttribute:NSBackgroundColorAttributeName withValue:self.textBackgroundColor toAttributedString:attributedString];
|
||||
}
|
||||
|
||||
_font = [RCTConvert UIFont:nil withFamily:fontFamily size:@(fontSize) weight:fontWeight style:fontStyle];
|
||||
_font = [RCTConvert UIFont:nil withFamily:fontFamily size:fontSize weight:fontWeight style:fontStyle];
|
||||
[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];
|
||||
|
||||
@@ -143,7 +156,7 @@ static css_dim_t RCTMeasure(void *context, float width)
|
||||
- (void)_addAttribute:(NSString *)attribute withValue:(id)attributeValue toAttributedString:(NSMutableAttributedString *)attributedString
|
||||
{
|
||||
[attributedString enumerateAttribute:attribute inRange:NSMakeRange(0, [attributedString length]) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
|
||||
if (!value) {
|
||||
if (!value && attributeValue) {
|
||||
[attributedString addAttribute:attribute value:attributeValue range:range];
|
||||
}
|
||||
}];
|
||||
@@ -223,6 +236,7 @@ RCT_TEXT_PROPERTY(Color, _color, UIColor *);
|
||||
RCT_TEXT_PROPERTY(FontFamily, _fontFamily, NSString *);
|
||||
RCT_TEXT_PROPERTY(FontSize, _fontSize, CGFloat);
|
||||
RCT_TEXT_PROPERTY(FontWeight, _fontWeight, NSString *);
|
||||
RCT_TEXT_PROPERTY(LetterSpacing, _letterSpacing, CGFloat);
|
||||
RCT_TEXT_PROPERTY(LineHeight, _lineHeight, CGFloat);
|
||||
RCT_TEXT_PROPERTY(ShadowOffset, _shadowOffset, CGSize);
|
||||
RCT_TEXT_PROPERTY(TextAlign, _textAlign, NSTextAlignment);
|
||||
|
||||
Reference in New Issue
Block a user