iOS的文字高度计算一直是个问题, 苹果也一直在改, 这几天看了一下 boundingRectWithSize 方法.

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(6_0);

  

踩了几个坑后, 总算找到比较好的使用方法.

参考: http://stackoverflow.com/questions/13621084/boundingrectwithsize-for-nsattributedstring-returning-wrong-size

使用时的注意事项:

1: NSAttributedString 的每个部分都要至少设置两个属性:

NSFontAttributeName

NSForegroundColorAttributeName

2: NSStringDrawingOptions 的值, 在多行的情况下, 至少要有

NSStringDrawingUsesLineFragmentOrigin

NSStringDrawingUsesFontLeading

3: 如果文字中可能会出现emoji表情的话, emoji的高度比文字要高一点点,

我的方法是简单的在高度基础上加了两个像素.

(用CoreText可能会好一些, 但相对复杂.)

附代码:

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:fullDescAndTagStr];

NSRange allRange = [fullDescAndTagStr rangeOfString:fullDescAndTagStr];
[attrStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:13.0]
range:allRange];
[attrStr addAttribute:NSForegroundColorAttributeName
value:[UIColor blackColor]
range:allRange]; NSRange destRange = [fullDescAndTagStr rangeOfString:tagStr];
[attrStr addAttribute:NSForegroundColorAttributeName
value:HEXCOLOR(0x009cdd)
range:destRange]; CGFloat titleHeight; NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX)
options:options
context:nil];
titleHeight = ceilf(rect.size.height); return titleHeight+2; // 加两个像素,防止emoji被切掉.

  

boundingRectWithSize 的使用, 计算UILable高度, 包含Emoji及多属性string.的更多相关文章

  1. iOS依据字符串计算UITextView高度

    iOS计算字符串高度,有须要的朋友能够參考下. 方法一:ios7.0之前适用 /** @method 获取指定宽度width,字体大小fontSize,字符串value的高度 @param value ...

  2. 动态计算UITableViewCell高度

    动态计算UITableViewCell高度 UILabel in UITableViewCell Auto Layout - UILabel的属性Lines设为了0表示显示多行.Auto Layout ...

  3. ios 根据文字数量计算UILabel高度(已修改)

    由于留言的朋友给出了更好的方法,所以下面的代码都是它留言中给出的,优于我前面计算Lable高度方法,这个可以说非常的准,是IOS自带的计算UILABEL高度的方式. 一.实现代码 // 创建label ...

  4. iOS 学习 - 11.圆角(小于等于四个)类似气泡和计算字符高度

    使用贝塞尔曲线, // 小于四个角 圆角 -(void)setbor{ NSString *str = @" couldn't fit this all in a comment to @l ...

  5. Swift - 计算文本高度

    Swift - 计算文本高度 效果 源码 // // String+StringHeight.swift // StringHeight // // Created by YouXianMing on ...

  6. 根据文字动态计算Label高度或宽度

    //根据已知的label宽度计算文字高度 CGRect rect = [reson boundingRectWithSize:CGSizeMake(label_W, 0) options:NSStri ...

  7. 转:动态计算UITableViewCell高度详解

    转自:http://www.cocoachina.com/industry/20140604/8668.html   不知道大家有没有发现,在iOS APP开发过程中,UITableView是我们显示 ...

  8. 动态计算UITableViewCell高度详解

    本文将介绍四种情况下UITableViewCell的计算方式,分别是: Auto Layout with UILabel in UITableViewCell Auto Layout with UIT ...

  9. 动态计算UITableViewCell高度详解 (转)

    感觉挺有用的一篇文章,分析了4种解决方案.回头测试之.如果有别的方案,我会在后面补上. 原文地址:http://www.ifun.cc/blog/2014/02/21/dong-tai-ji-suan ...

随机推荐

  1. From MSI to WiX, Part 2 - ARP support, by Alex Shevchuk

    Following content is directly reprinted from From MSI to WiX, Part 2 - ARP support Author: Alex Shev ...

  2. Java7 switch新特性

    在Java7之前,switch只能匹配整数值,和字符:而Java7添加了字符串的匹配特性. 代码如下: package blog; public class Main { public static ...

  3. win/linux 下使用 psutil 获取进程 CPU / memory / IO 占用信息

    psutil - A cross-platform process and system utilities module for Python 1. 安装 pip 安装即可. windows 下需要 ...

  4. OpenJudge 2802 小游戏 / Poj 1101 The Game

    1.链接地址: http://bailian.openjudge.cn/practice/2802 http://poj.org/problem?id=1101 2.题目: 总时间限制: 1000ms ...

  5. Linux中的sed

    sed [选项] [动作] 文件 选项:     -n :静默模式.使用-n则只有经过sed处理的那一行.     -e :允许多重编辑:       -f :结果默认输出到终端,使用-f会将结果写在 ...

  6. checked

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  7. lnmp 虚拟主机配置及重写

    lnmp安装与调试,请看 http://www.cnblogs.com/lin3615/p/4376224.html 虚拟主机的配置编辑nginx配置文件 nginx.conf此为主配置文件 vim ...

  8. VB winform自动更新 笔记

    看网上各种自动更新方法,最后自己找了个比较简单的,在此做个笔记. 服务器上的共享盘里存放生成的可执行文件和XML格式的配置: <?xml version="1.0" enco ...

  9. div重叠不变形

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. [HTML]marquee标签属性详解

    请大家先看下面这段代码:<marquee direction=upbehavior=scrollloop=3scrollamount=1 scrolldelay=10align=topbgcol ...