EMString 代表 Easy Markup String。我曾犹豫是否要将其称为 SONSAString : 痛苦地厌倦了 NSAttributedString...
尽管 Apple 尝试让处理自定义字体和文本样式变得更加容易,但它们还不是像应有的那样方便。通常,如果您需要显示具有不同样式的文本,比如 "这文本是加粗但然后utsch斜体。",您会使用 NSAttributedString
和其 API。
我个人不喜欢!它会用大量样板代码簇状组织我的类,以查找范围并应用样式等... 或者更糟糕的是,您会使用两个标签,但还要努力使它们的框架与动态文本对齐...
这应该是一个更简单的任务。就像现在我正在编写这个 README 文件。我只是自然地写了
<strong>This text is bold</strong> but then <em>italic.</em>
这就是 EMString 的初衷。使用我们所有人熟悉的有效 HTML 标记 系统,并在一行代码中获得具有风格化的 iOS 字符串,并按照您的预期行为。
除此之外,还有什么能展示 EMString 的强大之处呢?让我们通过一个没有与有 EMString 的代码片段来介绍一下。以我们 "这文本是加粗但然后utsch斜体。" 的例子为例,并用 Objective-c 编写它。
没有 EMString
NSString *myString = @"This text is bold but then italic.";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:myString];
[attributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:[myString rangeOfString:@"This text is bold"]];
[attributedString addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:16] range:[myString rangeOfString:@"italic."]];
myLabel.attributedText = attributedString;
使用 EMString
myLabel.attributedText = @"<strong>This text is bold</strong> but then <em>italic.</em>".attributedString;
我还需要说什么吗? :)
相当简单,只需下载存档并将 EMString
文件夹添加到您自己的项目中。不要忘记在哪里需要它导入头文件
#import "NSString+EMAdditions.h"
如果我们谈论文本样式,我们就是在谈论 定制 和 灵活性。 EMString 正确地提供了这一点。首先,它包括一组默认的标记:
这些标记可以很容易地定制以适应您的需求。基本上,您可以为此中的每个预设标记设置以下属性:
要应用不同的样式,必须修改 EMStringStylingConfiguration
的属性
例如,在接下来的代码中,我将向您展示如何更改 h1 和 strong 标记的行为
// Change h1 font
[EMStringStylingConfiguration sharedInstance].h1Font = [UIFont fontWithName:@"OpenSans" size:26];
// Change h1 color
[EMStringStylingConfiguration sharedInstance].h1Color = [UIColor blueColor];
// Change strong font
[EMStringStylingConfiguration sharedInstance].strongFont = [UIFont fontWithName:@"OpenSans-Bold" size:fontSize];
另外,如果您为您的整个应用设置了默认颜色和/或字体,您可以使用这两个方便的属性。
@property (strong, nonatomic) UIFont *defaultFont;
@property (strong, nonatomic) UIColor *defaultColor;
其次,如果你需要自定义标记?嗯,EMString 让这件事再次变得简单。使用方便的 EMStylingClass
对象来创建一个自定义类。
EMStylingClass *aStylingClass = [[EMStylingClass alloc] initWithMarkup:@"<customMarkup>"];
aStylingClass.color = [UIColor blueColor];
aStylingClass.font = [UIFont fontWithName:@"OpenSans" size:16];
// Or you can use the attributes property for full control
// aStylingClass.attributes = @{};
[[EMStringStylingConfiguration sharedInstance] addNewStylingClass:aStylingClass];
由于 EMStringStylingConfiguration.h
是一个单例,你不必每次都这样做。只需一次,设置你的样式需求。之后,所有调用 .attributedString 都将提供正确样式的 NSAttributedString。这将帮助你保持设计的一致性。
想了解你能做所有这些事情,我建议你查看头文件 EMStringStylingConfiguration.h
和 EMStylingClass.h
。
以下是我使用 EMString 时发现的一些优势:
NSAttributedString
API 的抽象最后一点可能需要进一步解释。当在项目中使用 NSAttributedString
时,我遇到了这个问题:有一个标签显示了一个包含加粗和轻字体的字符串。基本上,文本将由地名(加粗)和其城市位置(轻字体)组成。所以我就用了字符串范围来应用样式。但当餐厅的名字含有城市名时,情况变得复杂。比如 Café de Paris, Paris.
中的第一个“Paris”将是浅色而不是加粗,第二个将不会加样式。这个问题可能会由任何动态/翻译的文本引起。我知道我可以通过拼接 attributedString 来修复这个问题...但这会让我的代码变得更长、更难看。
// Simply like that with EMString
[NSString stringWithFormat:@"<strong>%@,</strong><light>%@</light>", place.name, place.city].attributedString
EMString 按照MIT许可证提供。有关更多信息,请参阅 LICENSE 文件。
请尽可能地为这个仓库做出贡献,任何建议都受欢迎!=)。我希望你会喜欢它。