Not getting callback after tapping "Insert text" button in text on camera for UITextField

I have implemented Text on camera feature in two ways

  1. UITextField with UITextContentType and UIKeyboardType as emailAddress, which helps to show a button in the keyboard to open the camera for capturing email.
  2. UITextField with basic text. I implemented a custom UIButton to open the camera for capturing text for this field.

In both cases I'm not getting callback in any of these functions insertText(_ text) setMarkedText(markedText:, selectedRange:) and unmarkText() after user taps Insert Text button.

Help me.

Replies

Have you set the delegates as needed for the UITextFields ?

Eventually, show the code.

  • Yes I have set the delegates too but that also don't get any response.

    `import UIKit

    class ViewController: UIViewController {          override func viewDidLoad() {         super.viewDidLoad()                  setupTextField()     }          func setupTextField() {         let emailTextField = TextField(frame: CGRect(x: 150, y: 150, width: 150, height: 40))         emailTextField.textContentType = .emailAddress         emailTextField.keyboardType = .emailAddress         emailTextField.autocorrectionType = .no         emailTextField.layer.borderWidth = 1         emailTextField.delegate = self         self.view.addSubview(emailTextField)                  let textField = TextField(frame: CGRect(x: 150, y: 250, width: 150, height: 40))         textField.layer.borderWidth = 1         textField.delegate = self         textField.rightViewMode = .always         self.view.addSubview(textField)                  let button = UIButton(primaryAction: UIAction.captureTextFromCamera(responder: textField, identifier: nil))         button.frame = CGRect(x: 150, y: 300, width: 150, height: 60)         self.view.addSubview(button)     } }

    extension ViewController: UITextFieldDelegate {     func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {         print("Should change in chars delegate")         print(textField.text)         return true     }

        func textFieldDidEndEditing(_ textField: UITextField) {         print("Did end editing delegate")         print(textField.text)     } }

    class TextField: UITextField {     override func insertText(_ text: String) {         super.insertText(text)         print("Insert tet function")         print(text)     }

        override func unmarkText() {         super.unmarkText()         print("Unmark text function")         print(self.text)     }

        override func setMarkedText(_ markedText: String?, selectedRange: NSRange) {         super.setMarkedText(markedText, selectedRange: selectedRange)         print("Marked text function")         print(markedText)     } }`

  • `import UIKit

    class ViewController: UIViewController {

        

        override func viewDidLoad() {

            super.viewDidLoad()

            

            setupTextField()

        }

        

        func setupTextField() {

            let emailTextField = TextField(frame: CGRect(x: 150, y: 150, width: 150, height: 40))

            emailTextField.textContentType = .emailAddress

            emailTextField.keyboardType = .emailAddress

            emailTextField.autocorrectionType = .no

            emailTextField.layer.borderWidth = 1

            emailTextField.delegate = self

            self.view.addSubview(emailTextField)

            

            let textField = TextField(frame: CGRect(x: 150, y: 250, width: 150, height: 40))

            textField.layer.borderWidth = 1

            textField.delegate = self

            textField.rightViewMode = .always

            self.view.addSubview(textField)

            

            let button = UIButton(primaryAction: UIAction.captureTextFromCamera(responder: textField, identifier: nil))

            button.frame = CGRect(x: 150, y: 300, width: 150, height: 60)

            self.view.addSubview(button)

        }

    }

    extension ViewController: UITextFieldDelegate {

        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

            print("Should change in chars delegate")

            print(textField.text)

            return true

        }

        

        func textFieldDidEndEditing(_ textField: UITextField) {

            print("Did end editing delegate")

            print(textField.text)

        }

    }

    class TextField: UITextField

    {

        override func insertText(_ text: String) {

            super.insertText(text)

            print("Insert tet function")

            print(text)

        }

        

        override func unmarkText() {

            super.unmarkText()

            print("Unmark text function")

            print(self.text)

        }

        

        override func setMarkedText(_ markedText: String?, selectedRange: NSRange) {

            super.setMarkedText(markedText, selectedRange: selectedRange)

            print("Marked text function")

            print(markedText)

        }

    }`

  • Showing a code in comments would not seem to be a good idea. You may need to use Your Answer to show codes well-formatted. Please check if the code is sufficient to reproduce the issue.

Add a Comment



class ViewController: UIViewController {

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        setupTextField()

    }

    

    func setupTextField() {

        let emailTextField = TextField(frame: CGRect(x: 150, y: 150, width: 150, height: 40))

        emailTextField.textContentType = .emailAddress

        emailTextField.keyboardType = .emailAddress

        emailTextField.autocorrectionType = .no

        emailTextField.layer.borderWidth = 1

        emailTextField.delegate = self

        self.view.addSubview(emailTextField)

        

        let textField = TextField(frame: CGRect(x: 150, y: 250, width: 150, height: 40))

        textField.layer.borderWidth = 1

        textField.delegate = self

        textField.rightViewMode = .always

        self.view.addSubview(textField)

        

        let button = UIButton(primaryAction: UIAction.captureTextFromCamera(responder: textField, identifier: nil))

        button.frame = CGRect(x: 150, y: 300, width: 150, height: 60)

        self.view.addSubview(button)

    }

}



extension ViewController: UITextFieldDelegate {

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        print("Should change in chars delegate")

        print(textField.text)

        return true

    }

    

    func textFieldDidEndEditing(_ textField: UITextField) {

        print("Did end editing delegate")

        print(textField.text)

    }

}



class TextField: UITextField

{

    override func insertText(_ text: String) {

        super.insertText(text)

        print("Insert tet function")

        print(text)

    }

    

    override func unmarkText() {

        super.unmarkText()

        print("Unmark text function")

        print(self.text)

    }

    

    override func setMarkedText(_ markedText: String?, selectedRange: NSRange) {

        super.setMarkedText(markedText, selectedRange: selectedRange)

        print("Marked text function")

        print(markedText)

    }

}
  • Thanks for showing your code. I could have confirmed that the delegates of the two text fields are connected properly and works as expected on manual key touches. In my opinion, it is a sort of bug that Text on Camera not calling delegate methods. You should better send a bug report. (On the other hand, it is a known issue that insertText and some other methods do not call delegate methods.)

  • Thanks. I have filed a bug report. Now in beta 3 "Email fro camera" button which shown on keyboard is not showing now.

  • Hi, i'm also not getting the callback for insertText(_ text) setMarkedText(markedText:, selectedRange:) and unmarkText() for UITextField. Is it a bug? any updates would be appreciated. Thanks!

Add a Comment

I edited the first comment to make it readable.

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTextField()
    }
    
    func setupTextField() {
        let emailTextField = TextField(frame: CGRect(x: 150, y: 150, width: 150, height: 40))
        emailTextField.textContentType = .emailAddress
        emailTextField.keyboardType = .emailAddress
        emailTextField.autocorrectionType = .no
        emailTextField.layer.borderWidth = 1
        emailTextField.delegate = self
        self.view.addSubview(emailTextField)

        let textField = TextField(frame: CGRect(x: 150, y: 250, width: 150, height: 40))
        textField.layer.borderWidth = 1
        textField.delegate = self
        textField.rightViewMode = .always
        self.view.addSubview(textField)

        let button = UIButton(primaryAction: UIAction.captureTextFromCamera(responder: textField, identifier: nil))
        button.frame = CGRect(x: 150, y: 300, width: 150, height: 60)
        self.view.addSubview(button)
    }
}

extension ViewController: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        print("Should change in chars delegate")
        print(textField.text)
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        print("Did end editing delegate")
        print(textField.text)
    }
}

class TextField: UITextField {
    override func insertText(_ text: String) {
        super.insertText(text)
        print("Insert tet function")
        print(text)
    }
    
    override func unmarkText() {
        super.unmarkText()
        print("Unmark text function")
        print(self.text)
    }
    
    override func setMarkedText(_ markedText: String?, selectedRange: NSRange) {
        super.setMarkedText(markedText, selectedRange: selectedRange)
        print("Marked text function")
        print(markedText)
    }
}

What do you get on console ? Do

        print("Should change in chars delegate")

or

        print("Insert tet function")

get printed ?

Did you read this thread, maybe there's some interesting info there:

https://developer.apple.com/forums/thread/682090

  • I get nothing on the console. none of these print are called (which is my concern), even shouldChangeCharactersIn is not called.

    Yes, I read that thread but it's for implementing text on camera on a custom view.

    Since UITextField has already inherited UITextInput there is no way to inherit it again.

  • I tested this code (not with a camera view, but a plain UIViewController). shouldChangeCharactersIn is properly. Could you show how you use the class on your camera view ?

Add a Comment