现在APP的开发中,UI为了是页面更加漂亮,文字更加饱满,经常会出现使用到富文本的场景。在实际开发中,富文本(NSAttributedString)在写代码时非常麻烦。针对不同范围内的文字,添加不同的富文本属性,可能只是短短几行文字,却要通过几十行甚至上百行代码去实现。
比如这样
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"今天天气不错呀"];
[AttributedStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:16.0]
range:NSMakeRange(2, 2)];
[AttributedStr addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(2, 2)];
testLabel.attributedText = AttributedStr;
或这样
NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:
[UIFontsystemFontOfSize:15.0],NSFontAttributeName,
[UIColorredColor],NSForegroundColorAttributeName,
NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀" attributes:attributeDict];
为了应对这样的需求,模仿masonry,利用函数式编程和链式编程对富文本进行了封装。
封装后的代码:
这样看起来是不是顺眼多了~
简单抽取部分内部实现代码,供大家参考,如有问题请指出。
首先需要一个NSMutableDictionary的category,利用链式编程去实现富文本属性的赋值。
@interface NSMutableDictionary (Attributes)
-(NSMutableDictionary *(^)(CGFloat))Font;
-(NSMutableDictionary *(^)(UIColor *))Color;
@end
@implementation NSMutableDictionary (Attributes)
-(NSMutableDictionary *(^)(CGFloat))Font {
return ^id(CGFloat font) {
[self setValue:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName];
return self;
};
}
-(NSMutableDictionary *(^)(UIColor *))Color {
return ^id(UIColor * color) {
[self setValue:color forKey:NSForegroundColorAttributeName];
return self;
};
}
@end
这样就可以给attribute用链式赋值
attributes.Font(24).Color([UIColor yellowColor]);
然后再给NSMutableAttributedString添加一个category,利用函数式编程去创建 NSMutableAttributedString对象,或者为NSMutableAttributedString对象添加文字和attribute。
@interface NSMutableAttributedString (category)
+(NSMutableAttributedString *)makeAttributeString:(NSString *)string Attribute:(void(^)(NSMutableDictionary * attributes))block;
-(NSMutableAttributedString *)makeAttributeStringAdd:(NSString *)string Attribute:(void(^)(NSMutableDictionary * attributes))block;
@end
@implementation NSMutableAttributedString (category)
+(NSMutableAttributedString *)makeAttributeString:(NSString *)string Attribute:(void(^)(NSMutableDictionary * attributes))block{
NSMutableDictionary * attributes = [NSMutableDictionary dictionary];
block(attributes);
NSMutableAttributedString * as = [[NSMutableAttributedString alloc] initWithString:string attributes:attributes];
return as;
}
-(NSMutableAttributedString *)makeAttributeStringAdd:(NSString *)string Attribute:(void(^)(NSMutableDictionary * attributes))block{
NSMutableDictionary * attributes = [NSMutableDictionary dictionary];
block(attributes);
NSMutableAttributedString * as = [[NSMutableAttributedString alloc] initWithString:string attributes:attributes];
[self appendAttributedString:as];
return self;
}
@end
NSMutableAttributedString * testAS = [NSMutableAttributedString makeAttributeString:@"直接创建" Attribute:^(NSMutableDictionary *attributes) {
attributes.Font(24).Color([UIColor yellowColor]);
}];
[testAS makeAttributeStringAdd:@"拼接新的文字" Attribute:^(NSMutableDictionary *attributes) {
attributes.Font(12).Color([UIColor redColor]);
}];
这里我只是抛砖引玉,一个简单的demo,具体该实现那些功能,还是需要根据大家的需求和实际场景去封装。