Swiftpack.co - david-swift/SuggestionField as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by david-swift.
david-swift/SuggestionField v0.3-beta
A simple text field for SwiftUI with completion suggestions
⭐️ 0
🕓 4 weeks ago
iOS macOS
.package(url: "https://github.com/david-swift/SuggestionField.git", from: "v0.3-beta")


SuggestionField is a simple text field for SwiftUI with completion suggestions in the background. The user can accept the suggestions by pressing the enter key.


Code of example
import SuggestionField
import SwiftUI
struct ContentView: View {
    @State private var values: [IdentifiableString] = [.init("")]
    @FocusState private var focusedValue: UUID?
    let words = ["tiger", "this", "ice", "snake", "SuggestionField"]
    var body: some View {
        ScrollView {
            ForEach($values) { $value in
                ScrollView(.horizontal) {
                    SuggestionField("Value", text: $value.string, divide: true, words: words)
                        .font(.system(size: 60))
                        .focused($focusedValue, equals: value.id)
                        .background {
                            RoundedRectangle(cornerRadius: 20)
                                .foregroundColor(.secondary.opacity(focusedValue == value.id ? 0.1 : 0))
                        .animation(.easeInOut, value: values)
                        .animation(.easeInOut, value: focusedValue)
        .onChange(of: values) { newValue in
            withAnimation {
                if let last = newValue.last {
                    if !last.string.isEmpty {
                } else {
struct IdentifiableString: Identifiable, Equatable {
    let id = UUID()
    var string: String
    init(_ string: String) {
        self.string = string


Use a SuggestionField whenever you want a text field but with suggestions for completing the input.

Your own algorithm

You can create your own suggestion algorithm:

struct ProgrammingLanguageField: View {
    @State private var programmingLanguage = "C#"
    var body: some View{
        SuggestionField("Programming Language", text: $programmingLanguage) { input in
            if input == "Swift" {
                return "UI"
            } else if input == "Python" {
                return " (no ... it's not a snake)"
            }  else {
                return ""


You can pass an array of strings with suggestions to the SuggestionField:

struct ProgrammingLanguageField: View {
    @State private var programmingLanguage = "C#"
    let programmingLanguages = ["C", "C#", "C++", "CSS", "HTML", "Java", "JavaScript", "Kotlin", "Objective-C", "Python", "Ruby", "Swift"]
    var body: some View {
        SuggestionField("Programming Language", text: $programmingLanguage, words: programmingLanguages)


You can combine the two completion methods. If your own algorithm provides a completion, this completion will be used, else, one of the words in the array will be used.


init(_: String, text: Binding<String>, divide: Bool = false, words: [String] = [], capitalized: Bool = false, autoComplete: @escaping (String) -> String = { _ in return "" })


The placeholder (_: String) is a string that is visible when the user's input is empty.


The text (text: Binding<String>) stores the user's input.


If divide (divide: Bool) is true, the suggestions are displayed after every single word.


The words (words: [String]) is an array of strings with the autocompletion suggestions.


The capitalized bool (capitalized: Bool) defines, if capital letters matter or if the suggestion is made without the correct capitalization as well.


AutoComplete (autoComplete: @escaping (String) -> String) is the function called when the user changes the input to update the completion suggestion. The parameter is the user's active input (if divide is true, it is the last word, else, it is the whole input) and the return value is the new completion suggestion. If there is no suggestion, it should be an empty string.


Stars: 0
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Beta 0.3
4 weeks ago

Beta 0.3

Text field with suggestions.

  • Text field with suggestions in the background
  • Accept suggestions and complete text
  • Generate suggestions algorithm with words
  • Move the cursor to the end of the text field after accepting a suggestion
  • Testing
  • More testing

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