Here is a dedicated extension of such for macOS:
// MARK: - NSAttributedString extension
// Ref: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html
public extension NSAttributedString {
@objc var boundingDimension: NSSize {
let rectA = boundingRect(
with: NSSize(width: Double.infinity, height: Double.infinity),
options: [.usesLineFragmentOrigin]
)
let textStorage = NSTextStorage(attributedString: self)
let textContainer = NSTextContainer()
let layoutManager = NSLayoutManager()
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
textContainer.lineFragmentPadding = 0.0
layoutManager.glyphRange(for: textContainer)
let rectB = layoutManager.usedRect(for: textContainer)
let width = ceil(max(rectA.width, rectB.width))
let height = ceil(max(rectA.height, rectB.height))
return .init(width: width, height: height)
}
}
I just wonder whether this (thereotically) works on Linux and Windows.