2013-10-24 38 views
0

我需要創建一個屬性串的一部分:NSAttributedString不顯示文本

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; 

,但它的第二部分不是屏幕,只是白色顯示。 當我記錄屬性刺痛時,它顯示完整的字符串。 有什麼問題?

+0

檢查你的標籤或textview的大小,以確保它有足夠的空間爲整個字符串 – Jack

+0

@JackWu,當我顯示文本沒有屬性它工作正常 –

回答

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)。

+0

這是它,謝謝! –