iOS开发笔记之NSMutableAttributeString富文本

在日常的开发中有时候会用到富文本,并且自己在很早就想记录下有关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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值