如何在SwiftUI中更改NSTextView中的选择颜色?

huangapple go评论65阅读模式
英文:

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

问题

我正在使用SwiftUI和NSViewRepresentable来创建一个自定义的NSTextView,可以显示可选文本。这是我的代码:

    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)
        }
    }

我想要更改NSTextView中选择高亮的颜色。我尝试设置selectedTextAttributes属性,但它没有按预期工作。NSTextView没有接收到处理文本选择所需的事件。

在SwiftUI中,我该如何更改NSTextView中的选择颜色?任何帮助将不胜感激。

英文:

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.

答案1

得分: 1

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)
    }
}
英文:

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)
    }
}

huangapple
  • 本文由 发表于 2023年6月9日 00:42:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定