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;


随着内容的增加,我想增加文本字段的大小。

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

谢谢

最佳答案

您需要将代码分成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;


祝好运!

关于iphone - 我在ScrollView上使用UITextView…并且我想在其中写一些东西时自动展开…,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11504211/

10-12 00:18