If you do not want to use KVO, you can also manually adjust the offset by exporting this code to a function like this:
-(void)adjustContentSize:(UITextView*)tv{ CGFloat deadSpace = ([tv bounds].size.height - [tv contentSize].height); CGFloat inset = MAX(0, deadSpace/2.0); tv.contentInset = UIEdgeInsetsMake(inset, tv.contentInset.left, inset, tv.contentInset.right); }
and calling him
-(void)textViewDidChange:(UITextView *)textView{ [self adjustContentSize:textView]; }
and every time you edit text in code. Remember to set the controller as a delegate
Swift 3 version:
func adjustContentSize(tv: UITextView){ let deadSpace = tv.bounds.size.height - tv.contentSize.height let inset = max(0, deadSpace/2.0) tv.contentInset = UIEdgeInsetsMake(inset, tv.contentInset.left, inset, tv.contentInset.right) } func textViewDidChange(_ textView: UITextView) { self.adjustContentSize(tv: textView) }
Beninho85 May 6 '15 at 20:39 2015-05-06 20:39
source share