HOPStrings 0.1.1

HOPStrings 0.1.1

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2015年5月

Chris Trott 维护。



HOPStrings 是一个用于创建 NSAttributedStrings 的小型实验性库。这灵感来源于 James Paolantonio 在 iOSoho 的演讲,他谈到了自己的库 JPAttributedString

这个库尚未在生产环境中得到广泛应用,因此被认为是预发布版本。API可能会有所变化。

欢迎 API 灵感和拉取请求。

Screenshot

使用

类概述

  • HOPAttributer - 构建属性字符串的主要接口。不可变。可链式。
  • HOPStringAttributes - 属性存储以及 NSAttributedString 字典属性键和 NSParagraphStyle 属性之间的仲裁。可变。

概述

  1. 使用类方法创建一个 HOPAttributer
  2. 链式使用 HOPAttributer 实例方法以追加字符串。所有实例方法都返回一个新的不可变 HOPAttributer 实例。
  3. 在任何一个 HOPAttributer 实例上调用 -attributedString 来卖出一个 NSAttributedString

1. 创建一个 HOPAttributer

/// You'll probably use these the most.
+ (instancetype)attributerWithDefaultAttributesBlock:(void(^)(HOPStringAttributes *attr))attributesBlock;
+ (instancetype)attributer;

/// You can use these too though.
+ (instancetype)attributerWithDefaultAttributes:(HOPStringAttributes *)attributes;
+ (instancetype)attributerWithString:(NSString *)string;
+ (instancetype)attributerWithString:(NSString *)string 
              defaultAttributesBlock:(void(^)(HOPStringAttributes *attr))attributesBlock;

HOPAttributer默认属性 的概念。默认属性将被应用到底链中所有追加的字符串上,但有例外(也可以被覆盖:参见 emptyAttributesBlock)。

2. 链式使用 HOPAttributer 实例

/// Appending starting with default attributes
- (instancetype)appendString:(NSString *)string;
- (instancetype)appendString:(NSString *)string 
             attributesBlock:(void (^)(HOPStringAttributes *attr))attributesBlock;

/// Appending without using default attributes
- (instancetype)appendString:(NSString *)string 
        emptyAttributesBlock:(void(^)(HOPStringAttributes *attr))attributesBlock;

使用上述方法向 attributer 追加字符串。

HOPAttributer 使用构建者模式。它接收一个带有一个可变 HOPStringAttributes 对象的块,该对象可以在返回给调用者并应用于字符串之前进行配置。

[[HOPAttributer attributer]
    appendString:@"Test string"
        attributesBlock:^(HOPStringAttributes *attr) {
            // These attributes will be applied to "Test String".
            attr.font = [UIFont systemFontOfSize:10];
            attr.foregroundColor = [UIColor blueColor];
        }];

appendString:attributesBlock: 将传递一个带有默认属性的 HOPStringAttributes 实例。如果你想获得一个全新的 HOPStringAttributes 实例,可以改用 appendString:emptyAttributesBlock: 变体。

3. 获取输出

当您完成字符串追加后,只需在 HOPAttributer 实例上调用 attributedString 以提供 NSAttributedString。可能您可以将 attributedText 设置在 UILabel 上?

更多示例

self.label.attributedText =
    [[[[HOPAttributer
        attributerWithDefaultAttributesBlock:^(HOPStringAttributes *attr) {
            attr.font = [UIFont systemFontOfSize:10];
            attr.foregroundColor = [UIColor blueColor];
        }]
        appendString:@"This string will be small and blue.\n"]
        appendString:@"This string will be too.\n"]
        attributedString];

self.label.attributedText =
    [[[[HOPAttributer
        attributerWithDefaultAttributesBlock:^(HOPStringAttributes *attr) {
            attr.font = [UIFont systemFontOfSize:10];
            attr.foregroundColor = [UIColor blueColor];
        }]
        appendString:@"This string will be small and blue.\n"]
        appendString:@"This string will be large and blue (because of defaults).\n"
            attributesBlock:^(HOPStringAttributes *attr) {
                attr.font = [UIFont systemFontOfSize:36];
            }]
        attributedString];

self.label.attributedText =
    [[[[HOPAttributer
        attributerWithDefaultAttributesBlock:^(HOPStringAttributes *attr) {
            attr.font = [UIFont systemFontOfSize:10];
            attr.foregroundColor = [UIColor blueColor];
        }]
        appendString:@"This string will be small and blue.\n"]
        appendString:@"This string will be large and black (because of `empty`).\n"
            emptyAttributesBlock:^(HOPStringAttributes *attr) {
                attr.font = [UIFont systemFontOfSize:36];
            }]
        attributedString];

示例项目

要运行示例项目,请克隆仓库,然后从示例目录中首次运行 pod install

要求

HOPStrings 不依赖于 UIKit 和 Foundation 之外的外部依赖。它使用了 iOS 7 中引入的属性字符串属性。

安装

HOPStrings 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile:

pod "HOPStrings"

作者

Chris Trott, [email protected]

额外感谢

HOPStringAttributesJPStringAttribute 借鉴了很多。

许可

HOPStrings 可在 MIT 许可证下使用。有关更多信息,请查看 LICENSE 文件。