8

カスタム UIButton のタイトルに下線を追加しようとしていますが、ここにある例に従って、画面に表示できません。

これが私が使用しようとしているコードです:

NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"The Quick Brown Fox"];

[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];

[button setAttributedTitle:commentString forState:UIControlStateNormal];

これは表示されません。しかし、私が通常のことをするだけなら、次のようになります:

[button setTitle:@"The Quick Brown Fox" forState:UIControlStateNormal];

これはうまく表示されます。誰かが私に欠けているものを教えてもらえますか?

4

1 に答える 1

11

ここで問題に気づきました。見えなかったので映っていないと思いました。ただし、表示されていたのは、背景と同じ色です。

[button setTitleColor:myColor forState:UIControlStateNormal];attributedTitle も使用する色であると誤って想定しました。ただし、私の背景は黒であり、NSMutableAttributedString はデフォルトで黒にする必要があります。

[commentString addAttribute:NSForegroundColorAttributeName value:myColor  range:NSMakeRange(0, [commentString length])];

正しく表示されるようにします。

于 2013-08-14T15:15:46.143 に答える