如何根据在ios中显示字符串自动扩展UITextview

时间:2014-02-17 10:42:48

标签: ios iphone objective-c uitextview

我有两个textViews,其数据动态填充。我想在填充数据后调整textview的高度,以便我看不到垂直滚动,也不会剪切文本。我想以编程方式完成这项任务。

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

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

[textview sizeToFit];
[textview.textContainer setSize:textview.frame.size];
iOS 7中的

你可以使用:

[UITEXTVIEW sizeToFit];
[UITEXTVIEW layoutIfNeeded];

如果问题仍然存在,请提供更新