Permalink
Browse files

Improved implementation of placeholer feature in RCTUITextView

Reviewed By: fkgozali

Differential Revision: D4746177

fbshipit-source-id: a8c27ec052b046d4732b14ed081dcaebb44bdaa7
  • Loading branch information...
shergin authored and facebook-github-bot committed Mar 21, 2017
1 parent 314ec87 commit 1433185a092f70047cc8ef3f90a953a32d756326
Showing with 12 additions and 20 deletions.
  1. +12 −20 Libraries/Text/RCTUITextView.m
@@ -36,7 +36,6 @@ - (instancetype)initWithFrame:(CGRect)frame
object:self];
_placeholderView = [[UILabel alloc] initWithFrame:self.bounds];
_placeholderView.hidden = YES;
_placeholderView.isAccessibilityElement = NO;
_placeholderView.numberOfLines = 0;
[self addSubview:_placeholderView];
@@ -55,20 +54,19 @@ - (void)dealloc
- (void)setPlaceholderText:(NSString *)placeholderText
{
_placeholderText = placeholderText;
[self invalidatePlaceholder];
_placeholderView.text = _placeholderText;
}
- (void)setPlaceholderTextColor:(UIColor *)placeholderTextColor
{
_placeholderTextColor = placeholderTextColor;
[self invalidatePlaceholder];
_placeholderView.textColor = _placeholderTextColor ?: defaultPlaceholderTextColor();
}
- (void)textDidChange
{
_textWasPasted = NO;
[self invalidatePlaceholder];
[self invalidatePlaceholderVisibility];
}
#pragma mark - UIResponder
@@ -101,7 +99,13 @@ - (void)didMoveToWindow
- (void)setFont:(UIFont *)font
{
[super setFont:font];
[self invalidatePlaceholder];
_placeholderView.font = font ?: defaultPlaceholderFont();
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
[super setTextAlignment:textAlignment];
_placeholderView.textAlignment = textAlignment;
}
- (void)setText:(NSString *)text
@@ -168,22 +172,10 @@ - (CGSize)sizeThatFits:(CGSize)size
#pragma mark - Placeholder
- (void)invalidatePlaceholder
- (void)invalidatePlaceholderVisibility
{
BOOL wasVisible = !_placeholderView.isHidden;
BOOL isVisible = _placeholderText.length != 0 && self.text.length == 0;
if (wasVisible != isVisible) {
_placeholderView.hidden = !isVisible;
}
if (isVisible) {
_placeholderView.font = self.font ?: defaultPlaceholderFont();
_placeholderView.textColor = _placeholderTextColor ?: defaultPlaceholderTextColor();
_placeholderView.textAlignment = self.textAlignment;
_placeholderView.text = _placeholderText;
[self setNeedsLayout];
}
_placeholderView.hidden = !isVisible;
}
@end

0 comments on commit 1433185

Please sign in to comment.