ios - contentOffset Changes when Setting UITextView Frame in textViewDidChange -
i trying set height of uitextview
dynamically in textviewdidchange
. frame adjust proper height, when height greater threshold have set, textview should scroll new line. however, contentoffset
gets messed , doesn't scroll down include new line.
- (void)viewdidload { [super viewdidload]; _textview.frame = cgrectmake(60, 100, 200, _textview.contentsize.height); _textview.scrollenabled = no; } - (void)textviewdidchange:(uitextview *)textview { nslog(@"offset: %f", _textview.contentoffset.y); cgfloat height = _textview.contentsize.height; _textview.scrollenabled = no; if (_textview.contentsize.height >= 120) { height = 120; _textview.scrollenabled = yes; _textview.contentoffset = cgpointmake(0, _textview.contentsize.height - _textview.frame.size.height); } cgrect frame = _textview.frame; frame.size.height = height; _textview.frame = frame; }
i suppose easier way of explaining happening this. on blank view controller add uitextview in storyboard. add these 3 lines textviewdidchange:
cgrect frame = _textview.frame; frame.size.height = _textview.contentsize.height; _textview.frame = frame;
every other time textview's height increases, text not vertically centered. if scroll around though , let go return correct position. frame correct size. contentoffset not quite right.
i wonder if post code you're doing?
i've tried on 5.0 , works.
-(void) textviewdidchange:(uitextview *)textview { int maxheight = 30; cgfloat height = [textview.text sizewithfont:textview.font constrainedtosize:cgsizemake(textview.frame.size.width, 99999) linebreakmode:nslinebreakbywordwrapping].height; cgrect rect = textview.frame; rect.size.height = min(height, maxheight); [textview setframe:rect];
}
Comments
Post a Comment