NSMutableAttributedStringを使用して複数行のUILabelを作成しようとしています。これは、次のように完全な文字列に属性を割り当てると正常に機能します。
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)];
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:[UIFont systemFontOfSize:13];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(0, [string length])];
contentLabel.attributedText = string;
しかし、私が必要としているのは、NSAttributedStringのさまざまなサブ範囲にいくつかの属性を適用することです(特定の単語を太字にするため)。
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,100)];
[contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
[contentLabel setNumberOfLines:0];
[contentLabel setFont:[UIFont systemFontOfSize:13];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello I am a long sentence that should break over multiple lines"];
[string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:13] range:NSMakeRange(3, 5)];
contentLabel.attributedText = string;
私が見つけたのは、これを行うと、テキストがラベルの複数の行にレンダリングされなくなることです。これは、ラベルのフレームの垂直方向の中央に配置された1本の線としてレンダリングされます。
ここに欠けているものはありますか?