I'm using SwiftUI and NSViewRepresentable to create a custom NSTextView that can display selectable text. Here's my code:
struct SelectableText: NSViewRepresentable {
let text: String
let font: NSFont?
init(text: String, font: NSFont? = nil) {
self.text = text
self.font = font
}
func makeNSView(context: Context) -> NSTextField {
let textField = NSTextField(wrappingLabelWithString: text)
textField.isBezeled = false
textField.isEditable = false
textField.isSelectable = true
textField.backgroundColor = .clear
textField.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
return textField
}
func updateNSView(_ nsView: NSTextField, context: Context) {
nsView.stringValue = text
nsView.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
}
}
I want to change the color of the selection highlight in the NSTextView. I tried setting the selectedTextAttributes property, but it didn't work as expected. The NSTextView is not receiving the events it needs to handle text selection correctly.
How can I change the selection color in an NSTextView in SwiftUI? Any help would be greatly appreciated.
If you meant
NSTextView, here is a possible approach: