答案 0 :(得分:1)
没有FocusRing聚焦的自定义字段代码。
总比没有好:)
struct NoFocusTextField: NSViewRepresentable {
@Binding var text: String
init(text: Binding<String>) {
_text = text
}
func makeNSView(context: Context) -> NSTextField {
let textField = NSTextField(string: text)
textField.delegate = context.coordinator
textField.isBordered = false
textField.backgroundColor = nil
textField.focusRingType = .none
return textField
}
func updateNSView(_ nsView: NSTextField, context: Context) {
nsView.stringValue = text
}
func makeCoordinator() -> Coordinator {
Coordinator { self.text = $0 }
}
final class Coordinator: NSObject, NSTextFieldDelegate {
var setter: (String) -> Void
init(_ setter: @escaping (String) -> Void) {
self.setter = setter
}
func controlTextDidChange(_ obj: Notification) {
if let textField = obj.object as? NSTextField {
setter(textField.stringValue)
}
}
}
}
答案 1 :(得分:0)
以下代码禁用它所在文件内所有 textFields 的 focusRing
useEffect(() => {
loadnewmessages();
statusListener();
}, []);
或
//Disable focusRing inside the FILE
fileprivate extension NSTextField {
override var focusRingType: NSFocusRingType {
get { .none }
set { }
}
}