Is it possible to change "return" key to "done" on keyboard in 2020 with SwiftUI?

Related searches

I didn't find any link or guide to change the "return" key to "done" when keyboard open for TextField in SwiftUI.

Is it possible now without customising UITextField?

If somebody is looking for wrapping UITextField in UIViewRepresentable then I have some code to share:

struct CustomTextField: UIViewRepresentable {

    let tag: Int
    let placeholder: String
    let keyboardType: UIKeyboardType
    let returnVal: UIReturnKeyType

    @Binding var text: String
    @Binding var activeFieldTag: Int?
    var totalFields: Int = 0
    @Binding var isSecureTextEntry: Bool
    var textColor: UIColor = .pureWhite
    var font: UIFont = .nexaBold13
    var placeholderTextColor: UIColor = .pureWhite
    var placeholderFont: UIFont = .nexaLight13
    var onEditingChanged: (Bool) -> Void = { _ in }

    var lastActiveFieldTag: Int? {

        // Return, if no active field
        // (It also means textFieldShouldReturn not called yet OR called for last field)
        guard let activeFieldTag = activeFieldTag else {
            return nil
        }
        // Return previous field
        if activeFieldTag > 0 {
            return activeFieldTag - 1
        }
        // Return, if no previous field
        return nil
    }

    func makeUIView(context: Context) -> UITextField {
        let textField = UITextField(frame: .zero)
        textField.keyboardType = self.keyboardType
        textField.returnKeyType = self.returnVal
        textField.tag = self.tag
        textField.textColor = textColor
        textField.font = font
        textField.attributedPlaceholder = NSAttributedString(
            string: self.placeholder,
            attributes: [
                NSAttributedString.Key.foregroundColor: placeholderTextColor,
                NSAttributedString.Key.font: placeholderFont,
            ]
        )
        textField.delegate = context.coordinator
        textField.autocorrectionType = .no
        textField.isSecureTextEntry = isSecureTextEntry
        return textField
    }

    func updateUIView(_ textField: UITextField, context: Context) {
        if textField.text != self.text {
            textField.text = self.text
        }
        handleFirstResponder(textField)
        if textField.isSecureTextEntry != isSecureTextEntry {
            textField.isSecureTextEntry = isSecureTextEntry
        }
    }

    func handleFirstResponder(_ textField: UITextField) {

        // return if field is neither active nor last-active
        if tag != lastActiveFieldTag && tag != activeFieldTag {
            return
        }

        // return if field is already active
        if lastActiveFieldTag == activeFieldTag {
            return
        }

        // It creates problem in UI when we press the next button too fast and continuously on keyboard
        //        // Remove focus from last active field
        //        if lastActiveFieldTag == tag {
        //            uiView.removeFocus()
        //            return
        //        }

        // Give focus to active field
        if activeFieldTag == tag {
            textField.focus()
            return
        }
    }

    // Its called when pressing Next button on the keyboard
    // See textFieldShouldReturn
    func updateNextTag() {
        // There is no next field so set activeFieldTag to nil
        if tag + 1 == totalFields {
            activeFieldTag = nil
        } else {
            // Set next field tag as active
            activeFieldTag = tag + 1
        }
    }

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

    class Coordinator: NSObject, UITextFieldDelegate {

        var parent: CustomTextField

        init(_ textField: CustomTextField) {
            self.parent = textField
        }

        func updatefocus(textfield: UITextField) {
            textfield.focus()
        }

        func textFieldShouldReturn(_ textField: UITextField) -> Bool {

            // Give focus to next field
            parent.updateNextTag()
            parent.text = textField.text ?? ""

            // If there is no next active field then dismiss the keyboard
            if parent.activeFieldTag == nil {
                DispatchQueue.main.async {
                    textField.removeFocus()
                }
            } 

            return true
        }

        func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
            DispatchQueue.main.async {
                // To enable user to click on any textField while another is active
                self.parent.activeFieldTag = self.parent.tag
                self.parent.onEditingChanged(true)
            }
            return true
        }

        func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
            self.parent.text = textField.text ?? ""
            DispatchQueue.main.async {
                self.parent.onEditingChanged(false)
            }
            return true
        }

        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            if let text = textField.text, let rangeExp = Range(range, in: text) {
                self.parent.text = text.replacingCharacters(in: rangeExp, with: string)
            }
            return true
        }
    }
}

Is it possible to change a person?, No It's not possible to change a person. I was married to My Ex before 14 years. Now I am 36. When I was separated from him I was 31. Their family hide� Personality change might not be easy, and changing some broad traits might never really be fully possible. But researchers do believe that there are things you can do to change certain parts of your personality, the aspects that exist beneath the level of those broad traits, that can result in real changes to the way you act, think, and function in your day-to-day life.

Yes, just set the textField.returnKeyType = .next and it will display Next instead of Return

Do People Really Change?, The authors had an important caveat: It's possible that a person's personality may have changed as a result of the condition that brought the� Unfortunately, the answer is not that simple. According to most personality type theories, the individual’s type is inborn and does not change. However, individuals can develop traits and habits that differ or even directly contradict the description of their type. How does that happen?

To get the go button on the keyboard. Try changing the keyboardtype to .webSearch.

// Tested on Xcode 12 beta 2 and iOS 14

.keyboardType(.webSearch)

Can People Change Or Do They Just Lie?, It's possible for any person to change-if they truly work to. There are a few things that impact a person's ability and desire to make changes in life. The theory that personality change is possible, and achievable, was put to the test in a pair of experiments conducted by University of Illinois at Urbana-Champaign psychologists Nathan Hudson and

Psychologists Say It's Really Possible To Change Our Personality, According to most personality type theories, the individual's type is inborn and does not change. However, individuals can develop traits and habits that differ or � He must change for his own sake -- not to keep his wife or girlfriend tied to him. • Get help from someone other than his partner or ex-partner . Many men try unsuccessfully to change their habits through sheer will power.

Is It Possible to Change Your Personality Type?, If you want an accurate assessment of whether change is really possible, a better question to ask than "Can you stop drinking?" would be "Can you fundamentally � Does it really matter? If we truly believe that it is acceptable to have sexual and romantic relationships with the same sex, then it shouldn't matter whether or not sexual orientation is

Are people capable of change?, Individuals can change IQ scores. Your score may change not because of any real change in general intelligence, but that different tests may be used which measure different mixtures of abilities .

Comments
  • Does this answer your question? Change 'Return' button function to 'Done' in swift in UITextView
  • No, they are using UIKit and I am looking for an answer for SwiftUI
  • I am looking for stackoverflow.com/questions/58121756/… but without UIKit.
  • Why am I getting errors saying Type 'UIColor' has no member 'pureWhite' and Value of type 'UITextField' has no member 'focus'? Did something change
  • I don't see returnKeyType on TextField in the docs. Are you perhaps looking at UITextField (which is a different type)? Is this in the framework but missing from the docs?
  • Create a custom text field and assign the returnVal of type UIReturnKeyType to .next
  • What do you mean here by "create a custom text field?" There is no such thing in SwiftUI that I'm aware of. There is just the TextField struct, which has no returnKeyType modifier (and if it did, it wouldn't be by assignment; SwiftUI Views are generally immutable). I believe you're thinking of UIKit and UITextField, which is not what this question is about.
  • @RobNapier Posted an answer if you want to use UIViewRepresentable.