0

I want to add underline to label. For that I have used below code. And I am using custom Font:

headingLbl.font=[UIFont fontWithName:GZFont size:20.0f];
NSMutableAttributedString *gpsSearch = [[NSMutableAttributedString alloc] initWithString:@"GPS Search"];
[gpsSearch addAttribute:(NSString*)kCTUnderlineStyleAttributeName
                        value:[NSNumber numberWithInt:kCTUnderlineStyleSingle]
                        range:(NSRange){0,[gpsSearch length]}];
self.headingLbl.attributedText = gpsSearch;
self.headingLbl.textColor = [UIColor blackColor];
vivek bhoraniya
  • 1,526
  • 2
  • 17
  • 36
  • is this a multi-line `UILabel`? because if it is not then you can override the `drawRect` of `UILabel` class -- link: http://stackoverflow.com/questions/2711297/underline-text-in-uilabel – staticVoidMan Dec 19 '13 at 09:19

2 Answers2

1

Use the below code. This worked for me.

NSMutableAttributedString *gpsSearch = [[NSMutableAttributedString alloc] initWithString:@"GPS Search"];
[gpsSearch addAttribute:NSUnderlineStyleAttributeName
                      value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
                      range:(NSRange){0,[gpsSearch length]}];

If it is not worked, then the problem may be with your custom font

manujmv
  • 6,450
  • 1
  • 22
  • 35
0
NSString *tempString=@"your string"; 

NSMutableAttributedString *attString=[[NSMutableAttributedString     
alloc]initWithString:tempString];     

int length=(int)[tempString length];  

[attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:2] 
range:NSMakeRange(0,length-1)];
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Charan Giri
  • 1,097
  • 1
  • 9
  • 15