UILabel:自定义下划线颜色? [英] UILabel: Custom underline color?

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

问题描述

我需要 UILabel 的文字为黑色,但文字下方有一个浅灰色下划线。是否可以使用 NSAttributedString TTTAttributedLabel ?或者是否需要使用Core Graphics的自定义绘图?

I need the text of a UILabel to be black, but have a light gray underline under the text. Is this possible with NSAttributedString or TTTAttributedLabel? Or is custom drawing using Core Graphics needed?

澄清:
我需要使用不同颜色下划线的特定颜色文本。示例:红色下划线上的蓝色文字。

CLARIFICATION: I need a specific color text on a different color underline. Example: blue text on red underline.

推荐答案

您可以使用 NSAttributedString 如下所示。

NSMutableAttributedString* string = [[NSMutableAttributedString alloc]initWithString:@"you string"];
[string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length)];//TextColor
[string addAttribute:NSUnderlineStyleAttributeName value:underlineNumber range:NSMakeRange(0, string.length)];//Underline color
[string addAttribute:NSUnderlineColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, string.length)];//TextColor
 yourlabel. attributedText = string;

注意:您还可以将特定范围的字符串加下划线为喜欢这篇文章。另请注意,它仅适用于ios6 +。

Note: You can also underline particular range of string as like in this post. Also note down, it works ios6+ only.

这篇关于UILabel:自定义下划线颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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