2013-05-22 144 views
0

enter image description here的UILabel文本顏色更改

輸出應該是這樣的

enter image description here

UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(5,10,300,20)]; 
name.text =[Language localizedStringForKey:@"Name *"]; 
name.font = [UIFont systemFontOfSize:14.0f]; 
[name setBackgroundColor:[UIColor clearColor]]; 
[self.View addSubview:name]; 

如何改變一個字母的顏色?我只需要改變標籤文字。

+0

NSAttributedString ...請參見http://計算器。com/questions/3482346/how-do-you-use-nsattributedstring –

+0

http://stackoverflow.com/questions/3786528/iphone-ipad-how-exactly-use-nsattributedstring – ajithmn89

+0

你的目標是iOS 6.0及以上版本嗎? – Bartu

回答

4

答:NSAttributedString

檢查這樣的回答:Answer for iOS5 and iOS6 also

對於如:

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"Name *"]; 
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5,1)]; 

更新:

使這一變化

[string addAttribute:(NSString*)kCTForegroundColorAttributeName 
       value:(id)[[UIColor redColor] CGColor] 
       range:NSMakeRange(5,1)]; 

添加後#import線以下到您.m文件

#if (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE) 
#import <CoreText/CoreText.h> 
#else 
#import <AppKit/AppKit.h> 
#endif 

好運!

-1

您應該使用NSAttributedString能夠改變單個字符屬性。

0

默認使用NSAttributedStringUILabel在iOS 6.0之後可用。

爲了支持下面的iOS 6.0,你需要使用TTTAttributedLabel。您可以在自述文件中找到使用和安裝說明。另外請注意,它也採用NSAttributedString。所以你可以結合TTTAttributedLabel + UILabel來支持iOS 6+和更低版本。

當然,您也可以創建自己的子類UILabel

1

嘗試使用這一個。這將工作在IOS6或更高版本。

UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(5,10,300,20)]; 
name.font = [UIFont systemFontOfSize:14.0f]; 
[name setBackgroundColor:[UIColor clearColor]]; 
[self.view addSubview:name]; 

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Name *"]]; 
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 1)]; 
name.attributedText = attrStr; 
+0

OP聲明他正在開發iOS 4.3及更高版本。歸屬文字屬性將不適用於iOS 4和5. – Bartu

+0

嗯大聲笑.......廢物努力工作... –

0

斯威夫特4
注:記法屬性串鑰匙在SWIFT 4改變)

這裏是NSMutableAttributedString一個擴展,在字符串/文本添加/套顏色。

extension NSMutableAttributedString { 

    func setColor(color: UIColor, forText stringValue: String) { 
     let range: NSRange = self.mutableString.range(of: stringValue, options: .caseInsensitive) 
     self.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: range) 
    } 

} 

現在,嘗試上面延伸與UILabel,看看導致

let label = UILabel() 
label.frame = CGRect(x: 40, y: 100, width: 280, height: 200) 
let title = "Name" 
let star = "*" 
let stringValue = "\(title) \(star)" 
label.textColor = UIColor.lightGray 
label.numberOfLines = 0 
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue) 
attributedString.setColor(color: UIColor.red, forText: star) // or use direct value for text "red" 
label.font = UIFont.systemFont(ofSize: 26) 
label.attributedText = attributedString 
self.view.addSubview(label) 
0

試試這個:

NSMutableAttributedString *newStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Name *"]]; 
[newStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 1)]; 
label.attributedText = newStr;