我需要将 UILabel
的文本设为黑色,但文本下方有浅灰色下划线。这可以用 NSAttributedString
或 TTTAtributedLabel
实现吗?或者是否需要使用 Core Graphics 自定义绘图?
澄清: 我需要不同颜色下划线的特定颜色文本。示例:红色下划线上的蓝色文本。
最佳答案
您可以按如下方式使用 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;
注意:您还可以将特定范围的字符串加下划线,如 like in this post .另请注意,它仅适用于 ios6+。
关于ios - UILabel:自定义下划线颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22419945/