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

北京互联网建站网站网络平台建站

北京互联网建站网站,网络平台建站,邯郸seo,南昌住房建设局网站一、概念 链式编程风格是一种将多个函数调用连接起来,形成一条函数调用链的编程风格。这种风格的代码可以通过返回 self 或某个适当的对象来实现。 1.优点 代码简洁、连贯、易于阅读。可以将一个方法的输出直接作为下一个方法的输入,降低中间变量的使…

一、概念

链式编程风格是一种将多个函数调用连接起来,形成一条函数调用链的编程风格。这种风格的代码可以通过返回 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.mmbaike.com/news/41773.html

相关文章:

  • 重庆网站建设服务搜索引擎的三个技巧
  • 网站的友情连接怎么做seo标题优化关键词怎么选
  • 珠海专业网站制作公司看b站二十四小时直播间
  • 搜房网站建设分析好搜搜索引擎
  • 网站设计说明东莞企业网站排名优化
  • 南阳哪有做网站公司搜索引擎优化seo什么意思
  • 上海网站建设网页设计河南靠谱seo地址
  • 永嘉网站制作哪家好今日军事新闻最新消息
  • 网站程序授权怎么做市场调研方法有哪些
  • 建网站挣钱靠谱吗付费恶意点击软件
  • 软件开发的六大步骤系统优化软件有哪些
  • 网站建设 开发票seo和点击付费的区别
  • 明薇通网站建设哪家好网站建设关键词排名
  • 网站建设公司权威排名seo课程在哪培训好
  • 龙江人社使用教程seo快速排名软件网站
  • 学做网站要学什么软件seo优化检测
  • 深圳装修公司电话seo最新技巧
  • 怎么做自己的微信网站希爱力5mg效果真实经历
  • 怎么申请个人网站定制开发公司
  • 神箭手wordpress免费吗智能优化网站
  • 网站制作手机免费正规的接单平台
  • 网站构建的工作真实有效的优化排名
  • 怎么看一个网站用什么平台做的自动连点器
  • 企业网站需求分析培训机构不退钱最怕什么举报
  • wordpress文章同步国外博客广州网站优化平台
  • 云匠网骗设计师入驻费曹操博客seo
  • 用Axure做的网站原型百度云知名品牌营销策略
  • 我想要个网站培训学校管理系统
  • 如何查找昆明做网站服务的公司app营销策划方案
  • 可以做动漫的网站有哪些网络营销的概念及特征