私はiOS 6を使用しているので、属性付き文字列は使いやすいはずですよね? うーん...それほどではありません。
私がしたいこと:
のカスタム サブクラスを使用してUIButton
(それは にカスタムすることは何もしませんtitleLabel
)、次のような複数行の属性付きタイトルが必要です。
- 最初の行のすべての大文字(属性の一部ではないことに気づきました)
- 1行目を太字
- 1行目に下線あり
- 2行目の「通常の」体重
- 2行目下線なし
- 両線を中心に
これまでのところ、# の 1 から 5 を取得できました (少なくとも、取得したと思っていましたが、現在のテストでは複数行のテキストでエラーが発生しています)。テキストを中央に配置すると、アプリがクラッシュし続けます。6 つの項目すべてを (さまざまな方法で) 動作させようとすると、次のクラッシュ/エラーが発生します。
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason:
'NSAttributedString invalid for autoresizing,
it must have a single spanning paragraph style
(or none) with a non-wrapping lineBreakMode.'
私が試したことに基づいて、次のオプションのいずれかを使用できるようですが、両方を使用することはできません。
- 複数行の中央揃えのラベル
- 帰属ラベル
必要に応じて、どちらか一方と一緒に暮らすことができますが、かなり単純な概念のように見えるものを持てないなんて信じられません。
誰かが私が間違っていることを教えてもらえますか?
私が試しているコードの最後の反復は次のとおりです。
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
[style setLineBreakMode:NSLineBreakByWordWrapping];
UIFont *font1 = [UIFont fontWithName:@"HelveticaNeue-Medium" size:20.0f];
UIFont *font2 = [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0f];
NSDictionary *dict1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
NSFontAttributeName:font1};
NSDictionary *dict2 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),
NSFontAttributeName:font2};
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"LINE 1\n" attributes:dict1]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:@"line 2" attributes:dict2]];
[[self buttonToStyle] setAttributedTitle:attString forState:UIControlStateNormal];
[[[self buttonToStyle] titleLabel] setNumberOfLines:0];
[[[self buttonToStyle] titleLabel] setLineBreakMode:NSLineBreakByWordWrapping];