UILabel文字颜色变化

时间:2013-05-22 12:54:57

标签: ios objective-c swift xcode uilabel

enter image description here

输出应该是这样的

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

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

6 个答案:

答案 0 :(得分:4)

答案: NSAttributedString

选中此答案:Answer for iOS5 and iOS6 also

对于Eg:

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

GoodLuck !!!

答案 1 :(得分: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;

答案 2 :(得分:0)

在iOS 6.0之后可以使用默认NSAttributedString中的UILabel

为了支持iOS 6.0以下版本,您需要使用TTTAttributedLabel。您可以在自述文件中找到使用和安装说明。另请注意,它也使用NSAttributedString。因此,您可以合并TTTAttributedLabel + UILabel以支持iOS 6及以下版本。

当然,您也可以创建自己的UILabel子类。

答案 3 :(得分: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)

答案 4 :(得分:0)

试试这个:

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

答案 5 :(得分:-1)

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

相关问题