Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

textFieldDidBeginEditing and SecureField #316

Unanswered
hoangnam714 asked this question in Q&A
Discussion options

how can get SecureField start editing event

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

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:

You must be logged in to vote
1 reply
Comment options

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
 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /