iOS使用NSMutableAttributedString实现富文本小结

NSAttributedString

NSAttributedString对象管理适用于字符串中单个字符或字符范围的字符串和关联的属性集(例如字体和字距)。NSAttributedString对象的默认字体是Helvetica 12点,可能与平台的默认系统字体不同。因此,您可能希望创建适用于您的应用程序的非默认属性的新字符串。您还可以使用NSParagraphStyle类及其子类NSMutableParagraphStyle来封装NSAttributedString类使用的段落或标尺属性。

实例化方法和使用方法

实例化方法

使用字符串初始化

- (instancetype)initWithString:(NSString *)str;

代码示例

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"测试数据"];

字典中存放一些属性名和属性值

- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs; 

代码示例

NSDictionary *attributedDict = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:16.0],
                                     NSForegroundColorAttributeName:[UIColor redColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick) }; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"测试数据" attributes:attributedDict]; 

使用NSAttributedString初始化,与NSMutableString,NSString类似

- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;

使用方法

为某一范围内的文字设置多个属性的方法

- (void)setAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range; 

//代码示例

NSString *string = @"测试数据";
NSDictionary *attributedDict = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor redColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick) }; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString setAttributes:attributedDict range:NSMakeRange(0, string.length)]; 

为某一范围内的文字添加某个属性的方法

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

//代码示例

NSString *string = @"测试数据";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, string.length)]; 

为某一范围内的文字添加多个属性的方法

- (void)addAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range; 

//代码示例

NSString *string = @"测试数据";
NSDictionary *attributedDict = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor redColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick) }; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)]; 

移除某个范围内的某个属性的方法

- (void)removeAttribute:(NSString *)name range:(NSRange)range;

//代码示例

 NSString *string = @"测试数据";
 NSDictionary *attributedDict = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor redColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick) }; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)]; [attributedString removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(0, string.length)]; 

属性及说明

key说明
NSFontAttributeName字体,value是UIFont对象
NSParagraphStyleAttributeName绘图的风格(居中,换行模式,间距等诸多风格),value是NSParagraphStyle对象
NSForegroundColorAttributeName文字颜色,value是UIColor对象
NSLigatureAttributeName字符连体,value是NSNumber
NSKernAttributeName字符间隔
NSStrikethroughStyleAttributeName删除线,value是NSNumber
NSUnderlineStyleAttributeName下划线,value是NSNumber
NSStrokeColorAttributeName描绘边颜色,value是UIColor
NSStrokeWidthAttributeName描边宽度,value是NSNumber
NSShadowAttributeName阴影,value是NSShadow对象
NSTextEffectAttributeName文字效果,value是NSString
NSAttachmentAttributeName附属,value是NSTextAttachment 对象
NSLinkAttributeName链接,value是NSURL or NSString
NSBaselineOffsetAttributeName基础偏移量,value是NSNumber对象
NSStrikethroughColorAttributeName删除线颜色,value是UIColor
NSObliquenessAttributeName字体倾斜
NSExpansionAttributeName字体扁平化
NSVerticalGlyphFormAttributeName垂直或者水平,value是 NSNumber,0表示水平,1垂直

富文本段落排版格式属性说明

属性说明
lineSpacing字体的行间距
firstLineHeadIndent首行缩进
alignment(两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然)
lineBreakMode结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
headIndent整体缩进(首行除外)
minimumLineHeight最低行高
maximumLineHeight最大行高
paragraphSpacing段与段之间的间距
paragraphSpacingBefore段首行空白空间
baseWritingDirection书写方向(一共三种)
hyphenationFactor连字属性 在iOS,唯一支持的值分别为0和1



作者:coder小鹏

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值