如何根据ios中显示的字符串自动展开UITextview
我有两个文本视图,它们的数据是动态填充的。我想在填充数据后调整textview的高度,这样我就不会看到垂直滚动,也不会剪切文本。我想以编程方式完成这项任务如何根据ios中显示的字符串自动展开UITextview,ios,iphone,objective-c,uitextview,Ios,Iphone,Objective C,Uitextview,我有两个文本视图,它们的数据是动态填充的。我想在填充数据后调整textview的高度,这样我就不会看到垂直滚动,也不会剪切文本。我想以编程方式完成这项任务 tViewhobbies=[[UITextView alloc]initWithFrame:CGRectMake(10, 330,
tViewhobbies=[[UITextView alloc]initWithFrame:CGRectMake(10,
330,
300,
strSize.height+20)];
tViewhobbies.backgroundColor=[UIColor clearColor];
tViewhobbies.layer.cornerRadius=5;
[tViewhobbies setUserInteractionEnabled:NO];
tViewhobbies.font=[UIFont systemFontOfSize:12.0];
tViewhobbies.backgroundColor=[UIColor colorWithRed:0.662745
green:0.662745
blue:0.662745
alpha:0.5];
tViewhobbies.delegate=self;
tViewhobbies.scrollEnabled=YES;
[scrollView addSubview:tViewhobbies];
lblInterests = [[UILabel alloc]initWithFrame:CGRectMake(10,
410,
300,
strSize.height+0)];
lblInterests.text=@"Interests";
lblInterests.font=[UIFont systemFontOfSize:16.0];
lblInterests.textColor=[UIColor colorWithRed:153.0f/255.0f
green:153.0f/255.0f
blue:153.0f/255.0f
alpha:1];
[scrollView addSubview:lblInterests];
tViewInterests=[[UITextView alloc]initWithFrame:CGRectMake(10,
430,
300,
strSize.height+30)];
tViewInterests.backgroundColor=[UIColor clearColor];
tViewInterests.layer.cornerRadius=5;
[tViewInterests setUserInteractionEnabled:NO];
tViewInterests.font=[UIFont systemFontOfSize:14.0];
tViewInterests.backgroundColor=[UIColor colorWithRed:0.662745
green:0.662745
blue:0.662745
alpha:0.5];
tViewInterests.delegate=self;
tViewInterests.scrollEnabled=YES;
[scrollView addSubview:tViewInterests];
试试这个:
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
CGRect rect =
[aTextView.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
aTextView.frame = CGRectMake(0, 0,300, rect.size.height);
在iOS 7中,您可以使用:
[UITEXTVIEW sizeToFit];
[UITEXTVIEW layoutIfNeeded];
如果问题仍然存在,请更新您是否尝试过
[txtvw sizeToFit]
?它是math.h中定义的常量,您不需要为其赋值。
[UITEXTVIEW sizeToFit];
[UITEXTVIEW layoutIfNeeded];