0

我正在使用 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它现在给我的编译错误?

4

2 回答 2

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];
于 2014-02-19T09:34:04.630 回答
1

最简单的方法是,

UIColor *tempColor = TEXT_LINK_COLOR;
[mutableAttributedString addAttribute:NSForegroundColorAttributeName value:tempColor range:blueRage];
于 2014-02-20T13:51:46.803 回答