【发布时间】:2018-08-16 08:00:23
【问题描述】:
我正在使用TTTAttributedLabel 并设置像 "text with #tag and www.example.com" 这样的文本。
我需要为 "www.example.com" 设置 redColor,为 "#tag" 设置 greenColor >。
但它设置为蓝色。
以下是我的代码:
[label setText:@"text with #tag and www.example.com" afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange rangeUrl = [[mutableAttributedString string] rangeOfString:@"www.example.com" options:NSCaseInsensitiveSearch];
NSRange rangeTag = [[mutableAttributedString string] rangeOfString:@"#tag" options:NSCaseInsensitiveSearch];
UIFont *boldSystemFont = [UIFont systemFontOfSize:IS_IPAD?16:14 weight:UIFontWeightRegular];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (font) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:rangeUrl];
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:[UIColor redColor] range:rangeUrl];
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:rangetag];
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:[UIColor greenColor] range: rangeTag];
[mutableAttributedString addAttribute:(NSString *)kCTUnderlineColorAttributeName value:[UIColor clearColor] range: rangeTag];
CFRelease(font);
}
return mutableAttributedString;
}];
如何解决这个问题。
请帮帮我!
【问题讨论】:
标签: ios objective-c colors tttattributedlabel