iOS开发SwiftUI

Swift UITextField 文字Padding

//UITextField : override textRect, editingRect
class LeftPaddedTextField: UITextField {

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: bounds.origin.x + 15, y: bounds.origin.y, width: bounds.width, height: bounds.height)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: bounds.origin.x + 15, y: bounds.origin.y, width: bounds.width, height: bounds.height)
    }

}
struct LoginTextfield: UIViewRepresentable {
    var placeHoder: String
    @Binding var text: String
    @Binding var myTextField: LeftPaddedTextField
    
    func makeUIView(context: Context) -> LeftPaddedTextField {
        let textfield = LeftPaddedTextField()
        textfield.keyboardType = .default
        textfield.textAlignment = .left
        textfield.placeholder = placeHoder
        textfield.delegate = context.coordinator
        textfield.text = text
        textfield.backgroundColor = uiColorF8F8F8
        textfield.layer.borderColor = cgColorE3E3E3
        textfield.layer.cornerRadius = 8
        textfield.layer.borderWidth = 1

        myTextField.text = text

        return textfield
    }

    func updateUIView(_ uiView: LeftPaddedTextField, context: Context) {
        // uiView.text = text
    }

    func makeCoordinator() -> LoginTextfield.Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject, UITextFieldDelegate {
        var control: LoginTextfield

        init(_ control: LoginTextfield) {
            self.control = control
        }

        func textFieldDidChangeSelection(_ textField: UITextField) {
            self.control.text = textField.text!
        }

        private func textFieldShouldBeginEditing(_ textField: LeftPaddedTextField) -> Bool {
            self.control.myTextField = textField
            return true
        }

        private func textFieldShouldEndEditing(_ textField: LeftPaddedTextField) -> Bool {
            self.control.text = textField.text!
            textField.endEditing(true)
            return true
        }

    }
}