TTAtributedLabel使用UIColor设置多色,使用MACRO在头文件中定义?

时间:2014-02-19 08:54:15

标签: ios iphone uicolor nsmutableattributedstring tttattritubedlabel

我正在使用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,因为它现在给我编译错误了吗?

2 个答案:

答案 0 :(得分:1)

示例代码:

@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];

答案 1 :(得分:1)

最简单的方法是,

UIColor *tempColor = TEXT_LINK_COLOR;
[mutableAttributedString addAttribute:NSForegroundColorAttributeName value:tempColor range:blueRage];