0

在此处输入图像描述

输出应该是这样的

在此处输入图像描述

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];

如何改变一个字母的颜色?我只需要更改标签文本。

4

6 回答 6

4

答案: NSAttributedString

检查这个答案:iOS5 和 iOS6 的答案也

例如:

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)];

在您的.m 文件中添加以下#import行:

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

祝你好运 !!!

于 2013-05-22T12:59:04.407 回答
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;
于 2013-05-22T13:04:57.143 回答
0

iOS 6.0 之后可以NSAttributedString默认使用。UILabel

为了支持 iOS 6.0 以下,您需要使用TTTAttributedLabel。您可以在自述文件中找到使用和安装说明。另请注意,它还使用NSAttributedString. 所以你可以结合TTTAttributedLabel+UILabel来支持iOS 6+及以下。

当然,您也可以创建自己的UILabel.

于 2013-05-22T13:01:53.280 回答
0

Swift 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)
于 2018-02-14T15:41:39.930 回答
0

尝试这个:

NSMutableAttributedString *newStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Name *"]];
[newStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 1)];
label.attributedText = newStr;
于 2018-02-16T08:19:53.483 回答
-1

您应该使用 NSAttributedString 来更改单个字符的属性。

于 2013-05-22T12:56:28.013 回答