0

我需要创建一个属性字符串:

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;

但它的第二部分没有显示在屏幕上,只是白色。当我记录属性刺痛时,它会显示完整的字符串。有什么问题?

4

1 回答 1

2

您正在错误地创建第二种颜色。这个:

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

应该:

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

这些值需要在 0.0 - 1.0 的范围内。超过 1.0 的任何内容都被视为 1.0,因此您的代码指定了白色(全部为 1.0)。

于 2013-10-24T15:17:00.720 回答