I'm wrapping a UITextField in a SwiftUI view using UIViewRepresentable protocol; I'd like to pass a LocalizedStringKey to the underlying UITextField 's placeholder . Something like:
An extension to avoid hardcode strings and easily lookup all localizable strings in the code:
extension LocalizedStringKey {
let search: LocalizedStringKey = "search"
}
The custom text field:
struct SuperTextField: UIViewRepresentable {
let placeholder: LocalizedStringKey
// all UIViewRepresentable methods
func updateUIView(_ uiView: UITextField, context: Context) {
uiView.placeholder = placeholder // of course this doesn't compile as textField placeholder is a simple `String`
}
}
The usage I'd like to have:
SuperTextField(placeholder: .search) // use auto infer
I don't think at the moment this is possible as LocalizedStringKey doesn't expose the string used to initialize it... Any other good approach?
2 Likes