Skip to content

Word Reference Library

Ryan Rudes edited this page Nov 9, 2021 · 1 revision

Word Reference Library

MoreUI brings UIReferenceLibraryViewController into SwiftUI as ReferenceLibrary. It also adds a view modifier .referenceLibrary similar to .sheet and .fullScreenCover for presenting a reference library. Here's an example, and for a screen recording demonstrating this sample, see here.

struct ContentView: View {
    @State var term = ""
    @State var isPresented = false
    @State var showingAlert = false
    
    var body: some View {
        Form {
            TextField("Term", text: $term)
                .textInputAutocapitalization(.never)
                .onSubmit {
                    guard !term.isEmpty else { return }
                    
                    if dictionaryHasDefinition(forTerm: term) {
                        isPresented = true
                    } else {
                        showingAlert = true
                    }
                }
        }
        .referenceLibrary(term: $term,
                          isPresented: $isPresented,
                          onDismiss: didDismiss)
        .alert("No definition found for \"\(term)\"", isPresented: $showingAlert) {
            Button("OK", role: .cancel) { }
        }
    }
    
    func didDismiss() {
        // Handle the dismissing action.
    }
}
Clone this wiki locally