利用函数式编程和链式编程封装富文本

现在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,具体该实现那些功能,还是需要根据大家的需求和实际场景去封装。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值