CocoaExtension // Fix NSAttributedString.boundingDimension.
This commit is contained in:
parent
dcff84c482
commit
b579bdca57
|
@ -49,22 +49,21 @@ public extension NSSize {
|
||||||
// Ref: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html
|
// Ref: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html
|
||||||
|
|
||||||
public extension NSAttributedString {
|
public extension NSAttributedString {
|
||||||
|
private static let tmpTextStorage = NSTextStorage()
|
||||||
|
private static let tmpLayoutManager = NSLayoutManager()
|
||||||
|
private static let tmpTextContainer = NSTextContainer()
|
||||||
|
|
||||||
@objc var boundingDimension: NSSize {
|
@objc var boundingDimension: NSSize {
|
||||||
let rectA = boundingRect(
|
Self.tmpTextStorage.setAttributedString(self)
|
||||||
with: NSSize(width: Double.infinity, height: Double.infinity),
|
if Self.tmpLayoutManager.textContainers.isEmpty || Self.tmpLayoutManager.textContainers.first !== Self.tmpTextContainer {
|
||||||
options: [.usesLineFragmentOrigin, .usesFontLeading]
|
Self.tmpLayoutManager.addTextContainer(Self.tmpTextContainer)
|
||||||
)
|
}
|
||||||
let textStorage = NSTextStorage(attributedString: self)
|
if Self.tmpTextStorage.layoutManagers.isEmpty || Self.tmpTextStorage.layoutManagers.first !== Self.tmpLayoutManager {
|
||||||
let textContainer = NSTextContainer()
|
Self.tmpTextStorage.addLayoutManager(Self.tmpLayoutManager)
|
||||||
let layoutManager = NSLayoutManager()
|
}
|
||||||
layoutManager.addTextContainer(textContainer)
|
Self.tmpTextContainer.lineFragmentPadding = 0
|
||||||
textStorage.addLayoutManager(layoutManager)
|
Self.tmpLayoutManager.glyphRange(for: Self.tmpTextContainer)
|
||||||
textContainer.lineFragmentPadding = 0.0
|
return Self.tmpLayoutManager.usedRect(for: Self.tmpTextContainer).size
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue