10

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;
4

1 に答える 1

6

これはあなたの以前の質問の続きだと思います。

これを自動的に行うことはできないと思いますがsize、NSAttributedStringのメソッドを使用して、文字列が大きすぎるかどうかを確認し、必要に応じて自分で調整することができます。

于 2013-02-10T14:15:49.210 に答える