LoginSignup
4
6

More than 5 years have passed since last update.

[iOS] 文字列の適正な高さを求める方法

Posted at

テーブルビューのセルの中に更にテーブルビューを入れ子でいれた時、入れ子で入れたテーブルビューの各セルの高さが動的に変わらなかったので、以下のように各要素毎に高さを求めました。

CalculateHeightOfString.swift
let string = "何かしらの文字列"
let maxSize = CGSize(width: UIScreen.main.bounds.width, height: 5000) // 幅は適正な値、高さは多めの数値を指定
let font = UIFont.systemFont(ofSize: 18) // フォントのサイズによって高さが変わるので実際のサイズを指定
let size = (string as NSString).boundingRect(with: maxSize, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSFontAttributeName: font], context: nil).size
4
6
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
4
6