DanielCardonaRojas / Veil

A flexible string masking and formatting library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SwiftUI

emelyanovkirill opened this issue · comments

Is there a sample for SwiftUI?
I write like this, but clearing characters requires clearing mask characters too

@Published var myText = "" {
        didSet {
            if myText != myMask.mask(input: myText, exhaustive: false) {
                myText = myMask.mask(input: myText, exhaustive: false)
            }
        }
    }

Hi @emelyanovkirill

import SwiftUI

struct MaskedTextFieldStyle: TextFieldStyle {
    @Binding var text: String
    var mask: Veil

    func _body(configuration: TextField<Self._Label>) -> some View {
        configuration
            .valueChanged(value: text) { oldValue in
                text = mask.mask(
                    input: oldValue,
                    exhaustive: false
                )
            }
    }
}