如何在iOS 6中为文本加下划线?

时间:2013-03-10 22:50:06

标签: ios objective-c string

我试图在标签中加下一些文字。但是,我不知道如何获得标签中整个文本的范围。这就是我到目前为止所做的:

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

我应该为该范围添加什么?

2 个答案:

答案 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;