Как создать собственный компонент, который принимает (и передает) все возможные аргументы TextField? ⇐ IOS
-
Anonymous
Как создать собственный компонент, который принимает (и передает) все возможные аргументы TextField?
I currently have following custom component that wraps and styles a default TextField from swift ui. I came across an issue where I now need to re-use this where @Binding is no longer just String, but rather, can be a number like int or double, thus underlying TextField needs to change and take in TextField(value: ..., formatter: ...) instead of just text.
I can turn all of the modifiers into a custom modifier and apply it to relevant text fields to apply the styling, but am wondering if there is a solution where I can keep this as a custom component and instead allow TextInputSmall to take in and pass down all possible permutations of TextField arguments, for example
TextInputSmall("", text: $someString) and TextInputSmall("", value: $someNumber, formatter: .number)
struct TextInputSmall: View { // Public Variables var accentColor = Color._orange @Binding var text: String // Private Variables @FocusState private var focused: Bool // Body var body: some View { TextField( "", text: $text ) .font(...) .foregroundStyle(...) .accentColor(...) .focused($focused) .submitLabel(.done) .frame(...) .padding(...) .background(...) } }
Источник: https://stackoverflow.com/questions/780 ... sible-text
I currently have following custom component that wraps and styles a default TextField from swift ui. I came across an issue where I now need to re-use this where @Binding is no longer just String, but rather, can be a number like int or double, thus underlying TextField needs to change and take in TextField(value: ..., formatter: ...) instead of just text.
I can turn all of the modifiers into a custom modifier and apply it to relevant text fields to apply the styling, but am wondering if there is a solution where I can keep this as a custom component and instead allow TextInputSmall to take in and pass down all possible permutations of TextField arguments, for example
TextInputSmall("", text: $someString) and TextInputSmall("", value: $someNumber, formatter: .number)
struct TextInputSmall: View { // Public Variables var accentColor = Color._orange @Binding var text: String // Private Variables @FocusState private var focused: Bool // Body var body: some View { TextField( "", text: $text ) .font(...) .foregroundStyle(...) .accentColor(...) .focused($focused) .submitLabel(.done) .frame(...) .padding(...) .background(...) } }
Источник: https://stackoverflow.com/questions/780 ... sible-text
Мобильная версия