LoginSignup
111
105

More than 5 years have passed since last update.

UILabelやUIViewでぴったりのサイズを取得する

Posted at

UILabelとかに文字を挿入した後に、それがぴったり入るサイズを計算したい場合があります。
このような場合、sizeToFitを実行することでビューのサイズが最適なものに変更されます。

[_titleLabel sizeToFit];

ただ、変更せずにサイズを取得したい場合や、
そのサイズを元に変更を加える場合などが存在します。

このような場合に使えるのがsizeThatFitsで、
自分のサイズを変更せずに、要素がぴったり入るサイズを返してくれます。

CGSize maxSize = CGSizeMake(parentWidth,parentHeight);
CGSize titleLabelSize = [_titleLabel sizeThatFits:maxSize];
111
105
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
111
105