10

簡単なクエリが 1 つあります。UIButton のテキストと色に下線を引く必要があります。uibutton の下線テキストがありますが、色合いに関しては機能しません。xib から色合いを設定しましたが、そこから取得していません。以下は私のコードです。

 NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"Testing"];
[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];
[_helpWithLoginLabel setAttributedTitle:commentString forState:UIControlStateNormal];
4

5 に答える 5

8

textColor ( NSForegroundColorAttributeName) には別の属性があります。次に例を示します。

NSArray * keys = [[NSArray alloc] initWithObjects:NSForegroundColorAttributeName, NSUnderlineStyleAttributeName, nil];
NSArray * objects = [[NSArray alloc] initWithObjects:self.descriptionTextView.textColor, [NSNumber numberWithInt:NSUnderlineStyleSingle], nil];
NSDictionary * linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
self.descriptionTextView.linkTextAttributes = linkAttributes;

お役に立てれば...

于 2013-10-08T19:53:06.000 に答える
7

リテラルを使用した @dequin と同じ応答は、より短い構文を取得します。

NSDictionary * linkAttributes = @{NSForegroundColorAttributeName:button.titleLabel.textColor, NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:button.titleLabel.text attributes:linkAttributes];            
[button.titleLabel setAttributedText:attributedString];
于 2015-10-08T17:48:37.440 に答える