我在滚动视图上使用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;
此代码对我不起作用......
由于
答案 0 :(得分: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;
}
答案 1 :(得分: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;
祝你好运!
答案 2 :(得分:0)
HPGrowingTextView
是一个UITextView
,随着文本的增长/缩小,当内容达到一定数量的行时开始滚动。类似于Apple在SMS-app中使用的那个。查看小型(过时)截屏视频的链接。