我在滚动视图上使用UITextView ...并且我想在其中写一些东西时自动扩展... ...但是我无法做到...

textViewBusiness = [[UITextView alloc] initWithFrame:CGRectMake(25,332,268,60)];
textViewBusiness.text=strMyBusiness;
textViewBusiness.editable=NO;
textViewBusiness.font = [UIFont fontWithName:@"Arial" size: 17.0];
textViewBusiness.layer.borderWidth = 2.0f;
textViewBusiness.layer.borderColor = [[UIColor grayColor] CGColor];
textViewBusiness.backgroundColor = [UIColor clearColor];
[textViewBusiness setTextColor:[UIColor blackColor]];
[self.scrollView addSubview: textViewBusiness];

CGRect frame = textViewBusiness.frame;
frame.size.height = textViewBusiness.contentSize.height;
textViewBusiness.frame = frame;

此代码对我不起作用...

谢谢

最佳答案

确保已设置文本字段的委托...

someTextField.delegate = self;

然后在适当的文本字段委托方法中调整textView的大小。
- (void)textViewDidChange:(UITextView *)textView;

- (void) textViewDidEndEditing:(UITextView *)textView;

您上面添加的代码是正确的。只要确保它在正确的位置即可。
- (void)textViewDidChange:(UITextView *)textView
{
   CGRect frame = textView.frame;
   frame.size.height = textView.contentSize.height;
   textView.frame = frame;
 }

Dynamic expand UITextView

07-26 09:37