ios - UILabel:自定义下划线颜色?

标签 ios ios7 uilabel nsattributedstring tttattributedlabel

我需要将 UILabel 的文本设为黑色,但文本下方有浅灰色下划线。这可以用 NSAttributedStringTTTAtributedLabel 实现吗?或者是否需要使用 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/

相关文章:

ios - 在 itunes connect 中对应用程序进行评分时是否应该考虑展示广告?

iOS 按需 VPN : Only outside company

UITableView 在 iOS 7 中以偏移量开始

iOS7:日志:无法在此设备上启用 MKMapView 音高

ios - 在 AppDelegate 外部注册通知 (FCM)

ios - 检测 iOS Google+ 登录不完整。

ios - 将多个值存储到 NSDictionary 中的单个键

ios - 重新打开 IB 后 UITableViewCell 内容调整大小

ios - iOS中UITable的tableheaderview需要添加多个UI元素

ios - 暂停和恢复 AnimateWithDuration 动画 ios