英文:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论