NSForegroundColorAttributeName 不适用于 UILabel [英] NSForegroundColorAttributeName not working for UILabel

查看:39
本文介绍了NSForegroundColorAttributeName 不适用于 UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用NSForegroundColorAttributeName"更改子字符串颜色时遇到问题.我很困惑,因为正在应用相同子字符串的其他属性(下划线).

I have a problem changing substring color using "NSForegroundColorAttributeName". I'm confused since other attributes for the same substring are being applied(underline).

还尝试使用不推荐使用的属性UITextAttributeTextColor"和其他建议的属性kCTForegroundColorAttributeName"并获得相同的效果.

Also tried to use deprecated attribute "UITextAttributeTextColor" and other suggested attribute "kCTForegroundColorAttributeName" and got the same effect.

我正在为 iOS 7 编译.

I'm compiling for iOS 7.

    NSString *freeText = [NSString stringWithFormat:@"(%@)", self.me.activity.text];

    int lblMaxWidth = arrImgView.origin.x - WPRConstraints.BorderOffset;
    int lblMaxHeight = self.activityView.size.height - WPRConstraints.BorderOffset * 2;

    RevUILabel *addActivityLbl = [[RevUILabel alloc] initWithFontNameMultiLine:[WPRFonts LattoBold]
                                                                          size:16
                                                                 sizeConstrain:CGSizeMake(lblMaxWidth,lblMaxHeight)];

    addActivityLbl.text = [NSString stringWithFormat:@"%@ %@", Translate(self.me.activity.activityKey), freeText] ;
    addActivityLbl.textColor = BlackColor;

    NSMutableAttributedString *str = [addActivityLbl.attributedText mutableCopy];



    [str addAttribute:NSUnderlineColorAttributeName  value:[UIColor redColor] range:[addActivityLbl.text rangeOfString:freeText]];
    [str addAttribute:NSUnderlineStyleAttributeName  value:[NSNumber numberWithInteger:1] range:[addActivityLbl.text rangeOfString:freeText]];

    [str addAttribute:NSForegroundColorAttributeName
                        value:[UIColor redColor]
                range:[addActivityLbl.text rangeOfString:freeText]];


    addActivityLbl.attributedText = str;


    addActivityLbl.frame = CGRectMake(WPRConstraints.BorderOffset,
                                      WPRConstraints.BorderOffset,
                                      addActivityLbl.size.width,
                                      addActivityLbl.size.height);
    [self.activityView addSubview:addActivityLbl];

推荐答案

问题是这一行:
NSMutableAttributedString *str = [addActivityLbl.attributedText mutableCopy];

我不知道您代码的前几行,但可能是 addActivityLbl.attributedText 为空.

I don't know the previous lines of your code, but it might be the case that addActivityLbl.attributedText is empty.

其次,使用 NSAttributedStringUILabel 不如使用 UITextView 可靠.如果没有明确提供属性,UILabelattributedText 确实继承了 UILabeltext 的属性.

Secondly, using NSAttributedString with UILabel is not as reliable as using it with UITextView. The attributedText of a UILabel does inherit the attributes from the text of UILabel if the attributes are not explicitly provided.

您的 addActivityLbl.textColor 是黑色的.而且您还没有设置您的 addActivityLbl.attributedText ForegroundColorAttribute.这意味着您的 addActivityLbl.attributedText 将从您的 addActivityLbl.textColor 继承 BlackColor.

Your addActivityLbl.textColor is black. And you still have not set your addActivityLbl.attributedText ForegroundColorAttribute. This means that your addActivityLbl.attributedText will inherit the BlackColor from your addActivityLbl.textColor.

此行不会按预期工作;因为你还没有设置你的addActivityLbl.attributedText.freeText 还没有范围.

This line will not work as expected; because you still have not set your addActivityLbl.attributedText. freeText has no range yet.

[str addAttribute:NSForegroundColorAttributeName值:[UIColor redColor]range:[addActivityLbl.text rangeOfString:freeText]];

底层工作是因为底层不是 addActivityLbl.text 的属性(不是由 attributesText 继承的).

The underlyning works because underlyning is not a property of addActivityLbl.text (not inherited by attributedText).

我建议您改用 UITextView(这样更安全).另一种选择是在引用它的某个范围之前设置您的 label.attributedText.

I recommend you to use UITextView instead (which is safer). An other option is to set your label.attributedText, before referencing to some range of it.

这篇关于NSForegroundColorAttributeName 不适用于 UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆