0

我正在使用TTAtributedLabel庫在標籤中使用多色以下代碼。TTAtributedLabel使用在MACRO標題中定義的UIColor設置多色?

[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使用上面時時彩在上面的線爲給我編誤差R現在呢?

回答

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

最簡單的方法是,

UIColor *tempColor = TEXT_LINK_COLOR; 
[mutableAttributedString addAttribute:NSForegroundColorAttributeName value:tempColor range:blueRage]; 
相關問題