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

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

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

问题

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

  1. struct SelectableText: NSViewRepresentable {
  2. let text: String
  3. let font: NSFont?
  4. init(text: String, font: NSFont? = nil) {
  5. self.text = text
  6. self.font = font
  7. }
  8. func makeNSView(context: Context) -> NSTextField {
  9. let textField = NSTextField(wrappingLabelWithString: text)
  10. textField.isBezeled = false
  11. textField.isEditable = false
  12. textField.isSelectable = true
  13. textField.backgroundColor = .clear
  14. textField.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
  15. return textField
  16. }
  17. func updateNSView(_ nsView: NSTextField, context: Context) {
  18. nsView.stringValue = text
  19. nsView.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
  20. }
  21. }

我想要更改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:

  1. struct SelectableText: NSViewRepresentable {
  2. let text: String
  3. let font: NSFont?
  4. init(text: String, font: NSFont? = nil) {
  5. self.text = text
  6. self.font = font
  7. }
  8. func makeNSView(context: Context) -> NSTextField {
  9. let textField = NSTextField(wrappingLabelWithString: text)
  10. textField.isBezeled = false
  11. textField.isEditable = false
  12. textField.isSelectable = true
  13. textField.backgroundColor = .clear
  14. textField.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
  15. return textField
  16. }
  17. func updateNSView(_ nsView: NSTextField, context: Context) {
  18. nsView.stringValue = text
  19. nsView.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
  20. }
  21. }

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:

  1. struct SelectableTextView: NSViewRepresentable {
  2. let text: String
  3. let font: NSFont?
  4. init(text: String, font: NSFont? = nil) {
  5. self.text = text
  6. self.font = font
  7. }
  8. func makeNSView(context: Context) -> NSTextView {
  9. let textView = NSTextView(frame: .zero)
  10. textView.isEditable = false
  11. textView.isSelectable = true
  12. textView.backgroundColor = .clear
  13. textView.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
  14. textView.string = text
  15. textView.selectedTextAttributes = [
  16. NSAttributedString.Key.foregroundColor : NSColor.systemGreen, // Change the text color
  17. NSAttributedString.Key.backgroundColor: NSColor.systemRed// Change the background color
  18. ]
  19. return textView
  20. }
  21. func updateNSView(_ nsView: NSTextView, context: Context) {
  22. nsView.string = text
  23. nsView.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
  24. }
  25. }
英文:

If you meant NSTextView, here is a possible approach:

  1. struct SelectableTextView: NSViewRepresentable {
  2. let text: String
  3. let font: NSFont?
  4. init(text: String, font: NSFont? = nil) {
  5. self.text = text
  6. self.font = font
  7. }
  8. func makeNSView(context: Context) -> NSTextView {
  9. let textView = NSTextView(frame: .zero)
  10. textView.isEditable = false
  11. textView.isSelectable = true
  12. textView.backgroundColor = .clear
  13. textView.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
  14. textView.string = text
  15. textView.selectedTextAttributes = [
  16. NSAttributedString.Key.foregroundColor : NSColor.systemGreen, // Change the text color
  17. NSAttributedString.Key.backgroundColor: NSColor.systemRed// Change the background color
  18. ]
  19. return textView
  20. }
  21. func updateNSView(_ nsView: NSTextView, context: Context) {
  22. nsView.string = text
  23. nsView.font = font ?? NSFont.systemFont(ofSize: NSFont.systemFontSize)
  24. }
  25. }

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:

确定