![](/img/trans.png)
[英]How to use CAEmitterLayer on macOS in a SwiftUI app using NSViewRepresentable
[英]Handle keyboard inputs in NSTextView embedded via NSViewRepresentable? (SwiftUI/MacOS)
我是 SwiftUI 的新手,我完全糊涂了。我设法将 NSTextView 嵌入到我的 SwiftUI 视图中,并将其文本与以下代码绑定。
我不明白的; 有没有办法处理NSTextView 的键盘输入并相应地更改其文本(例如 CMD + R 将所选文本的文本颜色设置为红色)? 是否有任何方法可以与 SwiftUI 中的 UI 元素进行交互?
“富文本字段”
struct RichTextField: NSViewRepresentable {
typealias NSViewType = NSTextView
@Binding var attributedString: NSAttributedString
func makeNSView(context: Context) -> NSTextView {...
// [...]
}
看法
struct EditWindow: View {
@ObservedObject var model: EditEntryViewModel
@Binding var isPresented: Bool
var body: some View {
RichTextField(attributedString: self.$model.answer1, isEditable: true)
// [...]
}
}
此外,我已经设法在 AppDelegate 中设置了一个菜单命令,但是我如何使用它来更改任意视图的 NSTextView 中的文本(在某个位置)?
@IBAction func setTagImportant(_ sender: Any) {
print("setTagImportant")
}
非常感谢您为我阐明了这一点...
具有讽刺意味的是,在最终发布此问题后,我立即找到了解决方案; 简单地将 NSTextView 子类化,然后覆盖 keyDown:
import SwiftUI
class RichTextFieldExtended: NSTextView {
override func keyDown(with event: NSEvent) {
if event.modifierFlags.contains(NSEvent.ModifierFlags.command) {
switch event.keyCode {
case 18: // 1
print("1 PRESSED")
default:
print("keyCode \(event.keyCode) wasn't handled")
super.keyDown(with: event)
}
} else {
super.keyDown(with: event)
}
}
}
然后在NSViewRepresentable中包含子类的NSTextView,如下
struct RichTextField: NSViewRepresentable {
typealias NSViewType = RichTextFieldExtended
@Binding var attributedString: NSAttributedString
var isEditable: Bool
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeNSView(context: Context) -> RichTextFieldExtended {
let textView = RichTextFieldExtended(frame: .zero)
textView.textStorage?.setAttributedString(self.attributedString)
textView.isEditable = isEditable
textView.delegate = context.coordinator
textView.translatesAutoresizingMaskIntoConstraints = false
textView.autoresizingMask = [.width, .height]
return textView
}
func updateNSView(_ nsView: RichTextFieldExtended, context: Context) {
// nsView.textStorage!.setAttributedString(self.attributedString)
}
// Source: https://medium.com/fantageek/use-xib-de9d8a295757
class Coordinator: NSObject, NSTextViewDelegate {
let parent: RichTextField
init(_ RichTextField: RichTextField) {
self.parent = RichTextField
}
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? RichTextFieldExtended else { return }
self.parent.attributedString = textView.attributedString()
}
}
}
干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.