开发过程中,经常会遇到动态计算行高的问题,

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullableNSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);

是苹果推荐的计算方法,显然会遇到段落格式问题,例如行间距、缩进等格式设置需求,attributes传进来的字典中,包含我们设置的字体及格式,其中NSParagraphStyleAttributeName是设置段落风格,NSFontAttributeName是设置字体。

ok,具体来看一下NSParagraphStyleAttributeName的功能。

//   NSParagraphStyleAttributeName 段落的风格(设置首行,行间距,对齐方式什么的)看自己需要什么属性,写什么
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = ;// 字体的行间距
paragraphStyle.firstLineHeadIndent = 20.0f;//首行缩进
paragraphStyle.alignment = NSTextAlignmentJustified;//(两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然)
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
paragraphStyle.headIndent = ;//整体缩进(首行除外)
paragraphStyle.tailIndent = ;//
paragraphStyle.minimumLineHeight = ;//最低行高
paragraphStyle.maximumLineHeight = ;//最大行高
paragraphStyle.paragraphSpacing = ;//段与段之间的间距
paragraphStyle.paragraphSpacingBefore = 22.0f;//段首行空白空间/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */
paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//从左到右的书写方向(一共➡️三种)
paragraphStyle.lineHeightMultiple = ;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
paragraphStyle.hyphenationFactor = ;//连字属性 在iOS,唯一支持的值分别为0和1

好了,现在就可以很轻松的计算某一段落高度,例如:

_descAtt = [[NSMutableAttributedString alloc] initWithString:_model.desc];
UIFont *descFont = [UIFont PingFangSC_Regular_WithSize:]; NSMutableParagraphStyle *descStyle = [[NSMutableParagraphStyle alloc]init];
[descStyle setLineSpacing:];//行间距 CGSize descSize = [_model.desc boundingRectWithSize:CGSizeMake(w, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:descFont,
NSParagraphStyleAttributeName :descStyle}
context:nil].size;

另外,再介绍几个富文本处理的属性:

// NSFontAttributeName                设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
// NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色
// NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
// NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
// NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
// NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数)
// NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色
// NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
// NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色
// NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
// NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象
// NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象
// NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
// NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
// NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
// NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
// NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写
// NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
// NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址
// NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
// NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象

参考:http://blog.csdn.net/u013346305/article/details/50896308

NSMutableAttributedString的更多相关文章

  1. iOS - NSMutableAttributedString富文本的实现

    NSMutableAttributedString继承于NSAttributedString(带属性的字符串)能够简单快速实现富文本的效果;不多说直接上效果图和代码,通俗易懂: (一)效果图: (二) ...

  2. iOS中富文本NSMutableAttributedString的用法

    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithString:@"我是富文 ...

  3. NSMutableAttributedString(转)

    NSMutableAttributedString计算高度的问题 _label_page2_1 = [[UILabel alloc] init]; _label_page2_1.numberOfLin ...

  4. NSMutableAttributedString常用代码

    NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNam ...

  5. NSMutableAttributedString 的使用

    NSMutableAttributedString *msAttriStr = [[NSMutableAttributedString alloc] initWithString:[NSString ...

  6. NSMutableAttributedString的使用

    1.设置字符串中数字字符串的颜色: // 设置字符串中数字的颜色 - (void)setTextColor:(UILabel *)label FontNumber:(id)font AndRange: ...

  7. objective-c NSMutableAttributedString

    NSMutableAttributedString 是一个很强悍的富文本处理字符串,可以方便的实现一个字符串中某个字符的样式处理.我把我下面代码实现的功能步骤说一下:首先拼接两个字符串,然后给前前半部 ...

  8. iOS 学习 - 12.NSMutableAttributedString 计算高度

    计算 NSMutableAttributedString 高度,必须要有两个属性 -(void)test{ UILabel *label = [[UILabel alloc]initWithFrame ...

  9. 新浪微博客户端(35)-使用NSMutableAttributedString实现多行文本的效果

    DJComposeViewController.m import "DJComposeViewController.h" #import "DJAccountTool.h ...

  10. NSMutableAttributedString可变属性字符串的用法

    适用于:当你想对一个字符串中的某几个字符更改颜色,字体... NSString *string = @"今日营养配餐提供热量1800千卡,需要饮食之外额外补充钙10mg,铁20mg,锌9.5 ...

随机推荐

  1. maven 阿里云节点,速度快

    修改maven根目录下的conf文件夹中的setting.xml文件. <mirrors> <mirror> <id>alimaven</id> < ...

  2. python继承的实例

    class SchoolMember(object):#定义学校 member=0#默认成员为0个 amount=0#默认学费为0元 def __init__(self,name,age,sex):# ...

  3. Linux-socket 模型理解

    一.socket 一般来说socket有一个别名也叫做套接字. socket起源于Unix,都可以用"打 开open –> 读写write/read –> 关闭close&quo ...

  4. win8.1远程连接Redis数据库

    环境:redis安装在虚拟机Centos6.5系统上 通过java远程连接 问题一:报错 connected refused redis.conf 注释掉 #bind 127.0.0.1 问题二:还是 ...

  5. IOS开发-ObjC-Category的使用

    在IOS移动App开发中,经常会出现以下情况:定义好了一个类,但后来需求升级或改变之后需要对这个类增加功能,这样的话往往需要修改类的结构,这样就会导致不能预期的问题产生,所以Obj-C提供了一种叫做C ...

  6. CSS实现导航条Tab切换的三种方法

    前面的话   导航条Tab在页面中非常常见,本文说详细介绍CSS实现导航条Tab的三种方法 布局   根据上图所示,先规定几个定义,上图的模块整体叫做导航,由导航标题和导航内容组成.要实现上图所示的布 ...

  7. 在Eclipse中配置tomcat

    为了在Eclipse中进行struts2的测试,才发现自己机器上的Eclipse没有集成Tomcat,在网上找了半天,不是这个插件没有下载地址,就是那个有好多注意事项或者版本问题. 结果,自己到tom ...

  8. Python3基础 filter与lambda表达式配合 筛选出1-100之间的奇数

    镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...

  9. 基于arm开发板四个按键控制四个灯亮

    基于s5pv2410,cortex a8的四个按键每一个按键点了对应的灯 对于用汇编来编程的话不难,重点在于数据手册,电路图,管脚的看懂 直接上代码 .globl _start_start: ldr ...

  10. iOS 之 线程和进程

    进程是系统调度单位,拥有自己的资源 线程是CPU调度的基本单位 进程的同步机制: 原子操作.信号量机制.自旋锁.分布式系统