2013-10-21 47 views
-1

我無法寫出下面的代碼在xcod4.2與模擬器5.0告訴我另一種解決方案如何在模擬器5.0中將NSAttributedString對象設置爲xcode4.2中的uilabel?

NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initwithString:@"This is Green and Red"]; 
NSDictionary *[email protected]{NSForeGroundColorAttributeName:[UIColor redColor]}; 
NSDictionary *[email protected]{NSForeGroundColorAttributeName:[UIColor greenColor]}; 

[str setAttribute:greenatt range:NSMakeRange(9,5)]; 
[str setAttribute:redatt range:NSMakeRange(19,3)]; 

lbl.attributedText=str; 
+0

什麼是你想實現的,使用上面的代碼? – Max

+0

你看過你收到的錯誤,說了什麼? – Wain

+0

錯誤:UILabel歸檔文本在xcode4.2中不可用。 –

回答

1

您需要導入CoreText框架,然後用it.Try做kCTForegroundColorAttributeName。更多信息請參見本例

NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initwithString:@"This is Green and Red"]; 


[str addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:NSMakeRange(9,5)]; 
[str addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor greenColor] CGColor] range:NSMakeRange(19,3)]; 

lbl.attributedText=str; 
+0

看到我編輯的答案.... –

+0

@LearnIphone什麼hpnd兄弟.... –

+0

嘗試像這樣導入#import

0

試試這個,已經嘗試過了偉大的工作:

NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initWithString:@"This is Green and Red"] ; 

    NSDictionary *[email protected]{NSForegroundColorAttributeName:[NSColor redColor]}; 
    NSDictionary *[email protected]{NSForegroundColorAttributeName:[NSColor greenColor]}; 

    [str addAttributes:redatt range:NSMakeRange(9,5)]; 
    [str addAttributes:greenatt range:NSMakeRange(1,4)]; 

    [lbl.textStorage setAttributedString:str]; 
+0

[lbl.textStorage setAttributedString:str]; //不能在xcode4.2中用模擬器5.0工作,需要特定於版本的解決方案。因爲uilabel沒有textStorage屬性和setAttributedString Suntion –

+0

已經嘗試在MAC OS中,它工作得很好,這就是爲什麼建議。 –

相關問題