Home > Cocoa > 複数行にわたったテキストを含むUILabelを適切な高さに調節する。

複数行にわたったテキストを含むUILabelを適切な高さに調節する。

  • 2010-01-09 (Sat) 23:13
  • Cocoa

複数行にわたったテキストを表示するためのUILabelのインスタンスであるlabelがあり、そのlabelを適切なサイズ高さに変更したい時がありました。

// すでに以下の2行は他の部分で設定されている。
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setNumberOfLines:0];

CGRect *frame;

frame = [label frame];
frame.size = CGSizeMake(WIDTH, 0);

[label setFrame:frame];
[label setText:@"long long long text..."];
[label sizeToFit];

ポイントはnumberOfLinesを0にすることと、sizeToFitメッセージを呼び出すこと。
numberOfLinesを0にしないと、sizeToFitを呼び出した時点で、指定した幅が無視されてしまう気がする。

毎回frameの幅を決めているのは、テキストが何度も書き換わる場所だから。
テキストが短いものになった場合、sizeToFitメッセージで短い幅が設定されてしまうと以降ずっとその短い幅になったままになってしまうため。

でもAppleのドキュメントには、以下のようにsizeToFitメッセージはnumberOfLinesを考慮するって書いてある。
自分のやり方がおかしいのか?

UILabel Class Reference」より抜粋
When the receiver is resized using the sizeToFit method, resizing takes into account the value stored in this property. For example, if this property is set to 3, the sizeToFit method resizes the receiver so that it is big enough to display three lines of text.

引用元: UILabel Class Reference

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://www.cocoalife.net/2010/01/post_829.html/trackback
Listed below are links to weblogs that reference
複数行にわたったテキストを含むUILabelを適切な高さに調節する。 from cocoa*life
blog comments powered by Disqus

Home > Cocoa > 複数行にわたったテキストを含むUILabelを適切な高さに調節する。

Feeds
Meta

Return to page top