2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift:NSAttributedStringの高さを計算する

Posted at
let attributes: [NSAttributedString.Key : Any] = [
    NSAttributedString.Key.font : NSFont.systemFont(ofSize: fontSize),
    NSAttributedString.Key.foregroundColor : NSColor.red
]

let attrStr = NSAttributedString(string: "Text", attributes: attributes)

let fieldW: CGFloat = 200 // 表示するテキストフィールドの幅
let rect = attrStr.boundingRect(with: NSSize(width: fieldW, height: CGFloat.greatestFiniteMagnitude),
                                            options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil)

let calcHeight: CGFloat = ceil(rect.height)
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?