2017-05-29 22:56:47 +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 "RCTUITextField.h"
|
|
|
|
|
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
#import <React/UIView+React.h>
|
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
#import "RCTBackedTextInputDelegateAdapter.h"
|
|
|
|
|
|
|
|
@implementation RCTUITextField {
|
|
|
|
RCTBackedTextFieldDelegateAdapter *_textInputDelegateAdapter;
|
|
|
|
}
|
2017-05-29 22:56:47 +00:00
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
if (self = [super initWithFrame:frame]) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(_textDidChange)
|
|
|
|
name:UITextFieldTextDidChangeNotification
|
|
|
|
object:self];
|
2017-07-18 21:33:31 +00:00
|
|
|
|
|
|
|
_textInputDelegateAdapter = [[RCTBackedTextFieldDelegateAdapter alloc] initWithTextField:self];
|
2017-05-29 22:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_textDidChange
|
|
|
|
{
|
|
|
|
_textWasPasted = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Properties
|
|
|
|
|
|
|
|
- (void)setTextContainerInset:(UIEdgeInsets)textContainerInset
|
|
|
|
{
|
|
|
|
_textContainerInset = textContainerInset;
|
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setPlaceholder:(NSString *)placeholder
|
|
|
|
{
|
|
|
|
[super setPlaceholder:placeholder];
|
|
|
|
[self _updatePlaceholder];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setPlaceholderColor:(UIColor *)placeholderColor
|
|
|
|
{
|
|
|
|
_placeholderColor = placeholderColor;
|
|
|
|
[self _updatePlaceholder];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_updatePlaceholder
|
|
|
|
{
|
|
|
|
if (self.placeholder == nil) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSMutableDictionary *attributes = [NSMutableDictionary new];
|
|
|
|
if (_placeholderColor) {
|
|
|
|
[attributes setObject:_placeholderColor forKey:NSForegroundColorAttributeName];
|
|
|
|
}
|
|
|
|
|
|
|
|
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder
|
|
|
|
attributes:attributes];
|
|
|
|
}
|
|
|
|
|
2017-06-27 23:04:59 +00:00
|
|
|
- (BOOL)isEditable
|
|
|
|
{
|
2017-07-04 04:11:48 +00:00
|
|
|
return self.isEnabled;
|
2017-06-27 23:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setEditable:(BOOL)editable
|
|
|
|
{
|
2017-07-04 04:11:48 +00:00
|
|
|
self.enabled = editable;
|
2017-06-27 23:04:59 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 22:56:47 +00:00
|
|
|
#pragma mark - Caret Manipulation
|
|
|
|
|
|
|
|
- (CGRect)caretRectForPosition:(UITextPosition *)position
|
|
|
|
{
|
|
|
|
if (_caretHidden) {
|
|
|
|
return CGRectZero;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [super caretRectForPosition:position];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Positioning Overrides
|
|
|
|
|
|
|
|
- (CGRect)textRectForBounds:(CGRect)bounds
|
|
|
|
{
|
|
|
|
return UIEdgeInsetsInsetRect([super textRectForBounds:bounds], _textContainerInset);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGRect)editingRectForBounds:(CGRect)bounds
|
|
|
|
{
|
|
|
|
return [self textRectForBounds:bounds];
|
|
|
|
}
|
|
|
|
|
2017-06-23 21:02:35 +00:00
|
|
|
#pragma mark - Overrides
|
|
|
|
|
2017-07-18 21:33:45 +00:00
|
|
|
- (void)setSelectedTextRange:(UITextRange *)selectedTextRange
|
|
|
|
{
|
|
|
|
[super setSelectedTextRange:selectedTextRange];
|
|
|
|
[_textInputDelegateAdapter selectedTextRangeWasSet];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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-06-23 21:02:35 +00:00
|
|
|
- (void)paste:(id)sender
|
|
|
|
{
|
|
|
|
[super paste:sender];
|
|
|
|
_textWasPasted = YES;
|
|
|
|
}
|
|
|
|
|
2017-05-29 22:56:47 +00:00
|
|
|
#pragma mark - Layout
|
|
|
|
|
2017-07-18 21:33:58 +00:00
|
|
|
- (CGSize)contentSize
|
|
|
|
{
|
|
|
|
// Returning size DOES contain `textContainerInset` (aka `padding`).
|
|
|
|
return self.intrinsicContentSize;
|
|
|
|
}
|
|
|
|
|
2017-05-29 22:56:47 +00:00
|
|
|
- (CGSize)intrinsicContentSize
|
|
|
|
{
|
|
|
|
// Note: `placeholder` defines intrinsic size for `<TextInput>`.
|
|
|
|
NSString *text = self.placeholder ?: @"";
|
|
|
|
CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: self.font}];
|
|
|
|
size = CGSizeMake(RCTCeilPixelValue(size.width), RCTCeilPixelValue(size.height));
|
|
|
|
size.width += _textContainerInset.left + _textContainerInset.right;
|
|
|
|
size.height += _textContainerInset.top + _textContainerInset.bottom;
|
2017-07-18 21:33:58 +00:00
|
|
|
// Returning size DOES contain `textContainerInset` (aka `padding`).
|
2017-05-29 22:56:47 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGSize)sizeThatFits:(CGSize)size
|
|
|
|
{
|
2017-07-18 21:33:58 +00:00
|
|
|
// All size values here contain `textContainerInset` (aka `padding`).
|
2017-05-29 22:56:47 +00:00
|
|
|
CGSize intrinsicSize = self.intrinsicContentSize;
|
|
|
|
return CGSizeMake(MIN(size.width, intrinsicSize.width), MIN(size.height, intrinsicSize.height));
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|