测试测试 | ✓ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最新发布 | 2017年10月 |
SwiftSwift 版本 | 4.0 |
由 James Paolantonio 维护。
用于创建、更改和使用 NSAttributedStrings 的 DSL
使用 CocoaPods 安装
pod 'JPAttributedString'
创建 NSAttributedStrings 时最大的问题是什么?
NSAttributedString *attributedString =
[[NSAttributedString alloc] initWithString:@"James"
attributes:@{
NSFontAttributeName : [UIFont boldSystemFontOfSize:22.f],
NSForegroundColorAttributeName : [UIColor blackColor],
}];
self.label.attributedText = attributedString;
代码相当冗长,而且不太可重用。让我们尝试提取字典。
JPStringAttribute *attribute = [[JPStringAttribute alloc] init];
attribute.font = [UIFont boldSystemFontOfSize:22.f];
attribute.foregroundColor = [UIColor blackColor];
self.label.attributedText = [[NSAttributedString alloc] initWithString:@"James"
attributes:attribute.attributedDictionary];
NSParagraphStyle
是 NSAttributedString
的一个属性,允许进一步配置段落或标尺属性。使用 JPStringAttribute
,修改段落样式更简单。
NSMutableParagraphStyle *mutableStyle = [[NSMutableParagraphStyle alloc] init];
mutableStyle.paragraphSpacing = 8.f;
mutableStyle.lineBreakMode = NSLineBreakByTruncatingMiddle;
NSAttributedString *attributedString =
[[NSAttributedString alloc] initWithString:@"James"
attributes:@{
NSFontAttributeName : [UIFont boldSystemFontOfSize:22.f],
NSForegroundColorAttributeName : [UIColor blackColor],
NSParagraphStyleAttributeName : mutableStyle,
}];
self.label.attributedText = attributedString;
与以下相比
JPStringAttribute *attribute = [[JPStringAttribute alloc] init];
attribute.font = [UIFont boldSystemFontOfSize:22.f];
attribute.foregroundColor = [UIColor blackColor];
attribute.paragraphSpacing = 8.f;
attribute.lineBreakMode = NSLineBreakByTruncatingMiddle;
self.label.attributedText = [[NSAttributedString alloc] initWithString:@"James"
attributes:attribute.attributedDictionary];
}
您是否曾经不得不添加一堆 NSAttributedStrings
,然后将它们分配给 UILabel
?
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
[string appendAttributedString:
[[NSAttributedString alloc] initWithString:@"James"
attributes:@{
NSFontAttributeName : [UIFont boldSystemFont:22.f],
NSForegroundColorAttributeName : [UIColor blackColor]
}]];
[string appendAttributedString:
[[NSAttributedString alloc] initWithString:@"\nEngineer"
attributes:@{
NSFontAttributeName : [UIFont boldSystemFont:16.f],
NSForegroundColorAttributeName : [UIColor darkGrayColor]
}]];
[string appendAttributedString:
[[NSAttributedString alloc] initWithString:@"New York City"
attributes:@{
NSFontAttributeName : [UIFont boldSystemFont:12.f],
NSForegroundColorAttributeName : [UIColor grayColor]
}]];
self.label.text = string;
这更好吗?
[self.label jp_appendString:@"James" attributesBlock:^(JPStringAttribute *make) {
make.font = [UIFont boldSystemFontOfSize:22.f];
make.foregroundColor = [UIColor blackColor];
}];
[self.label jp_appendString:@"\nEngineer" attributesBlock:^(JPStringAttribute *make) {
make.font = [UIFont boldSystemFontOfSize:16.f];
make.foregroundColor = [UIColor darkGrayColor];
}];
[self.label jp_appendString:@"\nNewYork" attributesBlock:^(JPStringAttribute *make) {
make.font = [UIFont boldSystemFontOfSize:12.f];
make.foregroundColor = [UIColor grayColor];
}];
项目包含一个示例。要安装,
cd Example/
pod install
open JPAttributedString.xcworkspace/
提交给我拉取请求!
James Paolantonio - @jpaolantonio
让我知道您是否在使用此工具或想进行任何修改!:)