UILabel: Custom underline color?
Asked Answered
P

6

13

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.

Piling answered 15/3, 2014 at 5:24 Comment(0)
M
23

You can do with NSAttributedString as below.

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;

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

Mer answered 15/3, 2014 at 5:29 Comment(1)
NSUnderlineColorAttributeName is available from iOS 7.0 right?Cadmus
M
5

Instead of creating a local NSMutableAttributedString and adding attributes one by one, we can always create multiple attributes in one single line (using NSDictionary symbols - @ { } ) to a specific UILabel including the actual text.

Objective 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]}]];

In the above example we have set an underline which is also bold - total 2 attributes.

Swift:

self.someUIButton.setAttributedTitle(NSAttributedString(string: "UIButtonStringTitle", attributes: [NSUnderlineStyleAttributeName : 1]), forState: .Normal)
Millymilman answered 11/11, 2015 at 12:39 Comment(0)
I
1
// 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`
Intercrop answered 15/3, 2014 at 5:59 Comment(0)
R
1
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];
Recalescence answered 15/3, 2014 at 7:12 Comment(0)
K
0
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];
Kana answered 15/3, 2014 at 5:33 Comment(1)
this doesn't explain how I can get a different text color from the underline color.Piling
H
-1

In swift, use NSAttributedString.Key.underlineColor.

Haricot answered 11/7, 2021 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.