2013-05-22 75 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;