Skip to content

Commit

Permalink
[TextControls] Fix jumpiness in text areas
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 322590189
  • Loading branch information
andrewoverton authored and material-automation committed Jul 22, 2020
1 parent 9039d47 commit 66497f7
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions components/TextControls/src/BaseTextAreas/MDCBaseTextArea.m
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,16 @@ - (void)postLayoutSubviews {
if (![self validateHeight]) {
[self invalidateIntrinsicContentSize];
}
[self.textView scrollRangeToVisible:self.textView.selectedRange];
[self scrollToVisibleText];
}

- (void)scrollToVisibleText {
// This method was added to address b/161887902, with help from
// https://stackoverflow.com/a/49631521
NSRange range = NSMakeRange(self.textView.text.length - 1, 1);
[self.textView scrollRangeToVisible:range];
self.textView.scrollEnabled = NO;
self.textView.scrollEnabled = YES;
}

- (MDCBaseTextAreaLayout *)calculateLayoutWithSize:(CGSize)size {
Expand Down Expand Up @@ -285,7 +294,7 @@ - (void)layoutGradientLayers {
}

- (CGFloat)numberOfLinesOfText {
// For more context on measurinig the lines in a UITextView see here:
// For more context on measuring the lines in a UITextView see here:
// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/TextLayout/Tasks/CountLines.html
NSLayoutManager *layoutManager = self.textView.layoutManager;
NSUInteger numberOfGlyphs = layoutManager.numberOfGlyphs;
Expand All @@ -298,8 +307,10 @@ - (CGFloat)numberOfLinesOfText {
numberOfLines += 1;
}

if (self.textView.text.length > 0 &&
[self.textView.text characterAtIndex:self.textView.text.length - 1] == '\n') {
BOOL textEndsInNewLine =
self.textView.text.length > 0 &&
[self.textView.text characterAtIndex:self.textView.text.length - 1] == '\n';
if (textEndsInNewLine) {
numberOfLines += 1;
}
return (CGFloat)numberOfLines;
Expand Down

0 comments on commit 66497f7

Please sign in to comment.