我正在使用 TTAtributedLabel 库,通过以下代码在标签中使用多色。
[lifeEventLabel setText:tempString afterInheritingLabelAttributesAndConfiguringWithBlock:^(NSMutableAttributedString *mutableAttributedString) {
NSRange blueRage = [tempString rangeOfString:[tempDict valueForKeyPath:@"person.firstName"]];
if (blueRage.location != NSNotFound) {
// Core Text APIs use C functions without a direct bridge to UIFont. See Apple's "Core Text Programming Guide" to learn how to configure string attributes.
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:blueRage];
}
return mutableAttributedString;
}];
我的问题是我在一个头文件中使用了一个常量 UIColor
#define TEXT_LINK_COLOR [UIColor colorWithRed:(68/255.0) green:(110/255.0) blue:(126/255.0) alpha:1];
但现在的问题是我无法使用以下方法访问它,以便为 uilabel 中的部分字符串提供上述颜色
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor blueColor].CGColor range:blueRage];
所以谁能告诉我如何在上面的行中使用上面的常量颜色来替换 [UIColor blueColor].CGColor
因为它现在给我编译错误?
最佳答案
示例代码:
@interface UIColor (MyProject)
+(UIColor *) textLinkColor ;
@end
@implementation UIColor (MyProject)
+(UIColor *) textLinkColor { return [UIColor colorWithRed:(68/255.0) green:(110/255.0) blue:(126/255.0) alpha:1]; }
@end
使用:
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:@"Label"];
[text addAttribute: NSForegroundColorAttributeName value:[UIColor textLinkColor] range: NSMakeRange(2, 1)];
[myLabel setAttributedText:text];
关于ios - TTAtributedLabel 设置多色使用 UIColor,它在 header 中用 MACRO 定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21875364/