DiffableTextViews

main

As-you-type formatting in SwiftUI
oscbyspro/DiffableTextViews

DiffableTextViews

An open-source module for as-you-type formatting and conversion in iOS.

DiffableAmountTextField.gif

DiffablePhoneNumberTextField.gif

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

  1. Use: Swift Package Manager.
  2. Copy/paste: https://github.com/oscbyspro/DiffableTextViews.
  3. 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
2️⃣ Bilingual Accepts both local and system inputs 

Examples

DiffableAmountTextField.gif

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

DiffablePhoneNumberTextField.gif

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) })
    }
}

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Oct 21 2024 14:28:23 GMT-0900 (Hawaii-Aleutian Daylight Time)