DiffableTextViews
An open source package for as-you-type formatting in SwiftUI.
Features
Feature | Description | |
---|---|---|
|
Responsive | Formats text as you type |
|
Automagical | Binds text to a chosen data type |
|
Proper | Validates and autocorrects input |
|
Versatile | Uses snapshots and attributes |
|
Performant | Uses O(n) differentiation algorithms |
|
Standalone | Uses no remote dependencies |
|
Open source | 100% transparent, as it should be |
Algorithms
Algorithm | Description | Complexity | |
---|---|---|---|
|
∆Text | Determines selection when text changes | ≤ Linear |
|
∆Selection | Determines selection when selection changes | ≤ Linear |
|
Attributes | Determines selection based on attributes | ≤ Linaer |
Installation
Simple instructions on how to install this package.
Swift Package Manager
- Select https://github.com/oscbyspro/DiffableTextViews.
- Select a VERSIONED release.
Import
import DiffableTextViews
Requirements
Swift | iOS | iPadOS | Mac Catalyst | tvOS |
---|---|---|---|---|
5.0+ | 15.0+ | 15.0+ | 15.0+ | 15.0+ |
Examples
The example app provides quick-and-easy-to-use customization tools.
Number | Pattern |
---|---|
Installation
Download this package and compile/run it with Xcode.
Views
DiffableTextField
A text field that binds values and formats them as you type.
Features
Feature | Description | |
---|---|---|
|
SwiftUI | Value, style, done |
|
Environment | Uses environment values |
|
Focus | Supports SwiftUI.FocusState |
Environment
environment(\.locale, _:)
environment(\.layoutDirection, _:)
diffableTextViews_disableAutocorrection(_:)
diffableTextViews_font(_:)
diffableTextViews_foregroundColor(_:)
diffableTextViews_multilineTextAlignment(_:)
diffableTextViews_onSubmit(_:)
diffableTextViews_submitLabel(_:)
diffableTextViews_textContentType(_:)
diffableTextViews_textFieldStyle(_:)
diffableTextViews_textInputAutocapitalization(_:)
diffableTextViews_tint(_:)
Styles
NumberTextStyle (Source, Tests)
A style that binds localized numbers using various formats.
Features
Feature | Description | |
---|---|---|
|
Values | Decimal, Double and (U)Int(8-64) |
|
Optionals | Optional and non-optional values |
|
Precision | Up to 38 digits of precision |
|
Bounds | Values are clamped to bounds |
|
Formats | Number, currency and percent |
|
Locales | Supports every Foundation.Locale |
|
Bilingual | Accepts both local and ASCII inputs |
Examples
import DiffableTextViews
import SwiftUI
//*============================================================================*
// MARK: View
//*============================================================================*
struct DiffableTextFieldXAmount: View {
//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=
@State var amount = 0 as Decimal
@State var locale = Locale(identifier: "sv_SE")
//=------------------------------------------------------------------------=
// MARK: Body
//=------------------------------------------------------------------------=
/// default precision is chosen based on currency
var body: some View {
DiffableTextField(value: $amount) {
.currency(code: "SEK")
.bounds((0 as Decimal)...)
// .precision(integer: 1..., fraction: 2)
}
.environment(\.locale, locale)
.diffableTextViews_keyboardType(.decimalPad)
}
}
PatternTextStyle (Source, Tests)
A style that processes characters laid out in custom patterns.
Features
Feature | Description | |
---|---|---|
|
Pattern | Characters are laid out as described by a pattern |
|
Placeholders | Placeholders represent not-yet-assigned values |
|
Independance | Supports multiple placeholders with different rules |
|
Invisibility | Pattern suffix can easily be \.hidden() |
Examples
import DiffableTextViews
import SwiftUI
//*============================================================================*
// MARK: View
//*============================================================================*
struct DiffableTextFieldXPhone: View {
//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=
@State var number: String = ""
@State var style = PatternTextStyle<String>
.pattern("+## (###) ###-##-##")
.placeholder("#") { $0.isASCII && $0.isNumber }
.equals(())
//=------------------------------------------------------------------------=
// MARK: Body
//=------------------------------------------------------------------------=
var body: some View {
DiffableTextField(value: $number, style: style)
.diffableTextViews_keyboardType(.numberPad)
}
}
WrapperTextStyle(s) (Source, Tests)
Decorative styles that modify the behavior of their content.
Style | Description | Method |
---|---|---|
Constant | Prevents style transformations | constant() |
Equals | Binds the style's equality to a proxy value | equals(_:) |