ios - 如何在用户键入时创建增加框架大小而不是增加滚动大小的 TextView

标签 ios iphone objective-c uitextfield uitextview

我需要创建两个文本输入字段,一个在另一个下面。当用户在上部文本字段中键入内容时,我需要动态增加框架大小(不建议在 TextView 内滚动。)并且下部文本输入框架应向下。这在ios上可以实现吗?任何帮助将不胜感激。

最佳答案

您可以通过其 contentSize 更新 textView 的框架,如下所示:

-(void)updateTextViewSize
{
   CGRect rect = textView.frame;
   rect.size.width = textView.contentSize.width;
   rect.size.height = textView.contentSize.height;
   textView.frame = rect;

   //lowering the textView2 below this textView
   //assuming that the lower textview touches the 
   //bottom of the upper textView.

   CGRect rect2 = textViewLower.frame;
   rect2.origin.y = textView.frame.origin.y + textView.frame.size.height;

   textViewLower.frame = rect2;

   //update your scrollView here
   //assuming textView and textViewLower are already added to the scrollView

   scrollView.contentSize = CGSizeMake(scrollView.contentSize.width, textViewLower.frame.origin.y + textViewLower.frame.size.height);
}

//implement this delegate method as shown below
-(void)textViewDidChange:(UITextView*)textView
{
  [self updateTextViewSize];
}

关于ios - 如何在用户键入时创建增加框架大小而不是增加滚动大小的 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19655376/

相关文章:

ios - GestureRecognizer 仅适用于一个单元格

iphone - UIView背景颜色

objective-c - UITableView 像联系人应用程序编辑模式

iphone - 自定义标注在 ios6 中显示不正确?

ios - 在重用 UITableViewCell 时使用 SDWebImage 处理图像下载

ios - 仅当发生碰撞时才运行 skaction

ios - writeToFile 不适用于保存照片

iphone - 更新应用程序后保留数据

iphone - 类似杂志的版式是如何制作的?

objective-c - 调整 UIView 的大小