9

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?

user1420042
  • 1,689
  • 9
  • 35
  • 53

2 Answers2

22

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;
Hless
  • 3,326
  • 20
  • 22
Ares
  • 5,905
  • 3
  • 35
  • 51
1
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;
Psnt143
  • 55
  • 8