您的位置:首页 > 产品设计 > UI/UE

UITextView 动态高度计算(iOS7版)

2014-04-25 17:49 302 查看
NSDictionary*attrsDictionary=[NSDictionarydictionaryWithObject:[UIFontsystemFontOfSize:kCellContentFontSize]

forKey:NSFontAttributeName];

NSAttributedString*attributedText=[[[NSAttributedStringalloc]initWithString:_contentStrattributes:attrsDictionary]autorelease];

NSIntegerdetailHeight=[BZDataDealertextViewHeightForAttributedText:attributedText

andWidth:detailLabelWidth];

UITextView*detailTextView=[[UITextViewalloc]initWithFrame:CGRectMake(10,35,detailLabelWidth,detailHeight+3)];//加3个像素

detailTextView.scrollEnabled=NO;//是否允许滚动会影响高度的展示

detailTextView.text=_contentStr;

[containerViewaddSubview:detailTextView];


+(NSInteger)textViewHeightForAttributedText:(NSAttributedString*)textandWidth:(CGFloat)width
{
UITextView*textView=[[UITextViewalloc]init];
[textViewsetAttributedText:text];
CGSizesize=[textViewsizeThatFits:CGSizeMake(width,FLT_MAX)];
return(NSInteger)(size.height);
}


以前的方法用先赋值,再取textview的contentsize.height,iOS7后,这个不能和了.

苹果目前很多情况下都是推荐使用NSAttributedString来计算一些数据.

ps:如果

detailTextView.scrollEnabled=NO;//是否允许滚动会影响高度的展示

不允许滚动,计算出的高度还是会少一点.所以上面加了3个像素.

不知道是不是苹果你妹的bug.


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: