I can't seem to find out how to create a emoji keyboard in swiftui. Is there someway to build a textfield that is attached to a keyboard with just emojis?
Asked
Active
Viewed 2,596 times
-3
-
You have to use UIKit for that and make UIViewRepresentable. By doing this you can use your Textfield in SwiftUI. For make Emoji Keyboard Textfield you can refer this answer - [https://stackoverflow.com/questions/11382753/change-the-keyboard-layout-to-emoji/44753740#44753740](https://stackoverflow.com/questions/11382753/change-the-keyboard-layout-to-emoji/44753740#44753740) For making a UIViewRepresentale class you can get help from here - [https://gist.github.com/unnamedd/9346e1b0205ed6e2d09bf4709229a029](https://gist.github.com/unnamedd/9346e1b0205ed6e2d09bf4709229a029) – shraddha11 Jan 21 '20 at 05:02
1 Answers
4
Please try this. Hope it helps.
import SwiftUI
struct EmojiTF: 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)
}
.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 {
EmojiTF()
}
}

shraddha11
- 783
- 4
- 17
-
-
Okay. I think there is already some class available with Cordinator Name. I have updated my code please try this. Sorry for inconvenience. – shraddha11 Jan 22 '20 at 05:09
-
@shraddha11, is there any solution for this question also, https://stackoverflow.com/q/71234010/15307343 – Feb 23 '22 at 16:07