Search code examples
swiftmacosswiftuiappkitnstextview

How to change the selection color in an NSTextView in SwiftUI?


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.


Solution

  • If you meant NSTextView, here is a possible approach:

    struct SelectableTextView: NSViewRepresentable {
        let text: String
        let font: NSFont?
        
        init(text: String, font: NSFont? = nil) {
            self.text = text
            self.font = font
        }
        
        func makeNSView(context: Context) -> NSTextView {
            let textView = NSTextView(frame: .zero)
            textView.isEditable = false
            textView.isSelectable = true
            textView.backgroundColor = .clear
            textView.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
            textView.string = text
            
            textView.selectedTextAttributes = [
                NSAttributedString.Key.foregroundColor : NSColor.systemGreen, // Change the text color
                NSAttributedString.Key.backgroundColor: NSColor.systemRed// Change the background color
            ]
            return textView
        }
        
        func updateNSView(_ nsView: NSTextView, context: Context) {
            nsView.string = text
            nsView.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
        }
    }