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

问题描述 投票:0回答:2

如何编辑 FocusRing 形状

或者如何禁用它?

import SwiftUI

struct FilterFieldView : View{
    @State var filterStr: String = ""

    var body : some View{
        ZStack{
            TextField("Filter", text: $filterStr)
                .textFieldStyle(PlainTextFieldStyle())
                .padding(.vertical, 2)
                .multilineTextAlignment(.center)
                .background(Color.clear)
                .padding(.trailing, 20)
                // needed to set border color 
                // + set field border beyond the "x" button
                .addBorder(color: Color.gray, radius: 10, lineWidth: 1)

            HStack() {
                Spacer()
                Button("X"){ self.filterStr = "" }
                    .buttonStyle(PlainButtonStyle()) //remove default button style
                    .padding(3)
                    .background(
                        Circle()
                        .stroke(Color.gray, lineWidth: 1)
                    )
                    .padding(.horizontal, 3)



            }
        }
    }
}

并添加边框扩展(不是必需的代码):

extension View {
    func addBorder(color: Color, radius: Int, lineWidth: Int) -> some View
    {
         self.modifier( CustomBorder(color: color, radius: CGFloat(radius), lineWidth: CGFloat(lineWidth) ) )
    }
}


struct CustomBorder: ViewModifier {
    @State var color: Color
    @State var radius: CGFloat
    @State var lineWidth: CGFloat

    func body (content: Content) -> some View
    {
        content
            .overlay(
                RoundedRectangle(cornerRadius: radius)
                    .stroke(color, lineWidth: lineWidth)
            )

    }
}
border swiftui
2个回答
3
投票

没有 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)
            }
        }
    }
}

2
投票
//Disable focusRing inside the FILE
fileprivate extension NSTextField {
    override var focusRingType: NSFocusRingType {
            get { .none }
            set { }
    }
}

//Disable focusRing inside PROJECT
public extension NSTextField {
    override var focusRingType: NSFocusRingType {
            get { .none }
            set { }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.