I have a UITextView which is designed to enlarge to fit the contentView when needed. When I paste in a paragraph of text, however, it puts the start and end points of the content vertically in the wrong places. Entering or deleting a character resets it back to the correct position.
Any ideas why this is?
-(void)textViewDidChange:(UITextView *)textView {
self.textView.frame = CGRectMake(
self.textView.frame.origin.x,
self.textView.frame.origin.y,
self.textView.frame.size.width,
self.textView.contentSize.height + HEADER_ADDITIONAL_HEIGHT);
self.textView.contentOffset = CGPointMake(0, 0);
self.previousContentSize = textView.contentSize;
}
When I used:
textView.contentSize = textView.frame.size;
textView.contentOffset = CGPointZero;
It solved my issue, but created a new issue where we sometimes get weird scrolling while typing or deleting text. So, I used this:
textView.contentSize = CGSizeMake( textView.contentSize.width,
textView.contentSize.height+1);
This also solved the issue. I think what we all need here is the effect which we get whenever the contentSize
of a textview is changed. Unfortunately, I do not know what this effect is. If somebody knows, please tell.
Update: I have found a method which you can use to solve your issue (I used this to resolve mine). You can ask NSLayoutMAnager to refresh the entire layout:
[textView.textStorage edited:NSTextStorageEditedCharacters range:NSMakeRange(0, textView.textStorage.length) changeInLength:0];
NSLayoutManager attempts to avoid refreshing the layout because it's time consuming and takes a lot of work, so it's set up to only do it when absolutely necessary (lazily).
There are a number of invalidateLayout
functions related to this class but none of them cause an actual re-layout when called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With