如何在增加帧大小的情况下创建TextView,而不是在用户键入时增加滚动大小

时间:2013-10-29 10:19:57

标签: ios iphone objective-c uitextfield uitextview

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

2 个答案:

答案 0 :(得分:4)

您可以通过其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];
}

答案 1 :(得分:-1)

使用UITextView contentSize.

可以很容易地根据内容将UITextView的大小调整到正确的高度
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;