Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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

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
Tamucha
東京でエンジニアをしています。 主にWEBシステムとスマフォアプリの開発をしております。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした