- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
int numLines = textView.contentSize.height/textView.font.lineHeight;
CGFloat goodHeight = BASE_COMMENT_HEIGHT + ((numLines-1) * textView.font.lineHeight);
CGFloat currentHeight = textView.frame.size.height;
CGFloat heightDiff = goodHeight - currentHeight;
if (heightDiff != 0) {
CGRect containerFrame = self.commentInputContainer.frame;
containerFrame.size.height += heightDiff;
containerFrame.origin.y -= heightDiff;
self.commentInputContainer.frame = containerFrame;
CGRect commentFrame = self.inlineCommentField.frame;
commentFrame.origin.y -= heightDiff;
commentFrame.size.height += heightDiff;
self.inlineCommentField.frame = commentFrame;
}
return YES;
}
我正在尝试动态调整评论字段的大小。当评论行数增加时,我打算增加字段的大小和字段容器的大小。
上述代码的问题是,当我尝试调整父(容器)和子(commentField)的大小时,子项的大小不会改变。
为什么会发生这种情况,我该如何解决这个问题?
答案 0 :(得分:0)
要获得调整大小以显示当前文本输入的UITextView,您可以禁用滚动并使用autolayout。使textView的superview的高度取决于textView的高度。