2

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

4

2 回答 2

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];
}
于 2013-10-29T10:29:58.907 回答
-1

很容易根据 UITextView 的内容使用UITextView contentSize.

CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
于 2013-10-29T10:35:05.440 回答