iOS开发笔记之NSMutableAttributeString富文本

来源:互联网 发布:禁用windows defender 编辑:程序博客网 时间:2024/05/29 07:33

在日常的开发中有时候会用到富文本,并且自己在很早就想记录下有关NSMutableAttributeString的用法,今天来将这个想法付诸于实现.

NSMutableAttributeStringNSAttributedString的子类,它的创建方法有很多这里只说一种initWithString:(NSString *)str也是最简单的一种.

NSMutableAttributeString有很多属性我们可以去设置:

NSBackgroundColorAttributeName:设置字体的背景颜色NSFontAttributeName:设置字体的样式NSForegroundColorAttributeName:设置字体的颜色NSStrokeColorAttributeName:设置字体空心颜色(这里设置的是相当于外框的颜色,内部是空心)NSStrokeWidthAttributeName:设置字体空心的距离NSKernAttributeName:设置字体之间的间距NSObliquenessAttributeName:设置字体的倾斜度NSExpansionAttributeName:设置字体的拉伸压缩NSBaselineOffsetAttributeName:设置字体的基线偏移量NSShadowAttributeName:设置字体的阴影NSUnderlineStyleAttributeName:设置字体下划线的样式NSUnderlineColorAttributeName:设置字体下划线的颜色NSStrikethroughStyleAttributeName:设置字体删除线的样式NSStrikethroughColorAttributeName:设置字体删除线的颜色

以上都是大多数我们会用到的属性,设置这些属性可以像这样来设置:

[xxx addAttribute:NSForegroundColorAttributeName                      value:[UIColor redColor]                      range:(NSRangeMake(location,length))];

也可用NSDictionary去设置的,所以上面给出的都可以作为Dictionary的key值,通过对应的key值可以设置不同的value:

NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor redColor]};[xx addAttributes:dic range:NSRangeMake(location, length)];

(之前一直记不住range中的location与length之间的关系,这里特地备注下NSRangeMake(location:从0开始计数, length:从1开始计数))

我对于这些方法做了一个简单封装,现在只需要设置value值以及range就OK了,比较方便也比较方便理解

/** 设置字体背景颜色 @param color 背景颜色值 @param range 区间(NSMakeRange(从0开始的数,从1开始数)) @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringBackgroundColor:(UIColor *)color                                                  range:(NSRange)range;/** 设置字体类型 @param font 字体对象 @param range 区间 @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringFont:(UIFont *)font range:(NSRange)range;/** 设置字体颜色 @param color 字体颜色值 @param range 区间 @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringColor:(UIColor *)color                                            range:(NSRange)range;/** 设置字体空心 @param color 字体空心颜色值 @param width 字体空心间距 @param range 区间 @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringStrokeColor:(UIColor *)color                                              width:(CGFloat)width                                              range:(NSRange)range;/** 设置字体间的间距 @param space 距离 @param range 区间 @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringSpace:(CGFloat)space range:(NSRange)range;/** 设置字体的倾斜 @param gradient 倾斜度(正:右,负:左) @param range 区间 @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringGradient:(CGFloat)gradient                                           range:(NSRange)range;/** 设置字体的拉伸,压缩 @param expansion 拉伸/压缩值(正:横向拉伸,负:横向压缩) @param range 区间 @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringExpansion:(CGFloat)expansion                                            range:(NSRange)range;/** 设置字体的基线偏移 @param offset 偏移值(正:上,负:下) @param range 区间 @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringBaselineOffset:(CGFloat)offset                                                 range:(NSRange)range;/** 设置字体的阴影 @param offset 阴影偏移量 @param radius 模糊半径 @param color 阴影半径 @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringShadowOffset:(CGSize)offset                                        shadowRadius:(CGFloat)radius                                               color:(UIColor *)color                                               range:(NSRange)range;/** 设置字体的下划线 @param style 下划线类型(NSUnderlineStyle枚举) @param color 下划线颜色 @param range 区间 @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringUnderline:(NSUnderlineStyle)style                                            color:(UIColor *)color                                            range:(NSRange)range;/** 设置字体删除线 @param style 删除线类型(NSUnderlineStyle枚举) @param color 删除线颜色 @param range 区间 @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringDeleteline:(NSUnderlineStyle)style                                             color:(UIColor *)color                                             range:(NSRange)range;/** 在字体中加图片 @param imageName 需要加的图片的名字 @param bounds 图片位置大小 @param index 图片加载的位置 @return NSMutableAttributedString */- (NSMutableAttributedString *)setStringWithImage:(NSString *)imageName                                           bounds:(CGRect)bounds                                            index:(NSInteger)index;
原创粉丝点击