All*_*ian 11
NSString *tweet = @"This is a tweet #MYTWEET";
NSArray *words = [tweet componentsSeparatedByString:@" "];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:tweet];
for (NSString *word in words) {
if ([word hasPrefix:@"#"]) {
// Colour your 'word' here
NSRange matchRange = [tweet rangeOfString:word];
[attrString addAttribute:kCTForegroundColorAttributeName
value:[UIColor redColor]
range:matchRange];
// Remember to import CoreText framework (the constant is defined there)
}
}
//Display your attributed string ...
Run Code Online (Sandbox Code Playgroud)
注意:如果您想知道如何显示字符串,这里有一个很好的开源项目:https://github.com/AliSoftware/OHAttributedLabel
归档时间: |
|
查看次数: |
3203 次 |
最近记录: |