Skip to content

Instantly share code, notes, and snippets.

@dothework
Created May 19, 2015 00:36
Show Gist options
  • Save dothework/76f702a5ea166e48fdd3 to your computer and use it in GitHub Desktop.
Save dothework/76f702a5ea166e48fdd3 to your computer and use it in GitHub Desktop.
Orange for the first comment only
- (NSAttributedString *) commentString {
NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] init];
BOOL bolFirst = YES;
for (Comment *comment in self.mediaItem.comments) {
// Make a string that says "username comment" followed by a line break
NSString *baseString = [NSString stringWithFormat:@"%@ %@\n", comment.from.userName, comment.text];
// Make an attributed string, with the "username" bold
NSMutableAttributedString *oneCommentString = [[NSMutableAttributedString alloc] initWithString:baseString attributes:@{NSFontAttributeName : lightFont, NSParagraphStyleAttributeName : paragraphStyle}];
NSRange usernameRange = [baseString rangeOfString:comment.from.userName];
[oneCommentString addAttribute:NSFontAttributeName value:boldFont range:usernameRange];
[oneCommentString addAttribute:NSForegroundColorAttributeName value:linkColor range:usernameRange];
NSRange commentRange = [baseString rangeOfString:comment.text];
// set the color to orange for the first comment only
if (bolFirst) {
[oneCommentString addAttribute:NSForegroundColorAttributeName value:oneCommentColor range:commentRange];
bolFirst = NO;
}
else {
[oneCommentString addAttribute:NSForegroundColorAttributeName value:linkColor range:commentRange];
}
[commentString appendAttributedString:oneCommentString];
}
return commentString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment