iOS链式编程风格 -- 富文本字符串

一、概念

        链式编程风格是一种将多个函数调用连接起来,形成一条函数调用链的编程风格。这种风格的代码可以通过返回 self 或某个适当的对象来实现。

1.优点

  1. 代码简洁、连贯、易于阅读。
  2. 可以将一个方法的输出直接作为下一个方法的输入,降低中间变量的使用。

2.缺点

  1. 链式调用过长可能会导致代码可读性降低。
  2. 由于错误可能出现在链的任何一环,所以调试可能会有所困难。

二、代码

        下面是一个使用链式编程风格构建的 NSMutableAttributedString 的例子,这个例子将展示如何将一系列的 NSAttributedString 配置操作链接在一起。

        首先,我们需要创建一个类 ChainableAttributedBuilder,它可以用于创建和配置 NSAttributedString:

1..h文件

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ChainableAttributedBuilder : NSObject

@property (nonatomic, strong, readonly) NSMutableAttributedString *mutableAttributedString;

- (ChainableAttributedBuilder *(^)(NSString *text))append;

- (ChainableAttributedBuilder *(^)(UIColor *color))textColor;

- (ChainableAttributedBuilder *(^)(UIFont *font))font;

- (ChainableAttributedBuilder *(^)(NSParagraphStyle *style))paragraphStyle;

@end

NS_ASSUME_NONNULL_END

2..m文件

#import "ChainableAttributedBuilder.h"

@implementation ChainableAttributedBuilder

- (instancetype)init {

    if (self = [super init]) {

        _mutableAttributedString = [[NSMutableAttributedString alloc] init];

    }

    return self;

}

- (ChainableAttributedBuilder *(^)(NSString *text))append {

    return ^(NSString *text) {

        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];

        [self.mutableAttributedString appendAttributedString:attributedString];

        return self;

    };

}

- (ChainableAttributedBuilder *(^)(UIColor *color))textColor {

    return ^(UIColor *color) {

        [self.mutableAttributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, self.mutableAttributedString.length)];

        return self;

    };

}

- (ChainableAttributedBuilder *(^)(UIFont *font))font {

    return ^(UIFont *font) {

        [self.mutableAttributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.mutableAttributedString.length)];

        return self;

    };

}

- (ChainableAttributedBuilder *(^)(NSParagraphStyle *style))paragraphStyle {

    return ^(NSParagraphStyle *style) {

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

        return self;

    };

}

@end

3.调用代码

    ChainableAttributedBuilder *builder = [[ChainableAttributedBuilder alloc] init];

    builder.append(@"Hello ").font([UIFont systemFontOfSize:16]).textColor([UIColor redColor]);

    builder.append(@"world!").font([UIFont systemFontOfSize:20]).textColor([UIColor blueColor]);

    NSAttributedString *attributedString = builder.mutableAttributedString;

    // 现在,attributedString 是一个带有不同样式的 "Hello world!" 的富文本字符串。

三、Demo链接

GitHub - danransmile/attributed_string: iOS链式编程风格 -- 富文本字符串

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值