Giter Site home page Giter Site logo

textfieldinputprediction's Introduction

Live TextField Input Prediction

Simple Swift TextField Live Input Prediction

This is an approach to get live input predictions/ auto completion of a SwiftUI TextField

How to use:

Copy the file PredictingTextField.swift into your Project and use it by calling a

struct MyView: View {
    
    @State var predictableValues: Array<String> = []
    @State var myPredictedValues: Array<String> = []
    
    /// Empty to start with. Will populate once user starts typing
    @State var textFieldInput: String = ""
    
    var body: some View {
        PredictingTextField(predictableValues: self.$predictableValues,
                            predictedValues: self.$myPredictedValues,
                            textFieldInput: self.$textFieldInput
        )
    }
}

You can and need to pass it the following variables:

Mandatory

  1. predictableValues -> @Binding Array<String> Needs to be an Array of Strings as we compare the input of the TextField which also is a String to these Strings. It can be only one Item in the Array or an empty array. In the later case the TextField will not make any predictions and the use of a normal TextField is recommended.
  2. predictedValues -> @Binding Array<String> This also needs to be an Array of Strings. Should be empty when initialized. In here will be the prediction/ -s of the input of the TextField based on the predictableValues. It is given as attribute so the predictions can be accessed on the parent view.
  3. textFieldInput -> @Binding String This is the text that is currently in the TextField (This is a parameter that is also used in the Swift implementation of TextField() ). It is provided as Binding Object so it can be reset from the parent view. If e.g. one wants to reset the input of the TextField once a prediction was made/ selected/ used.

Optinal

  1. textFieldTitle -> String? Use this to set a Title in an untouched TextField
  2. predictionInterval -> @State Double? This can be modified to accelerate or slower the prediction. Default is a predicition made every 0.1 seconds.

Note: If the TextField gets multiple inputs at once. E.g. in form of a String separated by spaces it will make predictions on every SubString of that input and append these predictions to the predictedValues Array. Altough every prediction will only occure once in the predictedValues.

E.g. of use

Example of TextFieldPrediction

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.