UILabel
改行で区切られた 2 つの属性付き文字列を含むものがあります。最初の文字列のフォント サイズは 17 に設定され、2 番目の文字列は 14 に設定されています。最初の文字列のNSMutableAttributedString
内容が 1 行に収まらない場合は、最小のフォント サイズにサイズ変更します。
それは可能ですか?
プレーン テキストの IB で「最小フォント サイズに自動縮小」を設定することで、このような動作を構成するのは非常に簡単UILabel
ですが、属性付きテキストの場合はそれを行う方法がわかりません。
これが私のコードです:
NSString *eventName = @"Looong Event Name";
NSString *placeString = @"My place";
eventName = [eventName stringByAppendingString:@"\n"];
NSMutableAttributedString *attrName = [[NSMutableAttributedString alloc] initWithString:eventName];
[attrName addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:NSMakeRange(0, [eventName length])];
NSMutableAttributedString *attrPlace = [[NSMutableAttributedString alloc] initWithString:placeString];
[attrPlace addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, placeString.length)];
[attrPlace addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, placeString.length)];
NSMutableAttributedString *finalString = [[NSMutableAttributedString alloc] initWithAttributedString:attrName];
[finalString appendAttributedString:attrPlace];
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
nameLabel.attributedText = finalString;