Nikita Shytyk
Nikita Shytyk

Reputation: 398

NSAttributedString doesn't show part of the text

I need to create an attributed string:

NSString *forecastLow = [forecast valueForKey: @"low"];
NSString *forecastHigh = [forecast valueForKey: @"high"];
self.forecastLabel.font = [UIFont fontWithName: @"PT Sans" size: 13];
NSMutableAttributedString *attriburedString = [[NSMutableAttributedString alloc] initWithString: [NSString stringWithFormat: @"%@/%@\u00B0", forecastLow, forecastHigh]];
[attriburedString addAttribute: NSForegroundColorAttributeName
                         value: [UIColor colorWithRed: 0.f green: 0.f blue: 0.f alpha: 1.f]
                             range: NSMakeRange(0, [forecastLow length])];
 attriburedString addAttribute: NSForegroundColorAttributeName
                             value: [UIColor colorWithRed: 10 green: 10 blue: 10 alpha: 1]
                             range: NSMakeRange([forecastLow length], [attriburedString length] - 1)];
 self.forecastLabel.attributedText = attriburedString;

but the second part of it isn't displayed on the screen, just white color. when I'm making log of the attributed sting, it shows the full string. what's the problem?

Upvotes: 0

Views: 237

Answers (1)

rmaddy
rmaddy

Reputation: 318794

You are creating the 2nd color incorrectly. This:

[UIColor colorWithRed: 10 green: 10 blue: 10 alpha: 1]

should be:

[UIColor colorWithRed: 10/255.0 green: 10/255.0 blue: 10/255.0 alpha: 1]

The values need to be in the range 0.0 - 1.0. Anything over 1.0 gets treated as 1.0 therefore your code specified a white color (all 1.0).

Upvotes: 2

Related Questions