2015-04-29 08:29:00 +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 "RCTTextView.h"
|
|
|
|
|
|
|
|
#import "RCTConvert.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
#import "UIView+React.h"
|
|
|
|
|
|
|
|
@implementation RCTTextView
|
|
|
|
{
|
|
|
|
RCTEventDispatcher *_eventDispatcher;
|
|
|
|
BOOL _jsRequestingFirstResponder;
|
|
|
|
NSString *_placeholder;
|
|
|
|
UITextView *_placeholderView;
|
|
|
|
UITextView *_textView;
|
2015-07-21 19:37:24 +00:00
|
|
|
NSInteger _nativeEventCount;
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
|
|
|
|
{
|
2015-06-15 14:53:45 +00:00
|
|
|
RCTAssertParam(eventDispatcher);
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
if ((self = [super initWithFrame:CGRectZero])) {
|
|
|
|
_contentInset = UIEdgeInsetsZero;
|
|
|
|
_eventDispatcher = eventDispatcher;
|
|
|
|
_placeholderTextColor = [self defaultPlaceholderTextColor];
|
|
|
|
|
|
|
|
_textView = [[UITextView alloc] initWithFrame:self.bounds];
|
|
|
|
_textView.backgroundColor = [UIColor clearColor];
|
2015-09-01 10:23:29 +00:00
|
|
|
_textView.scrollsToTop = NO;
|
2015-04-29 08:29:00 +00:00
|
|
|
_textView.delegate = self;
|
|
|
|
[self addSubview:_textView];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
- (void)updateFrames
|
|
|
|
{
|
|
|
|
// Adjust the insets so that they are as close as possible to single-line
|
2015-08-12 22:24:59 +00:00
|
|
|
// RCTTextField defaults, using the system defaults of font size 17 and a
|
|
|
|
// height of 31 points.
|
|
|
|
//
|
|
|
|
// We apply the left inset to the frame since a negative left text-container
|
|
|
|
// inset mysteriously causes the text to be hidden until the text view is
|
|
|
|
// first focused.
|
|
|
|
UIEdgeInsets adjustedFrameInset = UIEdgeInsetsZero;
|
|
|
|
adjustedFrameInset.left = _contentInset.left - 5;
|
|
|
|
|
|
|
|
UIEdgeInsets adjustedTextContainerInset = _contentInset;
|
|
|
|
adjustedTextContainerInset.top += 5;
|
|
|
|
adjustedTextContainerInset.left = 0;
|
|
|
|
|
|
|
|
CGRect frame = UIEdgeInsetsInsetRect(self.bounds, adjustedFrameInset);
|
|
|
|
_textView.frame = frame;
|
|
|
|
_placeholderView.frame = frame;
|
|
|
|
|
|
|
|
_textView.textContainerInset = adjustedTextContainerInset;
|
|
|
|
_placeholderView.textContainerInset = adjustedTextContainerInset;
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updatePlaceholder
|
|
|
|
{
|
|
|
|
[_placeholderView removeFromSuperview];
|
|
|
|
_placeholderView = nil;
|
|
|
|
|
|
|
|
if (_placeholder) {
|
|
|
|
_placeholderView = [[UITextView alloc] initWithFrame:self.bounds];
|
|
|
|
_placeholderView.backgroundColor = [UIColor clearColor];
|
|
|
|
_placeholderView.scrollEnabled = false;
|
2015-10-02 17:14:41 +00:00
|
|
|
_placeholderView.scrollsToTop = NO;
|
2015-04-29 08:29:00 +00:00
|
|
|
_placeholderView.attributedText =
|
|
|
|
[[NSAttributedString alloc] initWithString:_placeholder attributes:@{
|
|
|
|
NSFontAttributeName : (_textView.font ? _textView.font : [self defaultPlaceholderFont]),
|
|
|
|
NSForegroundColorAttributeName : _placeholderTextColor
|
|
|
|
}];
|
|
|
|
|
|
|
|
[self insertSubview:_placeholderView belowSubview:_textView];
|
|
|
|
[self _setPlaceholderVisibility];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:37:39 +00:00
|
|
|
- (UIFont *)font
|
|
|
|
{
|
|
|
|
return _textView.font;
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
- (void)setFont:(UIFont *)font
|
|
|
|
{
|
2015-05-14 16:37:39 +00:00
|
|
|
_textView.font = font;
|
2015-04-29 08:29:00 +00:00
|
|
|
[self updatePlaceholder];
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:37:39 +00:00
|
|
|
- (UIColor *)textColor
|
|
|
|
{
|
|
|
|
return _textView.textColor;
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
- (void)setTextColor:(UIColor *)textColor
|
|
|
|
{
|
|
|
|
_textView.textColor = textColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setPlaceholder:(NSString *)placeholder
|
|
|
|
{
|
|
|
|
_placeholder = placeholder;
|
|
|
|
[self updatePlaceholder];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setPlaceholderTextColor:(UIColor *)placeholderTextColor
|
|
|
|
{
|
|
|
|
if (placeholderTextColor) {
|
|
|
|
_placeholderTextColor = placeholderTextColor;
|
|
|
|
} else {
|
|
|
|
_placeholderTextColor = [self defaultPlaceholderTextColor];
|
|
|
|
}
|
|
|
|
[self updatePlaceholder];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setContentInset:(UIEdgeInsets)contentInset
|
|
|
|
{
|
|
|
|
_contentInset = contentInset;
|
|
|
|
[self updateFrames];
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:37:39 +00:00
|
|
|
- (NSString *)text
|
|
|
|
{
|
|
|
|
return _textView.text;
|
|
|
|
}
|
|
|
|
|
2015-07-21 19:37:24 +00:00
|
|
|
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
|
|
|
|
{
|
|
|
|
if (_maxLength == nil) {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
NSUInteger allowedLength = _maxLength.integerValue - textView.text.length + range.length;
|
|
|
|
if (text.length > allowedLength) {
|
|
|
|
if (text.length > 1) {
|
|
|
|
// Truncate the input string so the result is exactly maxLength
|
|
|
|
NSString *limitedString = [text substringToIndex:allowedLength];
|
|
|
|
NSMutableString *newString = textView.text.mutableCopy;
|
|
|
|
[newString replaceCharactersInRange:range withString:limitedString];
|
|
|
|
textView.text = newString;
|
|
|
|
// Collapse selection at end of insert to match normal paste behavior
|
|
|
|
UITextPosition *insertEnd = [textView positionFromPosition:textView.beginningOfDocument
|
|
|
|
offset:(range.location + allowedLength)];
|
|
|
|
textView.selectedTextRange = [textView textRangeFromPosition:insertEnd toPosition:insertEnd];
|
|
|
|
[self textViewDidChange:textView];
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
} else {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
- (void)setText:(NSString *)text
|
|
|
|
{
|
2015-07-21 19:37:24 +00:00
|
|
|
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
|
|
|
|
if (eventLag == 0 && ![text isEqualToString:_textView.text]) {
|
|
|
|
UITextRange *selection = _textView.selectedTextRange;
|
2015-08-24 10:14:33 +00:00
|
|
|
_textView.text = text;
|
2015-04-29 08:29:00 +00:00
|
|
|
[self _setPlaceholderVisibility];
|
2015-07-21 19:37:24 +00:00
|
|
|
_textView.selectedTextRange = selection; // maintain cursor position/selection - this is robust to out of bounds
|
|
|
|
} else if (eventLag > RCTTextUpdateLagWarningThreshold) {
|
2015-08-11 13:37:12 +00:00
|
|
|
RCTLogWarn(@"Native TextInput(%@) is %zd events ahead of JS - try to make your JS faster.", self.text, eventLag);
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_setPlaceholderVisibility
|
|
|
|
{
|
|
|
|
if (_textView.text.length > 0) {
|
|
|
|
[_placeholderView setHidden:YES];
|
|
|
|
} else {
|
|
|
|
[_placeholderView setHidden:NO];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAutoCorrect:(BOOL)autoCorrect
|
|
|
|
{
|
|
|
|
_textView.autocorrectionType = (autoCorrect ? UITextAutocorrectionTypeYes : UITextAutocorrectionTypeNo);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)autoCorrect
|
|
|
|
{
|
|
|
|
return _textView.autocorrectionType == UITextAutocorrectionTypeYes;
|
|
|
|
}
|
|
|
|
|
2015-04-29 22:55:36 +00:00
|
|
|
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
|
|
|
|
{
|
|
|
|
if (_selectTextOnFocus) {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[textView selectAll:nil];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
- (void)textViewDidBeginEditing:(UITextView *)textView
|
|
|
|
{
|
2015-04-29 22:55:36 +00:00
|
|
|
if (_clearTextOnFocus) {
|
|
|
|
_textView.text = @"";
|
|
|
|
[self _setPlaceholderVisibility];
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeFocus
|
|
|
|
reactTag:self.reactTag
|
2015-07-21 19:37:24 +00:00
|
|
|
text:textView.text
|
|
|
|
eventCount:_nativeEventCount];
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)textViewDidChange:(UITextView *)textView
|
|
|
|
{
|
|
|
|
[self _setPlaceholderVisibility];
|
2015-07-21 19:37:24 +00:00
|
|
|
_nativeEventCount++;
|
2015-04-29 08:29:00 +00:00
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeChange
|
|
|
|
reactTag:self.reactTag
|
2015-07-21 19:37:24 +00:00
|
|
|
text:textView.text
|
|
|
|
eventCount:_nativeEventCount];
|
2015-04-29 08:29:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)textViewDidEndEditing:(UITextView *)textView
|
|
|
|
{
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeEnd
|
|
|
|
reactTag:self.reactTag
|
2015-07-21 19:37:24 +00:00
|
|
|
text:textView.text
|
|
|
|
eventCount:_nativeEventCount];
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)becomeFirstResponder
|
|
|
|
{
|
|
|
|
_jsRequestingFirstResponder = YES;
|
2015-04-29 22:55:36 +00:00
|
|
|
BOOL result = [_textView becomeFirstResponder];
|
2015-04-29 08:29:00 +00:00
|
|
|
_jsRequestingFirstResponder = NO;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)resignFirstResponder
|
|
|
|
{
|
2015-04-29 22:55:36 +00:00
|
|
|
[super resignFirstResponder];
|
|
|
|
BOOL result = [_textView resignFirstResponder];
|
2015-04-29 08:29:00 +00:00
|
|
|
if (result) {
|
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeBlur
|
|
|
|
reactTag:self.reactTag
|
2015-07-21 19:37:24 +00:00
|
|
|
text:_textView.text
|
|
|
|
eventCount:_nativeEventCount];
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
[self updateFrames];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)canBecomeFirstResponder
|
|
|
|
{
|
|
|
|
return _jsRequestingFirstResponder;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIFont *)defaultPlaceholderFont
|
|
|
|
{
|
2015-07-09 23:45:48 +00:00
|
|
|
return [UIFont systemFontOfSize:17];
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UIColor *)defaultPlaceholderTextColor
|
|
|
|
{
|
|
|
|
return [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.098/255.0 alpha:0.22];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|