Skip to content

Commit 3acafd1

Browse files
sherginfacebook-github-bot
authored andcommitted
Better TextInput: Removed redundant UIScrollView from RCTTextView
Reviewed By: mmmulani Differential Revision: D4640207 fbshipit-source-id: 01fc65b0212ad6baef500625679dab5e99da9db5
1 parent d272334 commit 3acafd1

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

Libraries/Text/RCTTextView.m

+33-32
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ - (void)didMoveToWindow
5858
}
5959
}
6060

61+
- (void)setContentOffset:(CGPoint)contentOffset animated:(__unused BOOL)animated
62+
{
63+
// Turning off scroll animation.
64+
// This fixes the problem also known as "flaky scrolling".
65+
[super setContentOffset:contentOffset animated:NO];
66+
}
67+
6168
@end
6269

6370
@implementation RCTTextView
@@ -69,7 +76,6 @@ @implementation RCTTextView
6976
UITextView *_textView;
7077
RCTText *_richTextView;
7178
NSAttributedString *_pendingAttributedText;
72-
UIScrollView *_scrollView;
7379

7480
UITextRange *_previousSelectionRange;
7581
NSUInteger _previousTextLength;
@@ -100,27 +106,23 @@ - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
100106
#if !TARGET_OS_TV
101107
_textView.scrollsToTop = NO;
102108
#endif
103-
_textView.scrollEnabled = NO;
109+
_textView.scrollEnabled = YES;
104110
_textView.delegate = self;
105111

106-
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
107-
#if !TARGET_OS_TV
108-
_scrollView.scrollsToTop = NO;
109-
#endif
110-
_scrollView.delegate = self;
111-
[_scrollView addSubview:_textView];
112-
113-
[self addSubview:_scrollView];
112+
[self addSubview:_textView];
114113
}
115114
return self;
116115
}
117116

118117
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
119118
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
120119

120+
#pragma mark - RCTComponent
121+
121122
- (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)index
122123
{
123124
[super insertReactSubview:subview atIndex:index];
125+
124126
if ([subview isKindOfClass:[RCTText class]]) {
125127
if (_richTextView) {
126128
RCTLogError(@"Tried to insert a second <Text> into <TextInput> - there can only be one.");
@@ -143,11 +145,6 @@ - (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)index
143145
}
144146
}
145147

146-
- (void)dealloc
147-
{
148-
_scrollView.delegate = nil;
149-
}
150-
151148
- (void)removeReactSubview:(UIView *)subview
152149
{
153150
[super removeReactSubview:subview];
@@ -159,9 +156,11 @@ - (void)removeReactSubview:(UIView *)subview
159156

160157
- (void)didUpdateReactSubviews
161158
{
162-
// Do nothing, as we don't allow non-text subviews
159+
// Do nothing, as we don't allow non-text subviews.
163160
}
164161

162+
#pragma mark - Routine
163+
165164
- (void)setMostRecentEventCount:(NSInteger)mostRecentEventCount
166165
{
167166
_mostRecentEventCount = mostRecentEventCount;
@@ -262,7 +261,6 @@ - (void)updateFrames
262261
CGRect frame = UIEdgeInsetsInsetRect(self.bounds, adjustedFrameInset);
263262
_textView.frame = frame;
264263
_placeholderView.frame = frame;
265-
_scrollView.frame = frame;
266264
[self updateContentSize];
267265

268266
_textView.textContainerInset = adjustedTextContainerInset;
@@ -271,10 +269,8 @@ - (void)updateFrames
271269

272270
- (void)updateContentSize
273271
{
274-
CGSize size = (CGSize){_scrollView.frame.size.width, INFINITY};
275-
size.height = [_textView sizeThatFits:size].height;
276-
_scrollView.contentSize = size;
277-
_textView.frame = (CGRect){CGPointZero, size};
272+
CGSize size = _textView.frame.size;
273+
size.height = [_textView sizeThatFits:CGSizeMake(size.width, INFINITY)].height;
278274

279275
if (_viewDidCompleteInitialLayout && _onContentSizeChange && !CGSizeEqualToSize(_previousContentSize, size)) {
280276
_previousContentSize = size;
@@ -725,26 +721,31 @@ - (UIColor *)defaultPlaceholderTextColor
725721
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
726722
{
727723
if (_onScroll) {
724+
CGPoint contentOffset = scrollView.contentOffset;
725+
CGSize contentSize = scrollView.contentSize;
726+
CGSize size = scrollView.bounds.size;
727+
UIEdgeInsets contentInset = scrollView.contentInset;
728+
728729
_onScroll(@{
729730
@"contentOffset": @{
730-
@"x": @(scrollView.contentOffset.x),
731-
@"y": @(scrollView.contentOffset.y)
731+
@"x": @(contentOffset.x),
732+
@"y": @(contentOffset.y)
732733
},
733734
@"contentInset": @{
734-
@"top": @(_scrollView.contentInset.top),
735-
@"left": @(_scrollView.contentInset.left),
736-
@"bottom": @(_scrollView.contentInset.bottom),
737-
@"right": @(_scrollView.contentInset.right)
735+
@"top": @(contentInset.top),
736+
@"left": @(contentInset.left),
737+
@"bottom": @(contentInset.bottom),
738+
@"right": @(contentInset.right)
738739
},
739740
@"contentSize": @{
740-
@"width": @(_scrollView.contentSize.width),
741-
@"height": @(_scrollView.contentSize.height)
741+
@"width": @(contentSize.width),
742+
@"height": @(contentSize.height)
742743
},
743744
@"layoutMeasurement": @{
744-
@"width": @(_scrollView.frame.size.width),
745-
@"height": @(_scrollView.frame.size.height)
745+
@"width": @(size.width),
746+
@"height": @(size.height)
746747
},
747-
@"zoomScale": @(_scrollView.zoomScale ?: 1),
748+
@"zoomScale": @(scrollView.zoomScale ?: 1),
748749
});
749750
}
750751
}

0 commit comments

Comments
 (0)