2012-05-16 39 views
1

我在滚动视图上使用UITextView ...并且我希望它在我写入内容时自动展开......但我无法做到这一点......iPhone: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; 

此代码是不是为我工作...

感谢

回答

5

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

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

0

HPGrowingTextView是与文本一UITextView它生长/收缩,并启动时,含量达到一定数目的行滚动。类似于苹果在短信应用中使用的一种。查看小型(过时)截屏视频的链接。

1

您的代码将无法使用。

您需要将您的代码分成2个片段,然后将它们放置到适当的位置,然后您的代码应该可以工作。

片段1(在我的测试中,我把在viewDidLoad中这个片段):

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]; 

确保以上片段来看,和您的文本视图显示在屏幕上,然后运行第二个片段。如果你的文本视图没有显示在屏幕上,那么contentView不会被初始化,并且高度是未定义的。

片段2(在我的测试中,我把这个片段在viewDidAppear):

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

祝你好运!