Reputation: 51
I need to capture the typed text, before rendering, in order to apply some attributes to it.
For that i'm using the delegate method shouldChangeTextInRange of the UITextView along with the NSAttributedString. So far so good, and it works.
The problem is, when the UITextView gets a certain amount of text and it starts scrolling, changing the attributedText makes it scroll to the top, while the next char will make it scroll back to the end.
It keeps on that dance moving up and down while typing..
Setting textview.text, instead of the attributed text, it works n
Any suggestion?
Thanks in advance.
- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSAttributedString * newString = [[NSAttributedString alloc] initWithString:text attributes:nil];
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithAttributedString:_textView.attributedText];
[string insertAttributedString:newString atIndex:range.location];
textView.attributedText = string;
range.location += text.length;
textView.selectedRange = range;
return NO;
}
Upvotes: 4
Views: 1714
Reputation: 6144
textView.layoutManager.allowsNonContiguousLayout = NO;
Then it will not scroll to the top when changing the attributedText. It works for me.
Upvotes: 1
Reputation: 990
I'm not sure about the point of your shouldChangeTextInRange
, but to fix the undesired auto-scrolling, try surrounding the setting of textView.attributedText
with [textView setScrollEnabled:NO]
.
That is:
[textView setScrollEnabled:NO];
textView.attributedText = string;
range.location += text.length;
[textView setScrollEnabled:YES];
Upvotes: 0