Swiftpack.co - oscbyspro/DiffableTextViews as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by oscbyspro.
oscbyspro/DiffableTextViews v4.2.1
An open source package for as-you-type formatting in SwiftUI.
⭐️ 39
🕓 2 weeks ago
iOS macOS tvOS macCatalyst
.package(url: "https://github.com/oscbyspro/DiffableTextViews.git", from: "v4.2.1")

DiffableTextViews

An open source package for as-you-type formatting in SwiftUI.

Number Pattern

Features

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

Installation

Simple instructions on how to install this package.

Swift Package Manager

  1. Select https://github.com/oscbyspro/DiffableTextViews.
  2. Select a VERSIONED release.

Import

import DiffableTextViews

Requirements

Swift iOS iPadOS Mac Catalyst tvOS
5.0+ 15.0+ 15.0+ 15.0+ 15.0+

Apps

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
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
:iphone: SwiftUI Value, style, done
:mountain: Environment Uses environment values
:mag_right: 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
: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 inputs 

Examples

Number

import DiffableTextViews
import SwiftUI

//*============================================================================*
// MARK: * Number
//*============================================================================*

struct ContentView: View {
    typealias Amount = Decimal // Decimal, Double, (U)Int(8-64), Optional<T>
    
    //=------------------------------------------------------------------------=
    // MARK: State
    //=------------------------------------------------------------------------=
    
    @State var amount = 0 as Amount
    @State var currencyCode = "SEK"
    @State var locale = Locale(identifier: "sv_SE")
    
    //=------------------------------------------------------------------------=
    // MARK: Accessors
    //=------------------------------------------------------------------------=
    
    var en_US: Locale {
        Locale(identifier: "en_US")
    }
    
    //=------------------------------------------------------------------------=
    // MARK: Body
    //=------------------------------------------------------------------------=
    
    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(en_US).constant() // prevents changes by the environment
        }
        .environment(\.locale, locale)
        .diffableTextViews_keyboardType(.decimalPad)
    }
}

PatternTextStyle (Source, Tests)

A style that processes characters laid out in custom patterns.

Features

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

Examples

Pattern

import DiffableTextViews
import SwiftUI

//*============================================================================*
// MARK: * Pattern
//*============================================================================*

struct ContentView: View {
    
    //=------------------------------------------------------------------------=
    // MARK: State
    //=------------------------------------------------------------------------=
    
    @State var number = "123456789"
    
    //=------------------------------------------------------------------------=
    // MARK: Body
    //=------------------------------------------------------------------------=
    
    var body: some View {
        DiffableTextField(value: $number) {
            .pattern("+## (###) ###-##-##")
            .placeholder("#") { $0.isASCII && $0.isNumber }
            // .hidden(true) // hides pattern beyond last real value
            // .equals(()) // skips comparisons and discards changes
        }
        .diffableTextViews_keyboardType(.numberPad)
    }
}

WrapperTextStyle(s) (Source, Tests)

Decorative styles that modify the behavior of their content.

Style Description
constant() Prevents style transformations
equals(_:) Binds the style's equality to a proxy value

GitHub

link
Stars: 40
Last commit: 19 hours ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

v4.2.1
2 weeks ago

Just the latest and greatest before I look into dub dub beta software.

  • some performance improvements #105

Also, this is commit 1000 + 1337. It must be elite, the number says so.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics