boundingRectWithSize
来源:互联网 发布:破获网络贩毒 编辑:程序博客网 时间:2024/05/06 08:56
boundingRectWithSize 的使用, 计算UILable高度, 包含Emoji及多属性string.
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被切掉.
http://www.csdn123.com/html/topnews201408/92/1692.htm
0 0
- boundingRectWithSize
- boundingRectWithSize 备忘
- iOS 【boundingRectWithSize】
- boundingRectWithSize:options:attributes:context:
- sizeWithFont和boundingRectWithSize
- sizeWithFont转boundingRectWithSize
- boundingRectWithSize:options:attributes:context:
- iOS 7使用boundingRectWithSize
- boundingRectWithSize:options:context:
- lable boundingRectWithSize IOS6
- iOS boundingRectWithSize使用
- boundingRectWithSize 方法的使用
- boundingRectWithSize 的使用
- 使用boundingRectWithSize的注意事项
- ios boundingRectWithSize 使用方法 例子
- boundingRectWithSize计算label 高度
- ios boundingRectWithSize 方法使用
- boundingRectWithSize:options:attributes:context
- Android Support包里究竟有什么
- 第十二周项目四----利用遍历思想求解图问题之输出通过一个节点的所有简单回路
- 百度地图周边检索,和中心点位置计算和拖动计算
- 第十四周-交换排序之快速排序(以中间位置的元素为标准)
- Centos 6.5上安装Mysql过程
- boundingRectWithSize
- HDOJ 2095 find your present (2)
- java程序员的加冕之路~
- android studio java.exe finished with non-zero exit value 2 解决思路与方法
- mysql-5.7.9-winx64解压安装
- 接入google play service 的debug模式测试时候弹出选择谷歌账户后回调失败的原因【三个常见小坑】
- android 仿qq微信退出进入数据不用重新加载
- 第16周项目1-选择排序之直接选择排序(5)
- 图书馆界几个重要技术会议