计算UITextView的滑动高度

 1 - (CGFloat)getHeightByTextView:(UITextView *)myTextView withContent:(NSString *)content withFontSize:(CGFloat)size withTextColor:(UIColor *)color
 2 {
 3     CGFloat newSizeH = 0.;
 4     //UITextView的实际高度
 5     if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
 6     {
 7         //7.0以后需要自己计算高度
 8         float fPadding = 16.0; //8.0px x 2
 9         
10 //        CGSize constraint = CGSizeMake(myTextView.contentSize.width - fPadding,CGFLOAT_MAX);
11 //        CGSize size = [self.textView.text sizeWithFont:myTextView.font constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
12 //        newSizeH = size.height;
13         
14         CGSize constraint = CGSizeMake(myTextView.contentSize.width - fPadding, CGFLOAT_MAX);
15         NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica Neue" size:size],NSFontAttributeName,color,NSForegroundColorAttributeName, nil];
16         NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
17         CGRect rect = [myTextView.text boundingRectWithSize:constraint options:options  attributes:attrsDictionary context:nil];
18         newSizeH = rect.size.height;
19     }
20     else
21     {
22         newSizeH = myTextView.contentSize.height + 10;
23     }
24     return newSizeH;
25 }

 

posted on 2015-03-20 20:10  zcz1513  阅读(309)  评论(0编辑  收藏  举报

导航