NSForegroundColorAttributeName not working for UILabel

最后都变了- 提交于 2020-01-03 09:24:10

问题


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

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

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

回答1:


The problem is this line:
NSMutableAttributedString *str = [addActivityLbl.attributedText mutableCopy];

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

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.

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.

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

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

The underlyning works because underlyning is not a property of addActivityLbl.text (not inherited by 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.




回答2:


Make sure you're not setting some color on textColor property of UILabel after you've set the attributedText.




回答3:


I had a similar problem trying to set the color of the attributed title of a UIButton. I was applying other attributes like underline and they worked, but the NSForegroundColorAttributeName attribute had absolutely no effect.

After pulling my hair out and questioning my own sanity, I eventually discovered that my problem was that in the storyboard, my button was set to be a System button:

And it needed to be Custom:

After changing that one setting in storyboard I was able to set NSForegroundColorAttributeName with expected, sane behavior.

I hope this helps someone!



来源:https://stackoverflow.com/questions/28524513/nsforegroundcolorattributename-not-working-for-uilabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!