同一UILabel中是否可以有多种颜色 [英] Is it possible to to have more than one color in the same UILabel

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

问题描述

我要在同一UILabel中使用多种字体颜色。我不知道是否有可能。我真的不这么认为,但是也许你们当中有些人有一个明智的解决方案?也许像 stringWithFormat 之类的东西。 [NSString stringWithFormatAndColor:@带有颜色的文本:%@%@,文本,颜色,文本,颜色]

I want more than one font color in the same UILabel. I dont know if it is possible. I dont really think so but maybe some of you out there have a smart solution for it? Maybe something like stringWithFormat. [NSString stringWithFormatAndColor: @"Text with color: %@ %@", text, color, text, color]

此图像说明了我要完成的工作:

This image illustrate what I'm trying to accomplish:

推荐答案

您可以使用NSAttributedSting实现此目的。支持属性字符串的UILabels易于使用的直接替换是 TTTAtributedLabel OHAttributedLabel

You can achieve this with NSAttributedSting. An easy to use drop-in replacement for UILabels with support for attributed strings is TTTAtributedLabel or OHAttributedLabel

以我的经验,使用NSMutableAttributedStrings进行构建会更容易

In my experience it is easier to work with NSMutableAttributedStrings and build it up step by step.

NSMutableAttributedString *attrStr = [NSMutableAttributedString attributedStringWithString:@""];

NSMutableAttributedString *a = [NSMutableAttributedString attributedStringWithString:@"This is "];      
[a setTextColor:aColorObj];
NSMutableAttributedString *b = [NSMutableAttributedString attributedStringWithString:@"only one "];     
[b setTextColor:bColorObj];
NSMutableAttributedString *c = [NSMutableAttributedString attributedStringWithString:@"Label"];     
[c setTextColor:cColorObj];

[attrStr appendAttributedString:a];
[attrStr appendAttributedString:b];
[attrStr appendAttributedString:c];


OHAttributedLabel *attributedTextLabel = [[OHAttributedLabel] initWithFrame:frame]
[attributedTextLabel setAttributedText:attrStr];

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

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