Skip to content

Commit

Permalink
[iOS] Keep placeholder attributes sync with text input text's attributes
Browse files Browse the repository at this point in the history
[iOS] Keep placeholder attributes sync with text input text's attributes

Add nil check for placeholder
  • Loading branch information
zhongwuzw committed Mar 4, 2019
1 parent d451c03 commit 37445f2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Libraries/Text/TextInput/Multiline/RCTUITextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ - (NSString *)accessibilityLabel
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = placeholder;
_placeholderView.text = _placeholder;
_placeholderView.attributedText = [[NSAttributedString alloc] initWithString:_placeholder ?: @"" attributes:[self placeholderEffectiveTextAttributes]];
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
Expand All @@ -98,7 +98,8 @@ - (void)setReactTextAttributes:(RCTTextAttributes *)reactTextAttributes
}
self.typingAttributes = reactTextAttributes.effectiveTextAttributes;
_reactTextAttributes = reactTextAttributes;
_placeholderView.font = self.font ?: defaultPlaceholderFont();
// Update placeholder text attributes
[self setPlaceholder:_placeholder];
}

- (RCTTextAttributes *)reactTextAttributes
Expand Down Expand Up @@ -249,6 +250,17 @@ - (void)invalidatePlaceholderVisibility
_placeholderView.hidden = !isVisible;
}

- (NSDictionary<NSAttributedStringKey, id> *)placeholderEffectiveTextAttributes
{
NSDictionary<NSAttributedStringKey, id> *effectiveTextAttributes = @{
NSFontAttributeName: _reactTextAttributes.effectiveFont ?: defaultPlaceholderFont(),
NSForegroundColorAttributeName: self.placeholderColor ?: defaultPlaceholderColor(),
NSKernAttributeName:isnan(_reactTextAttributes.letterSpacing) ? @0 : @(_reactTextAttributes.letterSpacing)
};

return effectiveTextAttributes;
}

#pragma mark - Utility Methods

- (void)copyTextAttributesFrom:(NSAttributedString *)sourceString
Expand Down
5 changes: 5 additions & 0 deletions Libraries/Text/TextInput/Singleline/RCTUITextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ - (void)setReactTextAttributes:(RCTTextAttributes *)reactTextAttributes
}
self.defaultTextAttributes = reactTextAttributes.effectiveTextAttributes;
_reactTextAttributes = reactTextAttributes;
[self _updatePlaceholder];
}

- (RCTTextAttributes *)reactTextAttributes
Expand All @@ -87,6 +88,10 @@ - (void)_updatePlaceholder
if (_placeholderColor) {
[attributes setObject:_placeholderColor forKey:NSForegroundColorAttributeName];
}
// Kerning
if (!isnan(_reactTextAttributes.letterSpacing)) {
attributes[NSKernAttributeName] = @(_reactTextAttributes.letterSpacing);
}

self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder
attributes:attributes];
Expand Down

0 comments on commit 37445f2

Please sign in to comment.