我试图在标签中加下一些文字。但是,我不知道如何获得标签中整个文本的范围。这就是我到目前为止所做的:
NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy];
[mat addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)} range://??];
self.tableLabel.attributedText = mat;
我应该为该范围添加什么?
答案 0 :(得分:22)
对于您可能想要使用的范围:
NSMakeRange (0, mat.length);
像这样:
NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy];
[mat addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)} range:NSMakeRange (0, mat.length)];
self.tableLabel.attributedText = mat;
答案 1 :(得分:0)
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;