1

i am using below code for button title with underline

 NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
[btn setAttributedTitle:[[NSAttributedString alloc] initWithString:@"See Event TnC" attributes:underlineAttribute] forState:UIControlStateNormal];
vikramarkaios
  • 301
  • 1
  • 13

3 Answers3

4

Hello vikramarkaios you can change the UIButton type system to Custom in button of attributes

enter image description here

i hope it's helps for you!!

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
G.S.Koti
  • 66
  • 6
0

You can disable animation on UIView before setting the button title.

[UIView setAnimationsEnabled:NO];
NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
[btn setAttributedTitle:[[NSAttributedString alloc] initWithString:@"See Event TnC" attributes:underlineAttribute] forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
  • I think you forgot something. It was supposed to be like this.NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}; NSMutableAttributedString *attributedStr= [[NSMutableAttributedString alloc] initWithString:@"ABC"]; [attributedStr setAttributes:underlineAttribute range:NSMakeRange(0, attributedStr.length)]; [obj setAttributedTitle:attributedStr forState:UIControlStateNormal]; – User18474728 Jun 28 '18 at 03:01
0

From iOS6 it is now possible to use an NSAttributedString to perform underlining (and anything else attributed strings support) in a much more flexible way:

NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"The Quick Brown Fox"];

[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];

Note: added this as another answer - as its a totally different solution to my previous one.

oddly (in iOS8 at least) you have to underline the first character otherwise it doesn't work!

so as a workaround, set the first char underlined with clear colour!

NSMutableAttributedString* tncString = [[NSMutableAttributedString alloc] initWithString:@"View Terms and Conditions"];

    // workaround for bug in UIButton - first char needs to be underlined for some reason!
    [tncString addAttribute:NSUnderlineStyleAttributeName
                      value:@(NSUnderlineStyleSingle)
                      range:(NSRange){0,1}];
    [tncString addAttribute:NSUnderlineColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0, 1)];


    [tncString addAttribute:NSUnderlineStyleAttributeName
                      value:@(NSUnderlineStyleSingle)
                      range:(NSRange){5,[tncString length] - 5}];

    [tncBtn setAttributedTitle:tncString forState:UIControlStateNormal];
Dipen
  • 268
  • 1
  • 14