-
Notifications
You must be signed in to change notification settings - Fork 389
textFieldDidBeginEditing and SecureField #316
-
how can get SecureField start editing event
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
SecureField
is effectively the same UIKit type as TextField
, so you can use:
.introspect(.textField, on: .iOS(.v13, .v14, .v15, .v16, .v17)) { textField in ... }
I'll make sure to implement a .secureField
view type to make this more intuitive.
As to how to hook into textFieldDidBeginEditing
, if you're targeting iOS 15 or above you can simply use the @FocusState
APIs and observe when a particular focus is set via .onChange(of:perform:)
. You don't need introspection at all for this. Learn more: https://swiftwithmajid.com/2021/08/24/mastering-focusstate-property-wrapper-in-swiftui/
If you're targeting under iOS 15, your best bet is overriding the UITextField
delegate's textFieldDidBeginEditing
either by constructing a delegate of your own or by swizzling the existing delegate (if any). This is a more advanced topic that this library doesn't cover as it's out of scope, but you can take inspiration from e.g. my navigation transitions library:
- Custom delegate: https://github.com/davdroman/swiftui-navigation-transitions/blob/main/Sources/NavigationTransitions/NavigationTransitionDelegate.swift
- Delegate override: https://github.com/davdroman/swiftui-navigation-transitions/blob/f31e498e1b457c41c36795eb1b9923393aca5cbb/Sources/NavigationTransitions/NavigationTransition%2BUIKit.swift#L126-L132
- Where does that weird
self[]
syntax come from?: https://github.com/davdroman/swiftui-navigation-transitions/tree/main/Sources/RuntimeAssociation
Beta Was this translation helpful? Give feedback.
All reactions
-
🚀 1
-
i test custom delegate, but i have problem when using ObseverObject stateObject doesn't get the same error, app crash in "private weak var delegate: UITextFieldDelegate?" with error EXC_BAD_ACCESS (code=2, address=0x16f59ffd0), below is my code:
@ObservedObject var customTextFieldDelegate = CustomTextFieldDelegate()
TextField("", text: $text)
.font(defaultTextFont)
.multilineTextAlignment(getAlignment())
.accentColor(accentColor)
.keyboardType(keyboardType)
.disableAutocorrection(true)
.autocapitalization(.none)
.introspect(.textField, on: .iOS(.v13,.v14,.v15,.v16)) { textField in
customTextFieldDelegate.textField = textField
}
class CustomTextFieldDelegate: NSObject, ObservableObject, UITextFieldDelegate {
@Published private(set) var isEditing: Bool = false
var textField: UITextField? {
didSet {
if delegate == nil, let delegate = textField?.delegate {
textField?.delegate = self
self.delegate = delegate
}
}
}
private weak var delegate: UITextFieldDelegate?
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
guard let delegate = delegate, let textFieldShouldBeginEditing = delegate.textFieldShouldBeginEditing?(textField) else {
return true
}
return textFieldShouldBeginEditing
}
func textFieldDidBeginEditing(_ textField: UITextField) {
DispatchQueue.main.async {
self.isEditing = true
}
delegate?.textFieldDidBeginEditing?(textField)
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
guard let delegate = delegate, let textFieldEndEditing = delegate.textFieldShouldEndEditing?(textField) else {
return true
}
return textFieldEndEditing
}
func textFieldDidEndEditing(_ textField: UITextField) {
DispatchQueue.main.async {
self.isEditing = false
}
delegate?.textFieldDidEndEditing?(textField)
}
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
DispatchQueue.main.async {
self.isEditing = false
}
delegate?.textFieldDidEndEditing?(textField, reason: reason)
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let delegate = delegate, let shouldChangeCharactersIn = delegate.textField?(textField, shouldChangeCharactersIn: range, replacementString: string) else {
return true
}
return shouldChangeCharactersIn
}
func textFieldDidChangeSelection(_ textField: UITextField) {
delegate?.textFieldDidChangeSelection?(textField)
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
guard let delegate = delegate, let textFieldShouldClear = delegate.textFieldShouldClear?(textField) else {
return false
}
return textFieldShouldClear
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
guard let delegate = delegate, let textFieldShouldReturn = delegate.textFieldShouldReturn?(textField) else {
return true
}
return textFieldShouldReturn
}
}
Beta Was this translation helpful? Give feedback.