Skip to content

Instantly share code, notes, and snippets.

@masstone-phoenix
Created July 3, 2017 08:10
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 masstone-phoenix/f9e3ba6d8f89dc859d384e23fce21bb1 to your computer and use it in GitHub Desktop.
Save masstone-phoenix/f9e3ba6d8f89dc859d384e23fce21bb1 to your computer and use it in GitHub Desktop.
create NSAttributeString using RE
//center
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
//origin string
NSString *detail = [NSString stringWithFormat:@"%zd more people .....", [RemoteConfigUtil moreRecentOnlineTotalNumber]];
//attributed string
NSMutableAttributedString *attributedDetail = [[NSMutableAttributedString alloc] initWithString:detail
attributes:@{NSParagraphStyleAttributeName:paragraphStyle,
NSFontAttributeName:[UIFont systemFontOfSize:16],
NSForegroundColorAttributeName:RGBCOLOR_HEX(0x333E55)}];
NSRange boldRange = [detail rangeOfString:@"\\d+" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
if (boldRange.location != NSNotFound) {
[attributedDetail addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:boldRange];
}
//UILabel的numberOfLines对它的attributedText属性也有作用
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment