An open source package for as-you-type formatting in SwiftUI.
Feature | Description | |
---|---|---|
:keyboard: | Responsive | Formats text as you type |
:magic_wand: | Automagical | Binds text to appropriate types |
:heavy_check_mark: | Proper | Validates and autocorrects |
:hammer_and_wrench: | Versatile | Uses snapshots and attributes |
:running_man: | Performant | O(n) differentiation algorithms |
:desert_island: | Standalone | Uses no remote dependencies |
:open_book: | Open | 100% transparent |
How to install this package.
.package(url: "https://github.com/oscbyspro/DiffableTextViews", from: "5.0.0"),
Version | Swift | iOS | iPadOS | Mac Catalyst | tvOS |
---|---|---|---|---|---|
5.0.0+ | 5.7+ | 15.0+ | 15.0+ | 15.0+ | 15.0+ |
The src="https://raw.github.com/oscbyspro/DiffableTextViews/main/le app provides quick-and-easy-to-use customization toolssrc="https://raw.github.com/oscbyspro/DiffableTextViews/main/Number | Pattern |
---|---|
Download this package and compile/run it with Xcode.
A text field that binds values and formats them as you type.
Feature | Description | |
---|---|---|
:iphone: | SwiftUI | Value, style, done |
:mountain: | Environment | Uses environment values |
:mag_right: | Focus | Supports SwiftUI.FocusState |
environment(\.locale, _:)
environment(\.layoutDirection, _:)
diffableTextViews_autocorrectionDisabled(_:)
diffableTextViews_font(_:)
diffableTextViews_foregroundColor(_:)
diffableTextViews_multilineTextAlignment(_:)
diffableTextViews_onSubmit(_:)
diffableTextViews_submitLabel(_:)
diffableTextViews_textContentType(_:)
diffableTextViews_textFieldStyle(_:)
diffableTextViews_textInputAutocapitalization(_:)
diffableTextViews_tint(_:)
diffableTextViews_toolbarDoneButton(_:)
A style that binds localized numbers using various formats.
Feature | Description | |
---|---|---|
:coin: | Values | Decimal, Double, (U)Int(8-64) |
:grey_question: | Optional | Standard and optional values |
:art: | Formats | Number, currency and percent |
:bricks: | Bounds | Clamps values to bounds |
:bow_and_arrow: | Precision | Up to 38 digits of precision |
:national_park: | Locales | Supports Foundation.Locale |
:two: | Bilingual | Accepts local and ASCII input |
import DiffableTextViews
import SwiftUI
//*============================================================================*
// MARK: * Number [...]
//*============================================================================*
struct ContentView: View {
typealias Amount = Decimal // Decimal, Double, (U)Int(8-64), Optional<T>
//=------------------------------------------------------------------------=
@State var amount = 0 as Amount
@State var currencyCode = "SEK"
@State var locale = Locale(identifier: "sv_SE")
//=------------------------------------------------------------------------=
var body: some View {
DiffableTextField(value: $amount) {
.currency(code: currencyCode)
// .bounds((0 as Amount)...) // autocorrects while view is in focus
// .precision(integer: 1..., fraction: 2) // default is appropriate
// .locale(Locale(identifier: "en_US")).constant() // ignores sv_SE
}
.environment(\.locale, locale)
.diffableTextViews_font(.body.monospaced())
.diffableTextViews_keyboardType(.decimalPad)
}
}
A style that processes characters laid out in custom patterns.
Feature | Description | |
---|---|---|
:checkered_flag: | Pattern | Characters are laid out as described by a pattern |
:chess_pawn: | Placeholders | Placeholders represent not-yet-assigned values |
:fist_raised: | Independance | Supports multiple placeholders with different rules |
:ghost: | Invisibility | Pattern suffix can easily be \.hidden() |
import DiffableTextViews
import SwiftUI
//*============================================================================*
// MARK: * Pattern [...]
//*============================================================================*
struct ContentView: View {
typealias Number = String // Array<Character>
//=------------------------------------------------------------------------=
@State var number = Number("123456789")
//=------------------------------------------------------------------------=
var body: some View {
DiffableTextField(value: $number) {
.pattern("+## (###) ###-##-##")
.placeholders("#") { $0.isASCII && $0.isNumber }
// .hidden(true) // hides pattern beyond last real value
// .equals(()) // skips comparisons and discards changes
}
.diffableTextViews_font(.body.monospaced())
.diffableTextViews_keyboardType(.numberPad)
}
}
Decorative styles that modify the behavior of their content.
Style | Description |
---|---|
constant() | Prevents style transformations |
equals(_:) | Binds the style's equality to a proxy |
standalone() | Grants ownership of the style's cache |
prefix(_:) | Adds a prefix to the style |
suffix(_:) | Adds a suffix to the style |
link |
Stars: 52 |
Last commit: 1 year ago |
Reworked n’ improved. Such effort. Much wow.
#164 added toolbarDoneButton(_:)
#162 the view now dismisses on cyclical updates
#161 now supports reentrant updates
#156 prevents crash for large integers inputs (!)
#152 added .normal
style
#151 miscellaneous improvements
#144 added prefix(_;)
and suffix(_:)
modifiers
#138 added number total digits precision option
#121 added standalone()
modifier (niche)
#108 reworked styles, added an inout Cache
#106 changed standard font to plain .body
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics