SwiftUI:如何编辑FocusRing形状或禁用它?

时间:2019-11-25 17:14:20

标签: border swiftui

如何编辑FocusRing形状

还是如何禁用它?

enter image description here

generator

和addBorder扩展名(不需要代码):

ProcessCTL

2 个答案:

答案 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 { }
    }
}