Cheerful Godwin
Cheerful Godwin

Reputation: 51

So. I'm trying to get the character count under my textfield to increase on the screen but it's not incrementing

So. I'm trying to get the character count under my textfield to increase on the screen but it's not incrementing.I've been trying to make it work for the last couple of hours and can't seem to figure it out. Is there some way to make this work the way I want it to? Or is this not going to work because I'm using the TextFieldWrapperView?

import SwiftUI

struct ContentView: View {

    @State var emojiText = ""


    var body: some View {
        VStack(spacing: 10) {
            Text("Emoji Pad:")
                .font(.body)
                .foregroundColor(.gray)

            TextFieldWrapperView(text: self.$emojiText)
                .background(Color.gray)
                .frame(width: 200, height: 50)
            Text("\(emojiText.count)")
        }
        .frame(height: 40)
    }
}


struct TextFieldWrapperView: UIViewRepresentable {

    @Binding var text: String

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

extension TextFieldWrapperView {


    func makeUIView(context: UIViewRepresentableContext<TextFieldWrapperView>) -> UITextField {
        let textField = EmojiTextField()
        textField.delegate = context.coordinator
        return textField
    }


    func updateUIView(_ uiView: UITextField, context: Context) {

    }
}

class TFCoordinator: NSObject, UITextFieldDelegate {
    var parent: TextFieldWrapperView

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

    //        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    //            if let value = textField.text {
    //                parent.text = value
    //                parent.onChange?(value)
    //            }
    //
    //            return true
    //        }
}


class EmojiTextField: UITextField {

    // required for iOS 13
    override var textInputContextIdentifier: String? { "" } // return non-nil to show the Emoji keyboard ¯\_(ツ)_/¯

    override var textInputMode: UITextInputMode? {
        for mode in UITextInputMode.activeInputModes {
            if mode.primaryLanguage == "emoji" {
                return mode
            }
        }
        return nil
    }
}

struct EmojiTF_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 0

Views: 175

Answers (1)

Hrabovskyi Oleksandr
Hrabovskyi Oleksandr

Reputation: 3275

You leaved code commented in UITextFieldDelegate, but it is just what you needed. I update it a little, because there is mistake, when you delete text from textfield. Just replace your class TFCoordinator with this:

class TFCoordinator: NSObject, UITextFieldDelegate {
    var parent: TextFieldWrapperView

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

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

        if let value = textField.text, charIsBackSpace(string) {
            let stringWithoutLastChar = value.dropLast()
            parent.text = String(stringWithoutLastChar)
        } else if let value = textField.text {
            parent.text = value + string
        } else {
            parent.text = string
        }

        return true
    }

    private func charIsBackSpace(_ string: String) -> Bool {
        guard let char = string.cString(using: String.Encoding.utf8) else { return false }
        let charCode = strcmp(char, "\\b")
        let backSpaceCode = -92
        return charCode == backSpaceCode
    }

}

Upvotes: 1

Related Questions