ios - TTAtributedLabel 设置多色使用 UIColor,它在 header 中用 MACRO 定义?

标签 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 因为它现在给我编译错误?

最佳答案

示例代码:

@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/

相关文章:

iphone - 连接到 ViewController.h 的圆形矩形按钮

iphone - iOS 上的重复推送通知

iphone - 选择另一个日历时 EKEvent 标识符发生变化

iphone - 在核心图形中加入两个矩形

xcode - 在 Swift 中更改特定 TabBarItem 的颜色

objective-c - 在对象 'delegate' 上找不到属性 'AddEventViewController'

ios - 快速创建选择框

iphone - 如何在 iPhone/iPad 中制作组合框?

objective-c - 如何正确使用 UIColor 作为属性

swift - 在 Swift 中更改消息 UIAlertController 的颜色