SwiftUI:如何编辑 FocusRing 形状或禁用它?
SwiftUI: How to edit FocusRing shape or disable it?
如何编辑 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)
}
}
}
}
和 addBorder 扩展(不需要代码):
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)
)
}
}
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 = [=10=] }
}
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)
}
}
}
}
以下代码为它所在的文件内的所有文本字段禁用 focusRing
//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 { }
}
}
如何编辑 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)
}
}
}
}
和 addBorder 扩展(不需要代码):
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)
)
}
}
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 = [=10=] }
}
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)
}
}
}
}
以下代码为它所在的文件内的所有文本字段禁用 focusRing
//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 { }
}
}