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.
相关文章推荐
- UILabel、UITextView根据字符串动态计算高度
- UITextView自适应高度动态计算高度
- UITextField,UITextView,UILabel 根据内容来计算高度
- ios UIWebView 展示HTML代码(UITableView 中添加webview 动态计算高度)
- iOS NSString 没有去除首尾空格,换行符,空字符--导致UIlabel不能正确计算换行后的动态高度
- 动态计算UITableViewCell高度详解
- UiTextView 和UILabel 高度计算
- iOS根据字符串计算UITextView高度
- Swift iOS tableView static cell动态计算高度
- 动态计算UITableViewCell高度详解
- UITextview 根据输入的内容动态改变高度
- Android 动态改变高度以及计算长度的EditText
- 动态计算UITableViewCell高度详解
- 动态计算UITableViewCell的高度
- 键盘弹起及lab时的动态计算高度 --董鑫
- 动态计算UITableViewCell高度详解
- 动态计算UITableViewCell高度详解
- ios - lable宽度或高度动态计算
- 动态计算UITableViewCell高度详解
- 根据Label的文字内容动态计算Label高度