在 UITextField 与 UIViewRepresentable 和 SwifUI 之间绑定文本时出现问题
Issue when binding text between UITextField with UIViewRepresentable and SwifUI
我正在尝试在 SwiftUI 中创建一个 'emoji picker',它会弹出表情符号键盘,允许用户 select 表情符号,然后关闭键盘。我正在使用 UITextField 包装在带有字符串绑定的 UIViewRepresntable 中,但是由于某种原因,字符串的值永远不会更新。
这是我目前的代码:
/// Allows a user to pick an emoji character using the Emoji keyboard.
/// - Note: This does not prevent the user from manually switching to other keyboards and inputting a non-Emoji character
struct EmojiPicker: UIViewRepresentable {
@Binding var emoji: String
func makeUIView(context: UIViewRepresentableContext<EmojiPicker>) -> EmojiUITextField {
let textField = EmojiUITextField(frame: .zero)
textField.text = emoji
textField.delegate = context.coordinator
textField.autocorrectionType = .no
textField.returnKeyType = .done
textField.textAlignment = .center
textField.tintColor = .clear
return textField
}
func updateUIView(_ uiView: EmojiUITextField, context: Context) {
self.emoji = uiView.text!
}
func makeCoordinator() -> EmojiTextFieldCoordinator {
return EmojiTextFieldCoordinator(self)
}
}
internal class EmojiTextFieldCoordinator: NSObject, UITextFieldDelegate {
var emojiTextField: EmojiPicker
init(_ textField: EmojiPicker) {
self.emojiTextField = textField
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.emojiTextField.emoji = textField.text!
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
textField.text = string
if let text = textField.text, text.count == 1 {
self.emojiTextField.emoji = textField.text!
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
return true
}
}
internal class EmojiUITextField: UITextField {
override var textInputContextIdentifier: String? {
return ""
}
override var textInputMode: UITextInputMode? {
return UITextInputMode.activeInputModes.first {
[=10=].primaryLanguage == "emoji"
}
}
override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
return []
}
}
这是修复,唯一修改的部分,(用Xcode 12 / iOS 14测试)
func updateUIView(_ uiView: EmojiUITextField, context: Context) {
if self.emoji != uiView.text! { // << update only on change, otherwise
self.emoji = uiView.text! // it result in cycle and dropped
}
}
这是一个用于测试的视图
struct ContentView: View {
@State private var text = "<none>"
var body: some View {
VStack {
Text("Get: \(text)")
EmojiPicker(emoji: $text)
}
}
}
我正在尝试在 SwiftUI 中创建一个 'emoji picker',它会弹出表情符号键盘,允许用户 select 表情符号,然后关闭键盘。我正在使用 UITextField 包装在带有字符串绑定的 UIViewRepresntable 中,但是由于某种原因,字符串的值永远不会更新。
这是我目前的代码:
/// Allows a user to pick an emoji character using the Emoji keyboard.
/// - Note: This does not prevent the user from manually switching to other keyboards and inputting a non-Emoji character
struct EmojiPicker: UIViewRepresentable {
@Binding var emoji: String
func makeUIView(context: UIViewRepresentableContext<EmojiPicker>) -> EmojiUITextField {
let textField = EmojiUITextField(frame: .zero)
textField.text = emoji
textField.delegate = context.coordinator
textField.autocorrectionType = .no
textField.returnKeyType = .done
textField.textAlignment = .center
textField.tintColor = .clear
return textField
}
func updateUIView(_ uiView: EmojiUITextField, context: Context) {
self.emoji = uiView.text!
}
func makeCoordinator() -> EmojiTextFieldCoordinator {
return EmojiTextFieldCoordinator(self)
}
}
internal class EmojiTextFieldCoordinator: NSObject, UITextFieldDelegate {
var emojiTextField: EmojiPicker
init(_ textField: EmojiPicker) {
self.emojiTextField = textField
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.emojiTextField.emoji = textField.text!
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
textField.text = string
if let text = textField.text, text.count == 1 {
self.emojiTextField.emoji = textField.text!
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
return true
}
}
internal class EmojiUITextField: UITextField {
override var textInputContextIdentifier: String? {
return ""
}
override var textInputMode: UITextInputMode? {
return UITextInputMode.activeInputModes.first {
[=10=].primaryLanguage == "emoji"
}
}
override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
return []
}
}
这是修复,唯一修改的部分,(用Xcode 12 / iOS 14测试)
func updateUIView(_ uiView: EmojiUITextField, context: Context) {
if self.emoji != uiView.text! { // << update only on change, otherwise
self.emoji = uiView.text! // it result in cycle and dropped
}
}
这是一个用于测试的视图
struct ContentView: View {
@State private var text = "<none>"
var body: some View {
VStack {
Text("Get: \(text)")
EmojiPicker(emoji: $text)
}
}
}