Cowboy Tech

NSAttributedString使用详解

NSAttributedString 的基本使用

处理简单的字符串

NSString *string = @"天道酬勤";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];

// 设置富文本样式
[attributedString addAttribute:NSForegroundColorAttributeName
                         value:[UIColor redColor]
                         range:NSMakeRange(0, 1)];

[attributedString addAttribute:NSFontAttributeName
                         value:[UIFont systemFontOfSize:24.f]
                         range:NSMakeRange(0, 2)];

UILabel *label       = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 30)];
label.attributedText = attributedString;
[self.view addSubview:label];

处理段落样式

NSString *string = @"天道酬勤天道酬勤天道酬勤天道酬勤天道酬勤天道酬勤天道酬勤天道酬勤\n天道酬勤真不错!";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];

// 设置富文本 - 段落样式
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing              = 10.f;
style.paragraphSpacing         = 20.f;

[attributedString addAttribute:NSParagraphStyleAttributeName
                         value:style
                         range:NSMakeRange(0, string.length)];

UILabel *label       = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 400)];

//设置段落样式的时候,numberOfLines必须为0
label.numberOfLines  = 0;
label.attributedText = attributedString;
[self.view addSubview:label];

NSAttributedString设置的作用域

设置富文本的时候,先设置的先显示,后设置的,如果与先设置的样式不一致,是不会覆盖的,富文本设置的效果具有先后顺序

NSAtrributedString的简易封装

ViewController.m

#import "ViewController.h"
#import "NSString+AttributeStyle.h"
#import "ForeGroundColorStyle.h"
#import "FontStyle.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

NSString *string = @"极客学院";

UILabel *label       = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 30)];
label.attributedText = \
    [string createAttributedStringWithStyles:\
       @[colorStyle([UIColor redColor],             NSMakeRange(0, 2)),
         fontStyle ([UIFont systemFontOfSize:20.f], NSMakeRange(1, 2))]];

[self.view addSubview:label];
}

AttributedStyle.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface AttributedStyle : NSObject

@property (nonatomic, strong) NSString  *attributeName;
@property (nonatomic, strong) id         value;
@property (nonatomic)         NSRange    range;

/**
 *  便利构造器
 *
 *  @param attributeName 属性名字
 *  @param value         设置的值
 *  @param range         取值范围
 *
 *  @return 实例对象
 */
+ (AttributedStyle *)attributeName:(NSString *)attributeName value:(id)value range:(NSRange)range;
@end

AttributedStyle.m

#import "AttributedStyle.h"

@implementation AttributedStyle

+ (AttributedStyle *)attributeName:(NSString *)attributeName value:(id)value range:(NSRange)range {

AttributedStyle *style = [[self class] new];

style.attributeName = attributeName;
style.value         = value;
style.range         = range;

return style;
}

@end

NSString + AttributeStyle.h

#import <Foundation/Foundation.h>
#import "AttributedStyle.h"

@interface NSString (AttributeStyle)

/**
 *  根据styles数组创建出富文本
 *
 *  @param styles AttributedStyle对象构成的数组
 *
 *  @return 富文本
 */
- (NSAttributedString *)createAttributedStringWithStyles:(NSArray *)styles;

@end

NSString + AttributeStyle.m

#import "NSString+AttributeStyle.h"

@implementation NSString (AttributeStyle)

- (NSAttributedString *)createAttributedStringWithStyles:(NSArray *)styles {
if (self.length <= 0) {
    return nil;
}

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];

for (int count = 0; count < styles.count; count++) {
    AttributedStyle *style = styles[count];

    [attributedString addAttribute:style.attributeName
                             value:style.value
                             range:style.range];
}


return attributedString;
}

@end

ForeGroundColorStyle.h

#import "AttributedStyle.h"

@interface ForeGroundColorStyle : AttributedStyle

+ (id)withColor:(UIColor *)color range:(NSRange)range;

@end

/**
 *  内联函数
 *
 *  @param color 颜色
 *  @param range 范围
 *
 *  @return 实例对象
 */
static inline AttributedStyle* colorStyle(UIColor *color, NSRange range) {
return [ForeGroundColorStyle withColor:color range:range];
}

ForeGroundColorStyle.m

#import "ForeGroundColorStyle.h"

@implementation ForeGroundColorStyle

+ (id)withColor:(UIColor *)color range:(NSRange)range {

id style = [super attributeName:NSForegroundColorAttributeName
                          value:color
                          range:range];

return style;
}

@end

FontStyle.h

#import "AttributedStyle.h"

@interface FontStyle : AttributedStyle

+ (id)withFont:(UIFont *)font range:(NSRange)range;

@end

/**
 *  内联函数
 *
 *  @param font  字体
 *  @param range 范围
 *
 *  @return 实例对象
 */
static inline AttributedStyle* fontStyle(UIFont *font, NSRange range) {
return [FontStyle withFont:font range:range];
}

FontStyle.m

#import "FontStyle.h"

@implementation FontStyle

+ (id)withFont:(UIFont *)font range:(NSRange)range {

id fontStyle = [super attributeName:NSFontAttributeName
                              value:font
                              range:range];

return fontStyle;
}

@end

使用开源代码GONMarkupPaser处理富文本 (略)

UITextKit简介(略)