UITextView 动态高度计算(iOS7版)

 NSDictionary *attrsDictionary = [NSDictionarydictionaryWithObject:[UIFontsystemFontOfSize:kCellContentFontSize]

                                                                forKey:NSFontAttributeName];

    NSAttributedString *attributedText = [[[NSAttributedString alloc] initWithString:_contentStr attributes:attrsDictionary] autorelease];

    NSInteger detailHeight = [BZDataDealer textViewHeightForAttributedText:attributedText

                                                                  andWidth:detailLabelWidth];

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

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

    detailTextView.text = _contentStr;

    [containerView addSubview:detailTextView];

 

+ (NSInteger)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width
{
    UITextView *textView = [[UITextView alloc] init];
    [textView setAttributedText:text];
    CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)];
    return (NSInteger)(size.height);
}

 

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

 

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

 

ps: 如果 

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

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

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

 

 

 

转载于:https://www.cnblogs.com/willbin/p/3689305.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值