5

I need to underline a certain portion of a UILabel as the title suggests. For example: Please click ESPNSoccernet to read the latest Football News. I would like to underline the word ESPNSoccernet. This is because I want it to be clickable and it need to link to the website.

Need some guidance on doing this. If there is another way, do tell me...

lakshmen
  • 28,346
  • 66
  • 178
  • 276

7 Answers7

15

for ios 6, you can use AttributedStrings

NSMutableAttributedString *yourString = [[NSMutableAttributedString alloc] initWithString:@"Please click ESPNSoccernet to read the latest Football News."];
[yourString addAttribute:NSUnderlineStyleAttributeName
                        value:[NSNumber numberWithInt:1]
                        range:(NSRange){0,25}];

label.attributedText = [yourString copy];

you can also use a 3rd party UILable library TTTAttributedLabel.

Bonnie
  • 4,943
  • 30
  • 34
2

In Xcode:

  1. Select the label and choose identity inspector.
  2. In text choose Attributed instead of plain.
  3. Now Select the portion of text you want to underline.
  4. Select font and choose underline in fonts style.

There you go.

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Suhail Bhat
  • 443
  • 4
  • 13
2

Swift 2.0:

1) Make a nsmutablestring with underline attribute and add to sampleLabel's text.

2) Add a tap gesture to sampleLabel and associate a method for further action.

override func viewDidLoad() {
    super.viewDidLoad()

    let newsString: NSMutableAttributedString = NSMutableAttributedString(string: "Tap here to read the latest Football News.")
    newsString.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue], range: NSMakeRange(4, 4))
    sampleLabel.attributedText = newsString.copy() as? NSAttributedString

    let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapResponse:")
    tapGesture.numberOfTapsRequired = 1
    sampleLabel.userInteractionEnabled =  true
    sampleLabel.addGestureRecognizer(tapGesture)

}
func tapResponse(recognizer: UITapGestureRecognizer) {
    print("tap")
}
Alvin George
  • 14,148
  • 92
  • 64
1

UILabel is only capable of displaying plain text strings (in iOS 6 it can now also display NSAttributedStrings, but this will not work in older iOS versions, so it is best not to rely on this), so you will not be able to do this with a label.

You can look at TTTAttributedLabel for displaying attributed text (so you can add underlines and other formatting), but you will not be able to add hyperlinks with this class.

The options you have for a clickable segment of the string are basically:

  1. Use a plain UILabel and overlay a UIButton over the part that you want to be clickable, or

  2. Use TTTAttributedLabel to achieve the underline effect, and a UITapGestureRecognizer to detect and handle taps (note that this will capture taps on the entire label, not just the underlined part).

For iOS 6:

UILabel *label = [[UILabel alloc] init];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Tap here to read the latest Football News."];
[string addAttribute:NSUnderlineStyleAttributeName value:@(1) range:NSMakeRange(4, 4)];
label.attributedText = [string copy];

For earlier iOS versions as well as iOS 6:

TTTAttributedLabel *label = [[TTTAttributedLabel alloc] init];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Tap here to read the latest Football News."];
[string addAttribute:NSUnderlineStyleAttributeName value:@(1) range:NSMakeRange(4, 4)];
label.text = [string copy];

Then add a gesture recogniser and implement handleTap::

UITapGestureRecognizer *recogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[label addGestureRecognizer:recogniser];

- (void)handleTap:(UITapGestureRecognizer *)recogniser {
    // Handle the tap here
}
Greg
  • 9,068
  • 6
  • 49
  • 91
  • in iOS 6.0 UILabel supports NSAttributedString – Bonnie May 13 '13 at 10:31
  • @Bonnie yes, but most developers cannot afford to not support iOS 5 at the moment, so it is best to use something that works for older software versions as well. – Greg May 13 '13 at 10:32
  • If you're using TTTAttributedLabel and wonder how to detect the tap, just make sure you add a TTTAttributedLabelDelegate to the label. The delegate should implement didSelectLinkWithURL. – Ciryon Nov 18 '15 at 12:23
1

Well, i have done the same thing like this:

Make a custom button with text: ESPNSoccernet, and background clearColor Add a label as a subview with height 1 and some background color to this button, such that "ESPNSoccernet" looks underlined. Put the remaining text in a label adjacent to this button, so that it looks like a whole text.

Hope it helps!

Note: if you r doing only >iOS 6.0, you might wanna check the other answers.

Nikita P
  • 4,226
  • 5
  • 31
  • 55
0

If this app for ios 6 or later version in that case you can use NSMutableAttributedString

  NSMutableAttributedString *labelText = [[NSMutableAttributedString alloc] initWithString:@"Hello this is demmy label for testing"];
    [labelText addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1]
       range:(NSRange){10,10}];

    label.attributedText = labelText;

for less version you can use like this..

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 116, 171, 20)];
label.text = @"Hello this is demmy label for testing";
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:16];
[self.view addSubview:label];

//underline code
CGSize expectedLabelSize = [[m_BCListArray objectAtIndex:tagcount] sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:UILineBreakModeWordWrap];

UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake((label.frame.size.width - expectedLabelSize.width)/2,    expectedLabelSize.height + (label.frame.size.height - expectedLabelSize.height)/2,   expectedLabelSize.width, 1);
viewUnderline.backgroundColor=[UIColor whiteColor];
[self.view addSubview:viewUnderline];
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

The following code snippet produces desired result :

    NSDictionary *dictAttribute = @{NSForegroundColorAttributeName:[UIColor redColor],NSUnderlineStyleAttributeName:@1};        
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"Please click ESPNSoccernet to read the latest Football News." attributes:dictAttribute];

    lblText.attributedText = attrString;
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177