NSAttributedString API 远远不够完美,这在当今并不神秘。它基于 NSDictionary,看起来很丑陋,反面向对象设计,且难以维护...
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test attributed string" attributes:@{NSForegroundColorAttributeName: [UIColor greenColor], NSFontAttributeName: [fnt fontWithSize:20]}];
[attributedString addAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]
, NSFontAttributeName: [fnt2 fontWithSize:10]
, NSLigatureAttributeName: @2
, NSBaselineOffsetAttributeName: @1}
range:NSMakeRange(3, 5)];
[attributedString addAttributes:@{NSForegroundColorAttributeName: [UIColor blueColor]
, NSFontAttributeName: [fnt2 fontWithSize:30]}
range:NSMakeRange(6, 9)];
[attributedString addAttributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle)
, NSStrikethroughColorAttributeName: [UIColor redColor]
, NSBackgroundColorAttributeName: [UIColor yellowColor]}
range:NSMakeRange(7, 4)];
_attributedTextView2.attributedText = attributedString;
一些开发者真的感到非常绝望,因此他们编写了如这样的工具... 这再次证明 NSAttributedString API 离完美还远。
我爱 masonry。我爱它的清晰性和语法。如果你在你的项目中使用了自动布局并且使用 NSLayoutConstraint API 来编写约束,你应该 definitely 查看Masonry。简洁的语法让代码更容易阅读、维护和修改。
基于 masonry 语法,我决定创建一个类似框架,这将减轻创建 NSAttributedString 的一些痛苦。
NSMutableAttributedString *attributedString = [@"Test attributed string" bos_makeString:^(BOStringMaker *make) {
make.foregroundColor([UIColor greenColor]);
make.font([fnt fontWithSize:20]);
make.with.range(NSMakeRange(3, 5), ^{
make.foregroundColor([UIColor redColor]);
make.font([fnt2 fontWithSize:10]);
make.ligature(@2);
make.baselineOffset(@1);
});
make.with.range(NSMakeRange(6, 9), ^{
make.foregroundColor([UIColor blueColor]);
make.font([fnt2 fontWithSize:30]);
});
make.with.range(NSMakeRange(7, 4), ^{
make.strikethroughStyle(@(NSUnderlineStyleSingle));
make.strikethroughColor([UIColor redColor]);
make.backgroundColor([UIColor yellowColor]);
});
}];
制作字符串时,您可以使用示例中所示的基于块的语法指定属性的标记范围
make.with.range(NSMakeRange(6, 9), ^{
make.foregroundColor([UIColor blueColor]);
make.font([fnt2 fontWithSize:30]);
});
或为特定属性设置范围(`with` 是可选的语义填充符)
make.foregroundColor([UIColor blueColor]).with.range(NSRange(6, 9));
make.font([fnt2 fontWithSize:30]).range(NSRange(6, 9));
如果您未指定范围,则将使用字符串的完整范围。
BOString 支持哪些属性?它支持很多
font;
paragraphStyle;
foregroundColor;
backgroundColor;
ligature;
kern;
strikethroughStyle;
underlineStyle;
strokeColor;
strokeWidth;
shadow;
textEffect; // iOS only
attachment;
link;
baselineOffset;
underlineColor;
strikethroughColor;
obliqueness;
expansion;
writingDirection;
verticalGlyphForm;
superscript; // OS X only
cursor; // OS X only
toolTip; // OS X only
characterShape; // OS X only
glyphInfo; // OS X only
markedClauseSegment; // OS X only
textAlternatives; // OS X only
的确,有许多 CoreText 属性没有被定义为方法。例如,`kCTLanguageAttributeName`,`kCTCharacterShapeAttributeName`,`kCTBaselineClassAttributeName`,等等。在这种情况下,您可以使用 `attribute` 方法
make.attribute(kCTLanguageAttributeName, @"jp");
一些子串属性设置器。为一一设置找到的第一个子串的属性
NSAttributedString *result = [@"This is a string" bos_makeString:^(BOStringMaker *make) {
make.first.substring(@"is", ^{
make.foregroundColor([UIColor greenColor]);
});
}];
或突出显示每个子串
NSAttributedString *result = [@"This is a string" bos_makeString:^(BOStringMaker *make) {
make.each.substring(@"is", ^{
make.foregroundColor([UIColor greenColor]);
});
}];
您还可以使用正则表达式应用属性
NSAttributedString *result = [@"This is a string" bos_makeString:^(BOStringMaker *make) {
make.each.regexpMatch(@"\\ws", NSRegularExpressionCaseInsensitive, ^{
make.foregroundColor([UIColor greenColor]);
});
}];
或者带分组匹配的正则表达式
NSAttributedString *result = [@"This is a string" bos_makeString:^(BOStringMaker *make) {
make.first.regexpGroup(@"[^h](i\\w)\\s(\\w*)", NSRegularExpressionCaseInsensitive, ^{
make.foregroundColor([UIColor greenColor]);
});
}];
为了避免与其他框架发生冲突,用于分类方法的 bos_
前缀被使用。然而,如果您在导入 BOString.h
之前在您的 `prefix.pch` 文件中添加 #define BOS_SHORTHAND
,则可以使用没有该前缀的缩写方法。
HTML格式的文档可在此处或cocoadocs.org上找到。
应在iOS 4.3+和OS X 10.5+上运行良好(除非是几个属性),但我还没有机会对其进行测试。
最简单的方法是使用CocoaPods
在您的Podfile中
pod 'BOString'
并在需要制作类似老板式的字符串的文件中导入它
#import "Bostring.h"
请随意将pull请求提交到单独的分支。请不要向master
提交pull请求。
BOString是以MIT许可证发布的。