当前位置: 首页 > news >正文

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!" 的富文本字符串。

http://www.lryc.cn/news/118833.html

相关文章:

  • 后端开发5.Redis的搭建
  • 推特群推王构建你的流量池
  • 【从零学习python 】12.Python字符串操作与应用
  • MongoDB创建用户 、数据库、索引等基础操作
  • Docker容器监控(Cadvisor +Prometheus+Grafana)
  • 家电用PCM板:市场现状研究分析与发展前景预测
  • 详解lambda表达式(一):表达式定义与异常处理
  • UE5、CesiumForUnreal接入WMTS格式地图瓦片,如ArcGIS、Mapbox、天地图
  • AI模型公司如何定位 ?
  • C#,OpenCV开发指南(01)
  • windows永久关闭更新
  • python类型转换笔记.python运算符笔记
  • 【CSS】背景图定位问题适配不同机型
  • 20 个实例玩转 Java 8 Stream
  • 局部变量数组和malloc申请的指针使用区别和注意事项
  • Springboot2.5.6整合Elasticsearch7.12.1完整示例
  • 全网超全,接口自动化测试-动态数据生成/替换数据(实战应用)
  • CRUD操作-select
  • SD-WAN网络加速及应用场景分析
  • python机器学习(六)决策树(上) 构造树、信息熵的分类和度量、信息增益、CART算法、剪枝
  • eNSP:ospf和mgre的配置
  • 培训报名小程序-订阅消息发送
  • 资深测试员才知道的五个行业秘密
  • Ozone命令行接口详解
  • 机器学习笔记 - 基于C++的​​深度学习 二、实现卷积运算
  • python pandas 获取Excel文件下所有的sheet名称,表格数据
  • gateway做token校验
  • C#学习记录-线程
  • Spring Boot 启动注解分析
  • React Native数据存储