iPhone:UITextView在编辑时不会扩展

swe*_*ank 1 uitextview ios xcode4

我在滚动视图上使用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;
Run Code Online (Sandbox Code Playgroud)

这段代码对我不起作用......

谢谢

Bra*_*ker 5

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

someTextField.delegate = self;
Run Code Online (Sandbox Code Playgroud)

然后在相应的文本字段委托方法中调整textView的大小.

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

- (void) textViewDidEndEditing:(UITextView *)textView;
Run Code Online (Sandbox Code Playgroud)

您在上面添加的代码是正确的.只要确保它在正确的位置.

- (void)textViewDidChange:(UITextView *)textView
{
   CGRect frame = textView.frame;
   frame.size.height = textView.contentSize.height;
   textView.frame = frame;
 }
Run Code Online (Sandbox Code Playgroud)

动态扩展UITextView