我需要检测#Tags描述中给出UILabel
并将文本颜色更改为[UIColor BlueColor];
我无法更改其中的特定文本颜色UILabel
到蓝色。现在我正在使用这个UILabel
在定制中UITableViewCell
。有什么办法可以解决这个问题来区分#Tags和普通文本的文本颜色?有人可以帮我解决这个问题吗?
-(NSMutableAttributedString*)decorateTags:(NSString *)stringWithTags{
NSError *error = nil;
//For "Vijay #Apple Dev"
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error];
//For "Vijay @Apple Dev"
//NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"@(\\w+)" options:0 error:&error];
NSArray *matches = [regex matchesInString:stringWithTags options:0 range:NSMakeRange(0, stringWithTags.length)];
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:stringWithTags];
NSInteger stringLength=[stringWithTags length];
for (NSTextCheckingResult *match in matches) {
NSRange wordRange = [match rangeAtIndex:1];
NSString* word = [stringWithTags substringWithRange:wordRange];
//Set Font
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, stringLength)];
//Set Background Color
UIColor *backgroundColor=[UIColor orangeColor];
[attString addAttribute:NSBackgroundColorAttributeName value:backgroundColor range:wordRange];
//Set Foreground Color
UIColor *foregroundColor=[UIColor blueColor];
[attString addAttribute:NSForegroundColorAttributeName value:foregroundColor range:wordRange];
NSLog(@"Found tag %@", word);
}
// Set up your text field or label to show up the result
// yourTextField.attributedText = attString;
//
// yourLabel.attributedText = attString;
return attString;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)