9

我试图在标签中强调一些文本。但是,我不知道如何获取标签中整个文本的范围。这是我到目前为止所拥有的:

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

我应该为范围添加什么?

4

2 回答 2

22

对于您可能想要使用的范围:

NSMakeRange (0, mat.length);

像这样:

NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy];
[mat addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)} range:NSMakeRange (0, mat.length)];
self.tableLabel.attributedText = mat;
于 2013-03-10T22:51:59.947 回答
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;
于 2017-09-20T10:16:53.077 回答