富文本封装

1,093 阅读5分钟
原文链接: blog.csdn.net

现在APP的开发中,UI为了是页面更加漂亮,文字更加饱满,经常会出现使用到富文本的场景。在实际开发中,富文本(NSAttributedString)在写代码时非常麻烦。针对不同范围内的文字,添加不同的富文本属性,可能只是短短几行文字,却要通过几十行甚至上百行代码去实现。

  1. 比如这样  
  2. NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"今天天气不错呀"];  
  3.   
  4.    [AttributedStr addAttribute:NSFontAttributeName  
  5.   
  6.                          value:[UIFont systemFontOfSize: 16.0]  
  7.   
  8.                          range:NSMakeRange( 22)];  
  9.   
  10.    [AttributedStr addAttribute:NSForegroundColorAttributeName  
  11.   
  12.                          value:[UIColor redColor]  
  13.   
  14.                          range:NSMakeRange( 22)];  
  15.   
  16.    testLabel.attributedText = AttributedStr;  
  17.   
  18. 或这样  
  19. NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:  
  20.                                     [UIFontsystemFontOfSize:1 5.0],NSFontAttributeName,  
  21.                                     [UIColorredColor],NSForegroundColorAttributeName,  
  22.                                    NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];  
  23. NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀" attributes:attributeDict];  
比如这样
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,利用链式编程去实现富文本属性的赋值。

  1. @interface NSMutableDictionary (Attributes)  
  2. -(NSMutableDictionary *(^)(CGFloat))Font;  
  3. -(NSMutableDictionary *(^)(UIColor *))Color;  
  4. @end  
  5.   
  6.   
  7. @implementation NSMutableDictionary (Attributes)  
  8.   
  9. -(NSMutableDictionary *(^)(CGFloat))Font {  
  10.     return ^id(CGFloat font) {  
  11.         [self setValue:[UIFont systemFontOfSize:font]  forKey:NSFontAttributeName];  
  12.         return self;  
  13.     };  
  14. }  
  15.   
  16.   
  17. -(NSMutableDictionary *(^)(UIColor *))Color {  
  18.     return ^id(UIColor * color) {  
  19.         [self setValue:color forKey:NSForegroundColorAttributeName];  
  20.         return self;  
  21.     };  
  22. }  
  23.   
  24. @end  
@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


  1. 这样就可以给attribute用链式赋值  
  2. attributes.Font(24).Color([UIColor  yellowColor]);  
这样就可以给attribute用链式赋值
attributes.Font(24).Color([UIColor yellowColor]);

然后再给NSMutableAttributedString添加一个category,利用函数式编程去创建NSMutableAttributedString对象,或者为NSMutableAttributedString对象添加文字和attribute。

  1. @interface NSMutableAttributedString (category)  
  2.   
  3. +(NSMutableAttributedString *)makeAttributeString:(NSString *)string Attribute:( void(^)(NSMutableDictionary * attributes))block;  
  4.   
  5. -(NSMutableAttributedString *)makeAttributeStringAdd:(NSString *)string Attribute:( void(^)(NSMutableDictionary * attributes))block;  
  6.   
  7. @end  
  8.   
  9.   
  10. @implementation NSMutableAttributedString (category)  
  11.   
  12. +(NSMutableAttributedString *)makeAttributeString:(NSString *)string Attribute:( void(^)(NSMutableDictionary * attributes))block{  
  13.     NSMutableDictionary * attributes = [NSMutableDictionary dictionary];  
  14.     block(attributes);  
  15.     NSMutableAttributedString * as = [[NSMutableAttributedString alloc] initWithString:string  attributes:attributes];  
  16.     return as;  
  17. }  
  18.   
  19. -(NSMutableAttributedString *)makeAttributeStringAdd:(NSString *)string Attribute:( void(^)(NSMutableDictionary * attributes))block{  
  20.     NSMutableDictionary * attributes = [NSMutableDictionary dictionary];  
  21.     block(attributes);  
  22.     NSMutableAttributedString * as = [[NSMutableAttributedString alloc] initWithString:string  attributes:attributes];  
  23.     [self appendAttributedString:as];  
  24.     return self;  
  25. }  
  26.   
  27. @end  
@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


使用效果:
  1. NSMutableAttributedString * testAS = [NSMutableAttributedString makeAttributeString:@"直接创建"  Attribute:^(NSMutableDictionary *attributes) {  
  2.         attributes.Font(24).Color([UIColor  yellowColor]);  
  3.     }];  
  4.       
  5.     [testAS makeAttributeStringAdd:@"拼接新的文字" Attribute:^(NSMutableDictionary *attributes) {  
  6.         attributes.Font(12).Color([UIColor  redColor]);  
  7.     }];  
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,具体该实现那些功能,还是需要根据大家的需求和实际场景去封装。