DiffableTextViews
An open-source module for as-you-type formatting and conversion in iOS.
Features
Feature | Description | |
---|---|---|
Responsive | Formats and parses text as you type | |
Automagical | Binds text to its appropriate data type | |
Versatile | Uses snapshots and attributes | |
Performant | Uses O(n) differentiation algorithms | |
Agnostic | Supports varied length characters, emojis | |
Stand-alone | Has zero remote dependencies | |
Open-source | Open and transparent, as it should be |
Algorithms
It uses three main algorithms to determine text selection.
Algorithm | Description | Complexity | |
---|---|---|---|
Text | Determines selection when text changes | ≤ Linear | |
Positions | Determines selection by positions/offsets | ≤ Linear | |
Attributes | Determines selection based on attributes | ≤ Linaer |
Requirements
- iOS 15.0+
- Swift 5.0+
Installation
- Use: Swift Package Manager.
- Copy/paste: https://github.com/oscbyspro/DiffableTextViews.
- Select a VERSIONED branch.
import DiffableTextViews
import NumericTextStyles
import PatternTextStyles
Views
DiffableTextField
A view that uses styles to format and parse text.
Features
Feature | Description | |
---|---|---|
SwiftUI | Value, style, done | |
Environment | Uses environment values | |
Monospaced | The standard font is monospaced | |
Customizable | Styles may provide default values |
ProxyTextField
A customization point for the UITextField it is based on.
Styles
NumericTextStyle
A style that processes localized numbers in various formats.
Features
Feature | Description | |
---|---|---|
Values | Decimal, Float(16-64) and (U)Int(8-64) | |
Precision | Up to 38 significant digits | |
Bounds | Clamps input/output to specified range | |
Formats | Number, currency and percent | |
Locales | Supports every locale in Foundation | |
Bilingual | Accepts both local and system inputs |
Examples
struct DiffableAmountTextField: View {
@State var amount: Decimal = 0
var body: some View {
DiffableTextField($amount) {
.currency(code: "SEK")
.bounds((0 as Decimal)...)
.precision(integer: 1..., fraction: 2)
}
.environment(\.locale, Locale(identifier: "en_SE"))
}
}
PatternTextStyle
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
struct DiffablePhoneNumberTextField: View {
@State var phoneNumber: String = ""
var body: some View {
DiffableTextField($phoneNumber) {
.pattern("+## (###) ###-##-##")
.placeholder("#") { $0.isASCII && $0.isNumber }
.constant()
}
.diffableTextField_onSetup({ $0.keyboard(.phonePad) })
}
}