Keep placeholder paragraph style same with text input (#23763)

Summary:
Keep placeholder paragraph style same with text input.

[iOS] [Fixed] - Keep placeholder paragraph style same with text input
Pull Request resolved: https://github.com/facebook/react-native/pull/23763

Differential Revision: D14320996

Pulled By: cpojer

fbshipit-source-id: 07c20722809395a74a48300fbb7cadebb8707b03
This commit is contained in:
zhongwuzw
2019-03-04 23:05:56 -08:00
committed by Facebook Github Bot
parent 8491cc36dd
commit debd4462e0
3 changed files with 47 additions and 34 deletions

View File

@@ -66,6 +66,11 @@ extern NSString *const RCTTextAttributesTagAttributeName;
*/
- (NSDictionary<NSAttributedStringKey, id> *)effectiveTextAttributes;
/**
* Constructed paragraph style.
*/
- (NSParagraphStyle *_Nullable)effectiveParagraphStyle;
/**
* Constructed font.
*/

View File

@@ -79,33 +79,8 @@ NSString *const RCTTextAttributesTagAttributeName = @"RCTTextAttributesTagAttrib
_textTransform = textAttributes->_textTransform != RCTTextTransformUndefined ? textAttributes->_textTransform : _textTransform;
}
- (NSDictionary<NSAttributedStringKey, id> *)effectiveTextAttributes
- (NSParagraphStyle *)effectiveParagraphStyle
{
NSMutableDictionary<NSAttributedStringKey, id> *attributes =
[NSMutableDictionary dictionaryWithCapacity:10];
// Font
UIFont *font = self.effectiveFont;
if (font) {
attributes[NSFontAttributeName] = font;
}
// Colors
UIColor *effectiveForegroundColor = self.effectiveForegroundColor;
if (_foregroundColor || !isnan(_opacity)) {
attributes[NSForegroundColorAttributeName] = effectiveForegroundColor;
}
if (_backgroundColor || !isnan(_opacity)) {
attributes[NSBackgroundColorAttributeName] = self.effectiveBackgroundColor;
}
// Kerning
if (!isnan(_letterSpacing)) {
attributes[NSKernAttributeName] = @(_letterSpacing);
}
// Paragraph Style
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
BOOL isParagraphStyleUsed = NO;
@@ -136,6 +111,42 @@ NSString *const RCTTextAttributesTagAttributeName = @"RCTTextAttributesTagAttrib
}
if (isParagraphStyleUsed) {
return [paragraphStyle copy];
}
return nil;
}
- (NSDictionary<NSAttributedStringKey, id> *)effectiveTextAttributes
{
NSMutableDictionary<NSAttributedStringKey, id> *attributes =
[NSMutableDictionary dictionaryWithCapacity:10];
// Font
UIFont *font = self.effectiveFont;
if (font) {
attributes[NSFontAttributeName] = font;
}
// Colors
UIColor *effectiveForegroundColor = self.effectiveForegroundColor;
if (_foregroundColor || !isnan(_opacity)) {
attributes[NSForegroundColorAttributeName] = effectiveForegroundColor;
}
if (_backgroundColor || !isnan(_opacity)) {
attributes[NSBackgroundColorAttributeName] = self.effectiveBackgroundColor;
}
// Kerning
if (!isnan(_letterSpacing)) {
attributes[NSKernAttributeName] = @(_letterSpacing);
}
// Paragraph Style
NSParagraphStyle *paragraphStyle = [self effectiveParagraphStyle];
if (paragraphStyle) {
attributes[NSParagraphStyleAttributeName] = paragraphStyle;
}

View File

@@ -257,11 +257,8 @@ static UIColor *defaultPlaceholderColor()
NSForegroundColorAttributeName: self.placeholderColor ?: defaultPlaceholderColor(),
NSKernAttributeName:isnan(_reactTextAttributes.letterSpacing) ? @0 : @(_reactTextAttributes.letterSpacing)
}];
if (!isnan(_reactTextAttributes.lineHeight)) {
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
CGFloat lineHeight = _reactTextAttributes.lineHeight * _reactTextAttributes.effectiveFontSizeMultiplier;
paragraphStyle.minimumLineHeight = lineHeight;
paragraphStyle.maximumLineHeight = lineHeight;
NSParagraphStyle *paragraphStyle = [_reactTextAttributes effectiveParagraphStyle];
if (paragraphStyle) {
effectiveTextAttributes[NSParagraphStyleAttributeName] = paragraphStyle;
}