NSForegroundColorAttributeName not working for UILabel
Asked Answered
W

3

7

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.

enter image description here

    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];
Wheaton answered 15/2, 2015 at 8:59 Comment(6)
Have you tried to pass a CGColor instead UIColor?Wheeling
Yes I did with kCTForegroundColorAttributeName attributeWheaton
Sorry again, could comment the other attributes and update me with the results?Wheeling
Please look in to the code I posted and the screenshot.Wheaton
Does that label changes the text color elsewhere?Wheeling
The only place is in code I posted before applying attributesWheaton
A
8

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.

Anguine answered 15/2, 2015 at 12:20 Comment(4)
I've tried to set attributed text before anything else and got very strange behaviour - dots instead of letters. Anyway, the only thing that works for me as expected is using UITextView instead of UILabel. I'm not that happy to use this workaround since I still do not understand root case of the problem. If I won't figure it out during next few days, I'll accept this answer.Wheaton
I don't know what RevUILabel is. I googled it and couldn't find anything (but a 2 SO questions). Is it a subclass of UIView?Anguine
No. It is just UILabel extentionWheaton
Using UITextView is the only solution I found so far.Wheaton
A
11

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

Adrianaadriane answered 28/9, 2016 at 13:55 Comment(0)
A
8

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.

Anguine answered 15/2, 2015 at 12:20 Comment(4)
I've tried to set attributed text before anything else and got very strange behaviour - dots instead of letters. Anyway, the only thing that works for me as expected is using UITextView instead of UILabel. I'm not that happy to use this workaround since I still do not understand root case of the problem. If I won't figure it out during next few days, I'll accept this answer.Wheaton
I don't know what RevUILabel is. I googled it and couldn't find anything (but a 2 SO questions). Is it a subclass of UIView?Anguine
No. It is just UILabel extentionWheaton
Using UITextView is the only solution I found so far.Wheaton
B
0

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:

enter image description here

And it needed to be Custom:

enter image description here

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

I hope this helps someone!

Broz answered 8/1, 2017 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.