UILabel与两种不同颜色的文本 [英] UILabel with text of two different colors

查看:115
本文介绍了UILabel与两种不同颜色的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 UILabel 中显示这样的字符串:

I want to display a string like this in a UILabel:


共有5个结果。

There are 5 results.

其中数字5为红色,其余字符串为黑色。

Where the number 5 is red in color and the rest of the string is black.

我如何在代码中这样做?

How can I do this in code?

推荐答案

使用 NSAttributedString 如下:

NSMutableAttributedString *text = 
 [[NSMutableAttributedString alloc] 
   initWithAttributedString: label.attributedText];

[text addAttribute:NSForegroundColorAttributeName 
             value:[UIColor redColor] 
             range:NSMakeRange(10, 1)];
[label setAttributedText: text];

我创建了一个 UILabel 扩展功能

这篇关于UILabel与两种不同颜色的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆