UILabel:自定义下划线颜色?

我需要一个UILabel的文本是黑色的,但在文本下面有一个浅灰色的下划线。 这可能与NSAttributedStringTTTAttributedLabel ? 或者是使用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; 

注意:你也可以像在这篇文章中那样在特定的string范围内加下划线。 另外请注意,它只适用于ios6 +。

我们可以在一行中创build多个属性(使用NSDictionary符号 – @ {} )来创build一个包含实际文本的特定UILabel,而不是创build一个本地NSMutableAttributedString并逐个添加属性。

目标C

 [someUILabel setAttributedText: [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", [someObject stringProperty]] attributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick), NSUnderlineColorAttributeName:[[UIColor alloc] initWithRed:0.953f green:0.424f blue:0.416f alpha:1.00f]}]]; 

在上面的例子中,我们设置了一个粗体的下划线 – 共有2个属性。

Swift

 self.someUIButton.setAttributedTitle(NSAttributedString(string: "UIButtonStringTitle", attributes: [NSUnderlineStyleAttributeName : 1]), forState: .Normal) 
 m-farhan.com farhan will be underlined //----------------------------- // Create attributed string //----------------------------- NSString *str = @"m-Farhan.com"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str]; // Add attribute NSUnderlineStyleAttributeName [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(2, 4)]; // Set background color for entire range [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor colorWithRed:0.103 green:0.305 blue:0.492 alpha:1.000] range:NSMakeRange(0, [attributedString length])]; // Define label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 280, 80)]; [label setLineBreakMode:UILineBreakModeWordWrap]; [label setTextColor:[UIColor whiteColor]]; [label setBackgroundColor:[UIColor clearColor]]; [label setTextAlignment:UITextAlignmentLeft]; // Set label text to attributed string [label setAttributedText:attributedString]; [[self view] addSubview:label]; 
 NSAttributedString *title; title = [[NSAttributedString alloc] initWithString:@"iphone app" for NSAttributedString" attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:36], NSUnderlineStyleAttributeName : @1 , NSStrokeColorAttributeName : [UIColor blackColor]}]; UILabel *label; label = [[UILabel alloc] initWithFrame:CGRectMake( (self.view.bounds.size.width - title.size.width) / 2.0f, 40.0f, title.size.width, title.size.height)]; label.attributedText = title; [self.view addSubview:label]; 
 // Print `str` in black, and underline the word STRING in gray. NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"This is my STRING"]; [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, str.length-7)]; [str addAttribute:NSUnderlineColorAttributeName value:[UIColor grayColor] range:NSMakeRange([str length]-6, 6)]; [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange([str length]-6, 6)]; _label.attributedText = str; // assuming you have an iVar name `label`