Skip to content

Instantly share code, notes, and snippets.

@vishnevskiy
Created April 13, 2015 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishnevskiy/de9f64d4f9a452956243 to your computer and use it in GitHub Desktop.
Save vishnevskiy/de9f64d4f9a452956243 to your computer and use it in GitHub Desktop.
attributed string
+ (NSAttributedString *)attributedStringWithContent:(NSArray *)nodes {
return [self attributedStringWithContent:nodes attributes:@{}];
}
+ (NSAttributedString *)attributedStringWithContent:(NSArray *)nodes attributes:(NSDictionary *)attributes {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
for (NSDictionary *node in nodes) {
if ([node[@"type"] isEqual:@"paragraph"]) {
[attributedString appendAttributedString:[self attributedStringWithContent:node[@"content"]]];
}
else if ([node[@"type"] isEqual:@"text"]) {
NSMutableDictionary *newAttributes = [attributes mutableCopy];
if (!newAttributes[NSForegroundColorAttributeName]) {
newAttributes[NSForegroundColorAttributeName] = [RCTConvert UIColor:@"#737f8d"] /* GREY3 */;
}
if (!newAttributes[NSFontAttributeName]) {
newAttributes[NSFontAttributeName] = [UIFont fontWithName:@"Whitney Book" size:15];
}
[attributedString appendAttributedString:[[NSAttributedString alloc]
initWithString:node[@"content"]
attributes:newAttributes]];
}
else if ([node[@"type"] isEqual:@"strong"] || [node[@"type"] isEqual:@"em"]) {
// TODO: we need italic version of whitney
[attributedString appendAttributedString:[self attributedStringWithContent:node[@"content"] attributes:@{
NSFontAttributeName: [UIFont fontWithName:@"WhitneyBold" size:15],
}]];
}
else if ([node[@"type"] isEqual:@"u"]) {
[attributedString appendAttributedString:[self attributedStringWithContent:node[@"content"] attributes:@{
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
}]];
}
else if ([node[@"type"] isEqual:@"link"]) {
// TODO: make links clickable
[attributedString appendAttributedString:[self attributedStringWithContent:node[@"content"] attributes:@{
NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone),
NSForegroundColorAttributeName: [RCTConvert UIColor:@"#00b0f4"] /* ? */,
NSLinkAttributeName: node[@"target"]
}]];
}
else if ([node[@"type"] isEqual:@"mention"]) {
[attributedString appendAttributedString:[self attributedStringWithContent:node[@"content"] attributes:@{
NSForegroundColorAttributeName: [RCTConvert UIColor:@"#738bd7"] /* BRAND_PURPLE */,
NSBackgroundColorAttributeName: [RCTConvert UIColor:@"#f1f3fb"] /* BRAND_PURPLE -90% */,
}]];
}
else if ([node[@"type"] isEqual:@"newline"]) {
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
}
else {
NSLog(@"unknown node: %@", node);
}
}
return attributedString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment