SwiftUIExtension // Porting read-only NSTextField to SwiftUI.

This commit is contained in:
ShikiSuen 2023-02-21 22:58:08 +08:00
parent 888e6a9ddf
commit 9fbfe22fa6
1 changed files with 23 additions and 0 deletions

View File

@ -172,3 +172,26 @@ public extension AppProperty {
)
}
}
// MARK: - Porting NSTextField (Label) to SwiftUI.
@available(macOS 10.15, *)
public struct AttributedLabel: NSViewRepresentable {
private let text: NSAttributedString
public init(attributedString: NSAttributedString) {
text = attributedString
}
public func makeNSView(context _: Context) -> NSTextField {
let textField = NSTextField(labelWithAttributedString: text)
textField.isSelectable = false
textField.allowsEditingTextAttributes = false
textField.preferredMaxLayoutWidth = textField.frame.width
return textField
}
public func updateNSView(_ nsView: NSTextField, context _: Context) {
nsView.attributedStringValue = text
}
}