2014-02-17 29 views
0

我有兩個textViews的數據被動態填充。一旦數據填充後,我想調整textview的高度,以便我看不到垂直滾動或文本被剪切。我想以編程方式執行此任務。如何自動擴展UITextview基於在ios中顯示字符串

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

[email protected]"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]; 
+0

你有沒有試着去'[txtvw sizeToFit]'? –

回答

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

賦予「MAXFLOAT」多少值。 – user3222991

+0

它是math.h中定義的一個常量,不需要爲其賦值。 – johnyu

0
[textview sizeToFit]; 
[textview.textContainer setSize:textview.frame.size]; 

在iOS的7 U可以使用:

[UITEXTVIEW sizeToFit]; 
[UITEXTVIEW layoutIfNeeded]; 

如果問題仍然存在,給更新