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"
|
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTEventDispatcher.h>
|
iOS: Support allowFontScaling on TextInput
Summary:
Currently, only `Text` supports the `allowFontScaling` prop. This commit adds support for it on `TextInput`.
As part of this change, the TextInput setters for font attributes (e.g. size, weight) had to be refactored. The problem with them is that they use RCTFont's helpers which create a new font based on an existing font. These helpers lose information. In particular, they lose the scaleMultiplier.
For example, suppose the font size is 12 and the device's font multiplier is set to 1.5. So we'd create a font with size 12 and scaleMultiplier 1.5 which is an effective size of 18 (which is the only thing stored in the font). Next, suppose the device's font multiplier changes to 1. So we'd use an RCTFont helper to create a new font based on the existing font but with a scaleMultiplier of 1. However, the font didn't store the font size (12) and scaleMultiplier (1.5) separately. It just knows the (effective) font size of 18. So RCTFont thinks the new font has a font size of 18 and a scaleMultiplier of 1 so its effective font size is 18. This is incorrect and it should have been 12.
To fix this, the font attributes are now all stored individually. Anytime one of them changes, updateFont is called which recreates the font from scratch. This happens to fix some bugs around fontStyle and fontWeight which were reported several times before: #13730, #12738, #2140, #8533.
Created a test app where I verified that `allowFontScaling` works properly for `TextInputs` for all values (`undefined`, `true`, `false`) for a variety of `TextInputs`:
- Singleline TextInput
- Singleline TextInput's placeholder
- Multiline TextInput
- Multiline TextInput's placeholder
- Multiline TextInput using children instead of `value`
Also, verified switching `fontSize`, `fontWeight`, `fontStyle` and `fontFamily` through a bunch of combinations works properly.
Lastly, my team has been using this change in our app.
Adam Comella
Microsoft Corp.
Closes https://github.com/facebook/react-native/pull/14030
Reviewed By: TheSavior
Differential Revision: D5899959
Pulled By: shergin
fbshipit-source-id: c8c8c4d4d670cd2a142286e79bfffef3b58cecd3
2017-10-02 04:40:57 +00:00
|
|
|
#import <React/RCTFont.h>
|
2017-03-20 07:00:18 +00:00
|
|
|
#import <React/RCTUIManager.h>
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
#import <React/UIView+React.h>
|
|
|
|
|
2016-04-21 19:09:16 +00:00
|
|
|
#import "RCTShadowText.h"
|
2015-11-06 15:25:19 +00:00
|
|
|
#import "RCTText.h"
|
2016-08-26 00:18:05 +00:00
|
|
|
#import "RCTTextSelection.h"
|
2017-03-20 07:00:23 +00:00
|
|
|
#import "RCTUITextView.h"
|
2015-11-02 17:13:41 +00:00
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
@interface RCTTextView () <RCTBackedTextInputDelegate>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
@implementation RCTTextView
|
|
|
|
{
|
2017-07-18 21:33:31 +00:00
|
|
|
RCTUITextView *_backedTextInput;
|
2015-11-06 15:25:19 +00:00
|
|
|
RCTText *_richTextView;
|
|
|
|
NSAttributedString *_pendingAttributedText;
|
2016-06-24 13:28:38 +00:00
|
|
|
|
|
|
|
NSString *_predictedText;
|
|
|
|
|
|
|
|
BOOL _blockTextShouldChange;
|
|
|
|
BOOL _nativeUpdatesInFlight;
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:18 +00:00
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
2015-04-29 08:29:00 +00:00
|
|
|
{
|
2017-03-20 07:00:18 +00:00
|
|
|
RCTAssertParam(bridge);
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2017-06-27 23:05:05 +00:00
|
|
|
if (self = [super initWithBridge:bridge]) {
|
2017-07-18 21:33:35 +00:00
|
|
|
// `blurOnSubmit` defaults to `false` for <TextInput multiline={true}> by design.
|
2015-12-02 15:11:20 +00:00
|
|
|
_blurOnSubmit = NO;
|
2015-04-29 08:29:00 +00:00
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput = [[RCTUITextView alloc] initWithFrame:self.bounds];
|
|
|
|
_backedTextInput.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
|
_backedTextInput.backgroundColor = [UIColor clearColor];
|
|
|
|
_backedTextInput.textColor = [UIColor blackColor];
|
2017-03-20 07:00:18 +00:00
|
|
|
// This line actually removes 5pt (default value) left and right padding in UITextView.
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput.textContainer.lineFragmentPadding = 0;
|
2016-09-27 13:19:45 +00:00
|
|
|
#if !TARGET_OS_TV
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput.scrollsToTop = NO;
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput.scrollEnabled = YES;
|
|
|
|
_backedTextInput.textInputDelegate = self;
|
iOS: Support allowFontScaling on TextInput
Summary:
Currently, only `Text` supports the `allowFontScaling` prop. This commit adds support for it on `TextInput`.
As part of this change, the TextInput setters for font attributes (e.g. size, weight) had to be refactored. The problem with them is that they use RCTFont's helpers which create a new font based on an existing font. These helpers lose information. In particular, they lose the scaleMultiplier.
For example, suppose the font size is 12 and the device's font multiplier is set to 1.5. So we'd create a font with size 12 and scaleMultiplier 1.5 which is an effective size of 18 (which is the only thing stored in the font). Next, suppose the device's font multiplier changes to 1. So we'd use an RCTFont helper to create a new font based on the existing font but with a scaleMultiplier of 1. However, the font didn't store the font size (12) and scaleMultiplier (1.5) separately. It just knows the (effective) font size of 18. So RCTFont thinks the new font has a font size of 18 and a scaleMultiplier of 1 so its effective font size is 18. This is incorrect and it should have been 12.
To fix this, the font attributes are now all stored individually. Anytime one of them changes, updateFont is called which recreates the font from scratch. This happens to fix some bugs around fontStyle and fontWeight which were reported several times before: #13730, #12738, #2140, #8533.
Created a test app where I verified that `allowFontScaling` works properly for `TextInputs` for all values (`undefined`, `true`, `false`) for a variety of `TextInputs`:
- Singleline TextInput
- Singleline TextInput's placeholder
- Multiline TextInput
- Multiline TextInput's placeholder
- Multiline TextInput using children instead of `value`
Also, verified switching `fontSize`, `fontWeight`, `fontStyle` and `fontFamily` through a bunch of combinations works properly.
Lastly, my team has been using this change in our app.
Adam Comella
Microsoft Corp.
Closes https://github.com/facebook/react-native/pull/14030
Reviewed By: TheSavior
Differential Revision: D5899959
Pulled By: shergin
fbshipit-source-id: c8c8c4d4d670cd2a142286e79bfffef3b58cecd3
2017-10-02 04:40:57 +00:00
|
|
|
_backedTextInput.font = self.fontAttributes.font;
|
2017-07-18 21:33:31 +00:00
|
|
|
|
|
|
|
[self addSubview:_backedTextInput];
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2017-06-27 23:05:04 +00:00
|
|
|
- (id<RCTBackedTextInputViewProtocol>)backedTextInputView
|
|
|
|
{
|
2017-07-18 21:33:31 +00:00
|
|
|
return _backedTextInput;
|
2017-06-27 23:05:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:17 +00:00
|
|
|
#pragma mark - RCTComponent
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)index
|
2015-11-06 15:25:19 +00:00
|
|
|
{
|
2016-06-07 07:08:16 +00:00
|
|
|
[super insertReactSubview:subview atIndex:index];
|
2017-03-20 07:00:17 +00:00
|
|
|
|
2015-11-06 15:25:19 +00:00
|
|
|
if ([subview isKindOfClass:[RCTText class]]) {
|
|
|
|
if (_richTextView) {
|
|
|
|
RCTLogError(@"Tried to insert a second <Text> into <TextInput> - there can only be one.");
|
|
|
|
}
|
|
|
|
_richTextView = (RCTText *)subview;
|
2016-04-21 19:09:16 +00:00
|
|
|
|
|
|
|
// If this <TextInput> is in rich text editing mode, and the child <Text> node providing rich text
|
|
|
|
// styling has a backgroundColor, then the attributedText produced by the child <Text> node will have an
|
|
|
|
// NSBackgroundColor attribute. We need to forward this attribute to the text view manually because the text view
|
2017-03-20 07:00:18 +00:00
|
|
|
// always has a clear background color in `initWithBridge:`.
|
2016-04-21 19:09:16 +00:00
|
|
|
//
|
|
|
|
// TODO: This should be removed when the related hack in -performPendingTextUpdate is removed.
|
|
|
|
if (subview.backgroundColor) {
|
2017-07-18 21:33:31 +00:00
|
|
|
NSMutableDictionary<NSString *, id> *attrs = [_backedTextInput.typingAttributes mutableCopy];
|
2016-04-21 19:09:16 +00:00
|
|
|
attrs[NSBackgroundColorAttributeName] = subview.backgroundColor;
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput.typingAttributes = attrs;
|
2016-04-21 19:09:16 +00:00
|
|
|
}
|
2016-06-23 10:53:50 +00:00
|
|
|
|
|
|
|
[self performTextUpdate];
|
2015-11-06 15:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-14 18:25:00 +00:00
|
|
|
- (void)removeReactSubview:(UIView *)subview
|
2015-11-06 15:25:19 +00:00
|
|
|
{
|
2016-06-07 07:08:16 +00:00
|
|
|
[super removeReactSubview:subview];
|
2015-11-06 15:25:19 +00:00
|
|
|
if (_richTextView == subview) {
|
|
|
|
_richTextView = nil;
|
2016-06-23 10:53:50 +00:00
|
|
|
[self performTextUpdate];
|
2015-11-06 15:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 15:36:07 +00:00
|
|
|
- (void)didUpdateReactSubviews
|
2016-06-07 07:08:16 +00:00
|
|
|
{
|
2017-03-20 07:00:17 +00:00
|
|
|
// Do nothing, as we don't allow non-text subviews.
|
2016-06-07 07:08:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:17 +00:00
|
|
|
#pragma mark - Routine
|
|
|
|
|
2015-11-06 15:25:19 +00:00
|
|
|
- (void)setMostRecentEventCount:(NSInteger)mostRecentEventCount
|
|
|
|
{
|
|
|
|
_mostRecentEventCount = mostRecentEventCount;
|
|
|
|
|
|
|
|
// Props are set after uiBlockToAmendWithShadowViewRegistry, which means that
|
|
|
|
// at the time performTextUpdate is called, _mostRecentEventCount will be
|
|
|
|
// behind _eventCount, with the result that performPendingTextUpdate will do
|
|
|
|
// nothing. For that reason we call it again here after mostRecentEventCount
|
|
|
|
// has been set.
|
|
|
|
[self performPendingTextUpdate];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)performTextUpdate
|
|
|
|
{
|
|
|
|
if (_richTextView) {
|
|
|
|
_pendingAttributedText = _richTextView.textStorage;
|
|
|
|
[self performPendingTextUpdate];
|
|
|
|
} else if (!self.text) {
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput.attributedText = nil;
|
2015-11-06 15:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:09:16 +00:00
|
|
|
static NSAttributedString *removeReactTagFromString(NSAttributedString *string)
|
|
|
|
{
|
|
|
|
if (string.length == 0) {
|
|
|
|
return string;
|
|
|
|
} else {
|
|
|
|
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithAttributedString:string];
|
|
|
|
[mutableString removeAttribute:RCTReactTagAttributeName range:NSMakeRange(0, mutableString.length)];
|
|
|
|
return mutableString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 15:25:19 +00:00
|
|
|
- (void)performPendingTextUpdate
|
|
|
|
{
|
2016-06-24 13:28:38 +00:00
|
|
|
if (!_pendingAttributedText || _mostRecentEventCount < _nativeEventCount || _nativeUpdatesInFlight) {
|
2015-11-06 15:25:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:09:16 +00:00
|
|
|
// The underlying <Text> node that produces _pendingAttributedText has a react tag attribute on it that causes the
|
|
|
|
// -isEqualToAttributedString: comparison below to spuriously fail. We don't want that comparison to fail unless it
|
|
|
|
// needs to because when the comparison fails, we end up setting attributedText on the text view, which clears
|
|
|
|
// autocomplete state for CKJ text input.
|
|
|
|
//
|
|
|
|
// TODO: Kill this after we finish passing all style/attribute info into JS.
|
|
|
|
_pendingAttributedText = removeReactTagFromString(_pendingAttributedText);
|
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
if ([_backedTextInput.attributedText isEqualToAttributedString:_pendingAttributedText]) {
|
2015-11-06 15:25:19 +00:00
|
|
|
_pendingAttributedText = nil; // Don't try again.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When we update the attributed text, there might be pending autocorrections
|
|
|
|
// that will get accepted by default. In order for this to not garble our text,
|
|
|
|
// we temporarily block all textShouldChange events so they are not applied.
|
|
|
|
_blockTextShouldChange = YES;
|
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
UITextRange *selection = _backedTextInput.selectedTextRange;
|
|
|
|
NSInteger oldTextLength = _backedTextInput.attributedText.length;
|
2015-12-17 18:22:56 +00:00
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput.attributedText = _pendingAttributedText;
|
2016-06-24 13:28:38 +00:00
|
|
|
_predictedText = _pendingAttributedText.string;
|
2015-11-06 15:25:19 +00:00
|
|
|
_pendingAttributedText = nil;
|
2015-12-17 18:22:56 +00:00
|
|
|
|
|
|
|
if (selection.empty) {
|
|
|
|
// maintain cursor position relative to the end of the old text
|
2017-07-18 21:33:31 +00:00
|
|
|
NSInteger start = [_backedTextInput offsetFromPosition:_backedTextInput.beginningOfDocument toPosition:selection.start];
|
2015-12-17 18:22:56 +00:00
|
|
|
NSInteger offsetFromEnd = oldTextLength - start;
|
2017-07-18 21:33:31 +00:00
|
|
|
NSInteger newOffset = _backedTextInput.attributedText.length - offsetFromEnd;
|
|
|
|
UITextPosition *position = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:newOffset];
|
2017-07-18 21:33:45 +00:00
|
|
|
[_backedTextInput setSelectedTextRange:[_backedTextInput textRangeFromPosition:position toPosition:position]
|
|
|
|
notifyDelegate:YES];
|
2015-12-17 18:22:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
[_backedTextInput layoutIfNeeded];
|
2015-11-06 15:25:19 +00:00
|
|
|
|
2017-03-20 07:00:18 +00:00
|
|
|
[self invalidateContentSize];
|
2015-11-06 15:25:19 +00:00
|
|
|
|
|
|
|
_blockTextShouldChange = NO;
|
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:23 +00:00
|
|
|
#pragma mark - Properties
|
2015-04-29 08:29:00 +00:00
|
|
|
|
2015-05-14 16:37:39 +00:00
|
|
|
- (UIFont *)font
|
|
|
|
{
|
2017-07-18 21:33:31 +00:00
|
|
|
return _backedTextInput.font;
|
2015-05-14 16:37:39 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
- (void)setFont:(UIFont *)font
|
|
|
|
{
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput.font = font;
|
2017-05-29 22:56:46 +00:00
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
2017-03-20 07:00:23 +00:00
|
|
|
- (NSString *)text
|
|
|
|
{
|
2017-07-18 21:33:31 +00:00
|
|
|
return _backedTextInput.text;
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setText:(NSString *)text
|
|
|
|
{
|
|
|
|
NSInteger eventLag = _nativeEventCount - _mostRecentEventCount;
|
2017-07-18 21:33:31 +00:00
|
|
|
if (eventLag == 0 && ![text isEqualToString:_backedTextInput.text]) {
|
|
|
|
UITextRange *selection = _backedTextInput.selectedTextRange;
|
|
|
|
NSInteger oldTextLength = _backedTextInput.text.length;
|
2017-03-20 07:00:23 +00:00
|
|
|
|
|
|
|
_predictedText = text;
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput.text = text;
|
2017-03-20 07:00:23 +00:00
|
|
|
|
|
|
|
if (selection.empty) {
|
|
|
|
// maintain cursor position relative to the end of the old text
|
2017-07-18 21:33:31 +00:00
|
|
|
NSInteger start = [_backedTextInput offsetFromPosition:_backedTextInput.beginningOfDocument toPosition:selection.start];
|
2017-03-20 07:00:23 +00:00
|
|
|
NSInteger offsetFromEnd = oldTextLength - start;
|
|
|
|
NSInteger newOffset = text.length - offsetFromEnd;
|
2017-07-18 21:33:31 +00:00
|
|
|
UITextPosition *position = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument offset:newOffset];
|
2017-07-18 21:33:45 +00:00
|
|
|
[_backedTextInput setSelectedTextRange:[_backedTextInput textRangeFromPosition:position toPosition:position]
|
|
|
|
notifyDelegate:YES];
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[self invalidateContentSize];
|
|
|
|
} else if (eventLag > RCTTextUpdateLagWarningThreshold) {
|
2017-09-25 17:23:02 +00:00
|
|
|
RCTLogWarn(@"Native TextInput(%@) is %lld events ahead of JS - try to make your JS faster.", self.text, (long long)eventLag);
|
2017-03-20 07:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
#pragma mark - RCTBackedTextInputDelegate
|
2017-01-25 00:43:24 +00:00
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
- (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
|
2015-07-21 19:37:24 +00:00
|
|
|
{
|
2017-07-18 21:33:31 +00:00
|
|
|
if (!_backedTextInput.textWasPasted) {
|
2015-11-02 17:13:41 +00:00
|
|
|
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeKeyPress
|
|
|
|
reactTag:self.reactTag
|
|
|
|
text:nil
|
|
|
|
key:text
|
|
|
|
eventCount:_nativeEventCount];
|
|
|
|
}
|
|
|
|
|
2016-06-24 13:28:38 +00:00
|
|
|
// So we need to track that there is a native update in flight just in case JS manages to come back around and update
|
|
|
|
// things /before/ UITextView can update itself asynchronously. If there is a native update in flight, we defer the
|
|
|
|
// JS update when it comes in and apply the deferred update once textViewDidChange fires with the native update applied.
|
|
|
|
if (_blockTextShouldChange) {
|
|
|
|
return NO;
|
2015-07-21 19:37:24 +00:00
|
|
|
}
|
2016-06-24 13:28:38 +00:00
|
|
|
|
|
|
|
if (_maxLength) {
|
2017-07-18 21:33:31 +00:00
|
|
|
NSUInteger allowedLength = _maxLength.integerValue - _backedTextInput.text.length + range.length;
|
2016-06-24 13:28:38 +00:00
|
|
|
if (text.length > allowedLength) {
|
2016-07-14 14:39:18 +00:00
|
|
|
// If we typed/pasted more than one character, limit the text inputted
|
2016-06-24 13:28:38 +00:00
|
|
|
if (text.length > 1) {
|
|
|
|
// Truncate the input string so the result is exactly maxLength
|
|
|
|
NSString *limitedString = [text substringToIndex:allowedLength];
|
2017-07-18 21:33:31 +00:00
|
|
|
NSMutableString *newString = _backedTextInput.text.mutableCopy;
|
2016-06-24 13:28:38 +00:00
|
|
|
[newString replaceCharactersInRange:range withString:limitedString];
|
2017-07-18 21:33:31 +00:00
|
|
|
_backedTextInput.text = newString;
|
2016-07-14 14:39:18 +00:00
|
|
|
_predictedText = newString;
|
|
|
|
|
2016-06-24 13:28:38 +00:00
|
|
|
// Collapse selection at end of insert to match normal paste behavior
|
2017-07-18 21:33:31 +00:00
|
|
|
UITextPosition *insertEnd = [_backedTextInput positionFromPosition:_backedTextInput.beginningOfDocument
|
2016-06-24 13:28:38 +00:00
|
|
|
offset:(range.location + allowedLength)];
|
2017-07-18 21:33:45 +00:00
|
|
|
[_backedTextInput setSelectedTextRange:[_backedTextInput textRangeFromPosition:insertEnd toPosition:insertEnd]
|
|
|
|
notifyDelegate:YES];
|
2016-07-14 14:39:18 +00:00
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
[self textInputDidChange];
|
2016-06-24 13:28:38 +00:00
|
|
|
}
|
|
|
|
return NO;
|
2015-07-21 19:37:24 +00:00
|
|
|
}
|
2016-06-24 13:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_nativeUpdatesInFlight = YES;
|
|
|
|
|
|
|
|
if (range.location + range.length > _predictedText.length) {
|
|
|
|
// _predictedText got out of sync in a bad way, so let's just force sync it. Haven't been able to repro this, but
|
|
|
|
// it's causing a real crash here: #6523822
|
2017-07-18 21:33:31 +00:00
|
|
|
_predictedText = _backedTextInput.text;
|
2016-06-24 13:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NSString *previousText = [_predictedText substringWithRange:range];
|
|
|
|
if (_predictedText) {
|
|
|
|
_predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text];
|
2015-07-21 19:37:24 +00:00
|
|
|
} else {
|
2016-06-24 13:28:38 +00:00
|
|
|
_predictedText = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_onTextInput) {
|
|
|
|
_onTextInput(@{
|
|
|
|
@"text": text,
|
|
|
|
@"previousText": previousText ?: @"",
|
|
|
|
@"range": @{
|
|
|
|
@"start": @(range.location),
|
|
|
|
@"end": @(range.location + range.length)
|
|
|
|
},
|
|
|
|
@"eventCount": @(_nativeEventCount),
|
|
|
|
});
|
2015-07-21 19:37:24 +00:00
|
|
|
}
|
2016-06-24 13:28:38 +00:00
|
|
|
|
|
|
|
return YES;
|
2015-07-21 19:37:24 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 13:28:38 +00:00
|
|
|
static BOOL findMismatch(NSString *first, NSString *second, NSRange *firstRange, NSRange *secondRange)
|
|
|
|
{
|
|
|
|
NSInteger firstMismatch = -1;
|
|
|
|
for (NSUInteger ii = 0; ii < MAX(first.length, second.length); ii++) {
|
|
|
|
if (ii >= first.length || ii >= second.length || [first characterAtIndex:ii] != [second characterAtIndex:ii]) {
|
|
|
|
firstMismatch = ii;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firstMismatch == -1) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSUInteger ii = second.length;
|
|
|
|
NSUInteger lastMismatch = first.length;
|
|
|
|
while (ii > firstMismatch && lastMismatch > firstMismatch) {
|
|
|
|
if ([first characterAtIndex:(lastMismatch - 1)] != [second characterAtIndex:(ii - 1)]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ii--;
|
|
|
|
lastMismatch--;
|
|
|
|
}
|
|
|
|
|
|
|
|
*firstRange = NSMakeRange(firstMismatch, lastMismatch - firstMismatch);
|
|
|
|
*secondRange = NSMakeRange(firstMismatch, ii - firstMismatch);
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
- (void)textInputDidChange
|
2015-04-29 08:29:00 +00:00
|
|
|
{
|
2017-03-20 07:00:18 +00:00
|
|
|
[self invalidateContentSize];
|
2016-06-24 13:28:38 +00:00
|
|
|
|
2017-07-18 21:33:31 +00:00
|
|
|
// Detect when _backedTextInput updates happend that didn't invoke `shouldChangeTextInRange`
|
2016-06-24 13:28:38 +00:00
|
|
|
// (e.g. typing simplified chinese in pinyin will insert and remove spaces without
|
|
|
|
// calling shouldChangeTextInRange). This will cause JS to get out of sync so we
|
|
|
|
// update the mismatched range.
|
|
|
|
NSRange currentRange;
|
|
|
|
NSRange predictionRange;
|
2017-07-18 21:33:31 +00:00
|
|
|
if (findMismatch(_backedTextInput.text, _predictedText, ¤tRange, &predictionRange)) {
|
|
|
|
NSString *replacement = [_backedTextInput.text substringWithRange:currentRange];
|
|
|
|
[self textInputShouldChangeTextInRange:predictionRange replacementText:replacement];
|
2016-06-24 13:28:38 +00:00
|
|
|
// JS will assume the selection changed based on the location of our shouldChangeTextInRange, so reset it.
|
2017-07-18 21:33:31 +00:00
|
|
|
[self textInputDidChangeSelection];
|
|
|
|
_predictedText = _backedTextInput.text;
|
2016-06-24 13:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_nativeUpdatesInFlight = NO;
|
2015-07-21 19:37:24 +00:00
|
|
|
_nativeEventCount++;
|
2017-05-02 04:05:38 +00:00
|
|
|
|
|
|
|
if (!self.reactTag || !_onChange) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_onChange(@{
|
|
|
|
@"text": self.text,
|
|
|
|
@"target": self.reactTag,
|
|
|
|
@"eventCount": @(_nativeEventCount),
|
|
|
|
});
|
2015-04-29 08:29:00 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 00:43:24 +00:00
|
|
|
#pragma mark - UIScrollViewDelegate
|
|
|
|
|
2016-11-22 19:52:18 +00:00
|
|
|
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
|
|
|
|
{
|
|
|
|
if (_onScroll) {
|
2017-03-20 07:00:17 +00:00
|
|
|
CGPoint contentOffset = scrollView.contentOffset;
|
|
|
|
CGSize contentSize = scrollView.contentSize;
|
|
|
|
CGSize size = scrollView.bounds.size;
|
|
|
|
UIEdgeInsets contentInset = scrollView.contentInset;
|
|
|
|
|
2016-11-22 19:52:18 +00:00
|
|
|
_onScroll(@{
|
|
|
|
@"contentOffset": @{
|
2017-03-20 07:00:17 +00:00
|
|
|
@"x": @(contentOffset.x),
|
|
|
|
@"y": @(contentOffset.y)
|
2016-11-22 19:52:18 +00:00
|
|
|
},
|
|
|
|
@"contentInset": @{
|
2017-03-20 07:00:17 +00:00
|
|
|
@"top": @(contentInset.top),
|
|
|
|
@"left": @(contentInset.left),
|
|
|
|
@"bottom": @(contentInset.bottom),
|
|
|
|
@"right": @(contentInset.right)
|
2016-11-22 19:52:18 +00:00
|
|
|
},
|
|
|
|
@"contentSize": @{
|
2017-03-20 07:00:17 +00:00
|
|
|
@"width": @(contentSize.width),
|
|
|
|
@"height": @(contentSize.height)
|
2016-11-22 19:52:18 +00:00
|
|
|
},
|
|
|
|
@"layoutMeasurement": @{
|
2017-03-20 07:00:17 +00:00
|
|
|
@"width": @(size.width),
|
|
|
|
@"height": @(size.height)
|
2016-11-22 19:52:18 +00:00
|
|
|
},
|
2017-03-20 07:00:17 +00:00
|
|
|
@"zoomScale": @(scrollView.zoomScale ?: 1),
|
2016-11-22 19:52:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 08:29:00 +00:00
|
|
|
@end
|