Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I underline text in iOS 6?

I am trying to underline some text in a label. However, I do't know how to get the range of the entire text in the label. This is what I have so far:

NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy];
[mat addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)} range://??];
self.tableLabel.attributedText = mat;

What should I put for the range?

like image 926
user1420042 Avatar asked Mar 10 '13 22:03

user1420042


2 Answers

NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:strComplete];

[attributedString addAttribute:NSUnderlineStyleAttributeName
                         value:@(NSUnderlineStyleSingle)
                         range:[strComplete rangeOfString:strFirst]];

[attributedString addAttribute:NSForegroundColorAttributeName
                         value:[UIColor redColor]
                         range:[strComplete rangeOfString:strSecond]];


cell.textLabel.attributedText = attributedString;
like image 162
Psnt143 Avatar answered Nov 15 '22 20:11

Psnt143


For the range you may want to use:

NSMakeRange (0, mat.length);

Like this:

NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy];
[mat addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)} range:NSMakeRange (0, mat.length)];
self.tableLabel.attributedText = mat;
like image 42
Ares Avatar answered Nov 15 '22 19:11

Ares