TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Oct 2017 |
SwiftSwift Version | 4.0 |
Maintained by James Paolantonio.
A DSL for creating, changing, and using NSAttributedStrings
Install with CocoaPods:
pod 'JPAttributedString'
What's the biggest issue when creating NSAttributedStrings
?
NSAttributedString *attributedString =
[[NSAttributedString alloc] initWithString:@"James"
attributes:@{
NSFontAttributeName : [UIFont boldSystemFontOfSize:22.f],
NSForegroundColorAttributeName : [UIColor blackColor],
}];
self.label.attributedText = attributedString;
The code is rather verbose and not very reusable. Let's try extracting the dictionary.
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
is a property of NSAttributedString
that allows further configuration of paragraph or ruler attributes. With JPStringAttribute
, paragraph style is easier to modify.
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;
Compared to
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];
}
Have you ever had to append a bunch of NSAttributedStrings
, then assign them to a 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;
Is this better?
[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];
}];
There is an example included with the project. To install,
cd Example/
pod install
open JPAttributedString.xcworkspace/
Send me pull requests!
James Paolantonio - @jpaolantonio
Let me know if you are using this or want anything changed!! :)