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

use*_*042 9 string objective-c ios

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

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

我该怎么办?

Are*_*res 22

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

NSMakeRange (0, mat.length);
Run Code Online (Sandbox Code Playgroud)

像这样:

NSMutableAttributedString *mat = [self.tableLabel.attributedText mutableCopy];
[mat addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)} range:NSMakeRange (0, mat.length)];
self.tableLabel.attributedText = mat;
Run Code Online (Sandbox Code Playgroud)

  • NSMakeRange()可以说是更好,因为它不依赖于C99的隐含文字,它们存在一些问题. (2认同)