2017-03-20 07:00:23 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTUITextView.h"
|
|
|
|
|
2017-05-29 22:56:46 +00:00
|
|
|
#import <React/RCTUtils.h>
|
2017-05-29 22:56:47 +00:00
|
|
|
#import <React/UIView+React.h>
|
2017-04-03 22:11:02 +00:00
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
#import "RCTBackedTextInputDelegateAdapter.h"
|
|
|
|
|
2017-03-20 07:00:23 +00:00
|
|
|
@implementation RCTUITextView
|
|
|
|
{
|
|
|
|
UILabel *_placeholderView;
|
|
|
|
UITextView *_detachedTextView;
|
2017-07-18 21:33:31 +00:00
|
|
|
RCTBackedTextViewDelegateAdapter *_textInputDelegateAdapter;
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static UIFont *defaultPlaceholderFont()
|
|
|
|
{
|
|
|
|
return [UIFont systemFontOfSize:17];
|
|
|
|
}
|
|
|
|
|
2017-06-27 23:05:02 +00:00
|
|
|
static UIColor *defaultPlaceholderColor()
|
2017-03-20 07:00:23 +00:00
|
|
|
{
|
|
|
|
// Default placeholder color from UITextField.
|
|
|
|
return [UIColor colorWithRed:0 green:0 blue:0.0980392 alpha:0.22];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
if (self = [super initWithFrame:frame]) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(textDidChange)
|
|
|
|
name:UITextViewTextDidChangeNotification
|
|
|
|
object:self];
|
|
|
|
|
|
|
|
_placeholderView = [[UILabel alloc] initWithFrame:self.bounds];
|
|
|
|
_placeholderView.isAccessibilityElement = NO;
|
|
|
|
_placeholderView.numberOfLines = 0;
|
2017-06-27 23:05:02 +00:00
|
|
|
_placeholderView.textColor = defaultPlaceholderColor();
|
2017-03-20 07:00:23 +00:00
|
|
|
[self addSubview:_placeholderView];
|
2017-07-18 21:33:31 +00:00
|
|
|
|
|
|
|
_textInputDelegateAdapter = [[RCTBackedTextViewDelegateAdapter alloc] initWithTextView:self];
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:08:27 +00:00
|
|
|
- (NSString *)accessibilityLabel
|
|
|
|
{
|
|
|
|
NSMutableString *accessibilityLabel = [NSMutableString new];
|
|
|
|
|
|
|
|
NSString *superAccessibilityLabel = [super accessibilityLabel];
|
|
|
|
if (superAccessibilityLabel.length > 0) {
|
|
|
|
[accessibilityLabel appendString:superAccessibilityLabel];
|
|
|
|
}
|
|
|
|
|
2017-06-27 23:05:02 +00:00
|
|
|
if (self.placeholder.length > 0 && self.text.length == 0) {
|
2017-06-05 23:08:27 +00:00
|
|
|
if (accessibilityLabel.length > 0) {
|
|
|
|
[accessibilityLabel appendString:@" "];
|
|
|
|
}
|
2017-06-27 23:05:02 +00:00
|
|
|
[accessibilityLabel appendString:self.placeholder];
|
2017-06-05 23:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return accessibilityLabel;
|
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:23 +00:00
|
|
|
#pragma mark - Properties
|
|
|
|
|
2017-06-27 23:05:02 +00:00
|
|
|
- (void)setPlaceholder:(NSString *)placeholder
|
2017-03-20 07:00:23 +00:00
|
|
|
{
|
2017-06-27 23:05:02 +00:00
|
|
|
_placeholder = placeholder;
|
|
|
|
_placeholderView.text = _placeholder;
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 23:05:02 +00:00
|
|
|
- (void)setPlaceholderColor:(UIColor *)placeholderColor
|
2017-03-20 07:00:23 +00:00
|
|
|
{
|
2017-06-27 23:05:02 +00:00
|
|
|
_placeholderColor = placeholderColor;
|
|
|
|
_placeholderView.textColor = _placeholderColor ?: defaultPlaceholderColor();
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)textDidChange
|
|
|
|
{
|
|
|
|
_textWasPasted = NO;
|
2017-03-21 19:53:48 +00:00
|
|
|
[self invalidatePlaceholderVisibility];
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Overrides
|
|
|
|
|
|
|
|
- (void)setFont:(UIFont *)font
|
|
|
|
{
|
|
|
|
[super setFont:font];
|
2017-03-21 19:53:48 +00:00
|
|
|
_placeholderView.font = font ?: defaultPlaceholderFont();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTextAlignment:(NSTextAlignment)textAlignment
|
|
|
|
{
|
|
|
|
[super setTextAlignment:textAlignment];
|
|
|
|
_placeholderView.textAlignment = textAlignment;
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setText:(NSString *)text
|
|
|
|
{
|
|
|
|
[super setText:text];
|
|
|
|
[self textDidChange];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAttributedText:(NSAttributedString *)attributedText
|
|
|
|
{
|
|
|
|
[super setAttributedText:attributedText];
|
|
|
|
[self textDidChange];
|
|
|
|
}
|
|
|
|
|
2017-07-18 21:33:45 +00:00
|
|
|
#pragma mark - Overrides
|
|
|
|
|
|
|
|
- (void)setSelectedTextRange:(UITextRange *)selectedTextRange notifyDelegate:(BOOL)notifyDelegate
|
|
|
|
{
|
|
|
|
if (!notifyDelegate) {
|
|
|
|
// We have to notify an adapter that following selection change was initiated programmatically,
|
|
|
|
// so the adapter must not generate a notification for it.
|
|
|
|
[_textInputDelegateAdapter skipNextTextInputDidChangeSelectionEventWithTextRange:selectedTextRange];
|
|
|
|
}
|
|
|
|
|
|
|
|
[super setSelectedTextRange:selectedTextRange];
|
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:23 +00:00
|
|
|
- (void)paste:(id)sender
|
|
|
|
{
|
|
|
|
[super paste:sender];
|
|
|
|
_textWasPasted = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setContentOffset:(CGPoint)contentOffset animated:(__unused BOOL)animated
|
|
|
|
{
|
|
|
|
// Turning off scroll animation.
|
|
|
|
// This fixes the problem also known as "flaky scrolling".
|
|
|
|
[super setContentOffset:contentOffset animated:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Layout
|
|
|
|
|
2017-06-27 23:05:08 +00:00
|
|
|
- (CGFloat)preferredMaxLayoutWidth
|
|
|
|
{
|
2017-07-18 21:33:58 +00:00
|
|
|
// Returning size DOES contain `textContainerInset` (aka `padding`).
|
2017-06-27 23:05:08 +00:00
|
|
|
return _preferredMaxLayoutWidth ?: self.placeholderSize.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGSize)placeholderSize
|
|
|
|
{
|
|
|
|
UIEdgeInsets textContainerInset = self.textContainerInset;
|
|
|
|
NSString *placeholder = self.placeholder ?: @"";
|
|
|
|
CGSize placeholderSize = [placeholder sizeWithAttributes:@{NSFontAttributeName: self.font ?: defaultPlaceholderFont()}];
|
|
|
|
placeholderSize = CGSizeMake(RCTCeilPixelValue(placeholderSize.width), RCTCeilPixelValue(placeholderSize.height));
|
|
|
|
placeholderSize.width += textContainerInset.left + textContainerInset.right;
|
|
|
|
placeholderSize.height += textContainerInset.top + textContainerInset.bottom;
|
|
|
|
// Returning size DOES contain `textContainerInset` (aka `padding`; as `sizeThatFits:` does).
|
|
|
|
return placeholderSize;
|
|
|
|
}
|
|
|
|
|
2017-07-18 21:33:58 +00:00
|
|
|
- (CGSize)contentSize
|
|
|
|
{
|
|
|
|
CGSize contentSize = super.contentSize;
|
|
|
|
CGSize placeholderSize = self.placeholderSize;
|
|
|
|
// When a text input is empty, it actually displays a placehoder.
|
|
|
|
// So, we have to consider `placeholderSize` as a minimum `contentSize`.
|
|
|
|
// Returning size DOES contain `textContainerInset` (aka `padding`).
|
|
|
|
return CGSizeMake(
|
|
|
|
MAX(contentSize.width, placeholderSize.width),
|
|
|
|
MAX(contentSize.height, placeholderSize.height));
|
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:23 +00:00
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
|
|
|
|
CGRect textFrame = UIEdgeInsetsInsetRect(self.bounds, self.textContainerInset);
|
|
|
|
CGFloat placeholderHeight = [_placeholderView sizeThatFits:textFrame.size].height;
|
|
|
|
textFrame.size.height = MIN(placeholderHeight, textFrame.size.height);
|
|
|
|
_placeholderView.frame = textFrame;
|
|
|
|
}
|
|
|
|
|
2017-06-27 23:05:08 +00:00
|
|
|
- (CGSize)intrinsicContentSize
|
|
|
|
{
|
2017-07-18 21:33:58 +00:00
|
|
|
// Returning size DOES contain `textContainerInset` (aka `padding`).
|
2017-06-27 23:05:08 +00:00
|
|
|
return [self sizeThatFits:CGSizeMake(self.preferredMaxLayoutWidth, INFINITY)];
|
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:23 +00:00
|
|
|
- (CGSize)sizeThatFits:(CGSize)size
|
2017-05-29 22:56:46 +00:00
|
|
|
{
|
|
|
|
// Returned fitting size depends on text size and placeholder size.
|
|
|
|
CGSize textSize = [self fixedSizeThatFits:size];
|
2017-06-27 23:05:08 +00:00
|
|
|
CGSize placeholderSize = self.placeholderSize;
|
2017-07-18 21:33:58 +00:00
|
|
|
// Returning size DOES contain `textContainerInset` (aka `padding`).
|
2017-05-29 22:56:46 +00:00
|
|
|
return CGSizeMake(MAX(textSize.width, placeholderSize.width), MAX(textSize.height, placeholderSize.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGSize)fixedSizeThatFits:(CGSize)size
|
2017-03-20 07:00:23 +00:00
|
|
|
{
|
|
|
|
// UITextView on iOS 8 has a bug that automatically scrolls to the top
|
|
|
|
// when calling `sizeThatFits:`. Use a copy so that self is not screwed up.
|
|
|
|
static BOOL useCustomImplementation = NO;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
useCustomImplementation = ![[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){9,0,0}];
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!useCustomImplementation) {
|
|
|
|
return [super sizeThatFits:size];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_detachedTextView) {
|
|
|
|
_detachedTextView = [UITextView new];
|
|
|
|
}
|
|
|
|
|
|
|
|
_detachedTextView.attributedText = self.attributedText;
|
|
|
|
_detachedTextView.font = self.font;
|
|
|
|
_detachedTextView.textContainerInset = self.textContainerInset;
|
|
|
|
|
|
|
|
return [_detachedTextView sizeThatFits:size];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Placeholder
|
|
|
|
|
2017-03-21 19:53:48 +00:00
|
|
|
- (void)invalidatePlaceholderVisibility
|
2017-03-20 07:00:23 +00:00
|
|
|
{
|
2017-06-27 23:05:02 +00:00
|
|
|
BOOL isVisible = _placeholder.length != 0 && self.text.length == 0;
|
2017-03-21 19:53:48 +00:00
|
|
|
_placeholderView.hidden = !isVisible;
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|