https://github.com/netceteragroup/swiftui-transformingtextfield
Applies a transformation to characters as they're typed into a SwiftUI TextField or TextEditor, while managing its cursor position.
https://github.com/netceteragroup/swiftui-transformingtextfield
cursor modifier swiftui texteditor textfield uitextfield uitextview
Last synced: 6 days ago
JSON representation
Applies a transformation to characters as they're typed into a SwiftUI TextField or TextEditor, while managing its cursor position.
- Host: GitHub
- URL: https://github.com/netceteragroup/swiftui-transformingtextfield
- Owner: netceteragroup
- Created: 2023-07-24T15:38:25.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-22T14:29:53.000Z (4 months ago)
- Last Synced: 2025-01-22T15:30:32.550Z (4 months ago)
- Topics: cursor, modifier, swiftui, texteditor, textfield, uitextfield, uitextview
- Language: Swift
- Homepage:
- Size: 2.85 MB
- Stars: 9
- Watchers: 7
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SwiftUI-TransformingTextField
I love SwiftUI! ❤️
While it simplifies the way we build apps, it (currently) has some limitations and even some strange quirks. For example, manipulating a `TextField`'s text during editing causes the cursor to jump uncontrollably, and you really don't want to expose your users to that kind of madness. 🤪
Enter...
## TransformingTextFieldModifier

`TransformingTextFieldModifier` applies a transformation to characters as they're typed into a `TextField`.
It uses view introspection (thanks to [SwiftUI-Introspect](https://github.com/siteline/SwiftUI-Introspect)) to find the underlying `UITextField` (so it can even be applied to custom `UIViewRepresentable`s that wrap a `UITextField`). 🎁It can be used via convenient `View` extensions, which **can be combined**:
```swift
TextField(...)
.transformingChanges(in: $text) { text, range, replacement in
replacement.filter(\.isLetter)
}
.uppercased(text: $text)
.characterLimit(6, in: $text)```
## New in 1.1.0
Added support for `TextEditor` and `TextField` with vertical axis on iOS 16 (both backed by a `UITextView`).
## TransformingTextFieldDelegate
`TransformingTextFieldDelegate` is a `UITextFieldDelegate` that takes over the task of updating the text field's text, applying the given transformation to any text changes, and preserving the cursor's logical position.
More importantly, this works around SwiftUI's current undesirable behavior of throwing the cursor to the end of the field whenever the text buffer is modified programmatically.
# Installation
Add https://github.com/netceteragroup/SwiftUI-TransformingTextField.git to your Swift package dependencies and `import TransformingTextField`.
For a quick demo, try the SwiftUI preview in `TransformingTextField.swift`. You can also run it by dropping `TransformingTextField_Previews.Example()` into your `ContentView` while in DEBUG mode.
```swift
var body: some View {
TransformingTextField_Previews.Example()
}
```> SwiftUI-TransformingTextField was created by [Vikram Kriplaney](https://github.com/markiv) in July 2023.