如何在内部输入时调整UITextView的大小? [英] How to resize UITextView while typing inside it?

查看:105
本文介绍了如何在内部输入时调整UITextView的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个评论部分,就像Facebook在其iOS应用程序中用于其消息部分的评论部分。我希望 UITextView 调整高度,以便我输入的文本适合它,而不是必须滚动才能看到溢出的文本。我有什么想法可以这样做吗?我调查了可能使用 CGRect 分配给文本视图的大小和高度,然后匹配内容大小:

I'm creating a comment section much like the one Facebook uses for it's messaging section in it's iOS app. I want the UITextView to resize the height so the text I'm typing fits inside it rather than you having to scroll to see the text that overflows. Any ideas how I might go about doing this? I've looked into maybe using a CGRect that is assigned to the size and height of the text view which then matches the content size:

CGRect textFrame = textView.frame;
textFrame.size.height = textView.contentSize.height;
textView.frame = textFrame;

我假设我需要某种功能来检测文本何时达到<$ c的界限$ c> UITextView 然后调整视图的高度?有没有人在用同样的概念挣扎?

I assume I need some sort of function that detects when the text reaches the bounds of the UITextView and then resizes the height of the view? Has anyone struggled with this same concept?

推荐答案

你可以在这个委托方法中调整框架,不要忘记将textView的委托设置为自。

You can adjust frame in this delegate method, do not forget to set textView's delegate to self.

-(BOOL)textView:(UITextView *)_textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
      [self adjustFrames];  
      return YES;
}


-(void) adjustFrames
{
   CGRect textFrame = textView.frame;
   textFrame.size.height = textView.contentSize.height;
   textView.frame = textFrame;
}

此解决方案适用于iOS6及之前...适用于iOS7请参阅此

this solution is for iOS6 and prior... for iOS7 refer to this

StackOverflow答案

这篇关于如何在内部输入时调整UITextView的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆