Skip to content

Commit

Permalink
simplify use of custom keyboard extension. Allowing usage from any Vi…
Browse files Browse the repository at this point in the history
…ew and automatically matching UITextField or UITextArea. (Axis in TextField seems to create a UITextArea under the hood)
  • Loading branch information
paescebu authored Feb 16, 2024
1 parent 44b0f20 commit c10a077
Showing 1 changed file with 5 additions and 35 deletions.
40 changes: 5 additions & 35 deletions Sources/CustomKeyboardKit/ViewExtensions/View+CustomKeyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,20 @@ import UIKit
import SwiftUI
@_spi(Advanced) import SwiftUIIntrospect

public extension TextField {
public extension View {
func customKeyboard(view: @escaping (UITextDocumentProxy, CustomKeyboardBuilder.SubmitHandler?, CustomKeyboardBuilder.SystemFeedbackHandler?) -> some View) -> some View {
customKeyboard(CustomKeyboardBuilder(customKeyboardView: view))
}
}

public extension TextEditor {
func customKeyboard(view: @escaping (UITextDocumentProxy, CustomKeyboardBuilder.SubmitHandler?, CustomKeyboardBuilder.SystemFeedbackHandler?) -> some View) -> some View {
customKeyboard(CustomKeyboardBuilder(customKeyboardView: view))
}
}

public extension TextField {
func customKeyboard(_ keyboardType: CustomKeyboard) -> some View {
self
.modifier(CustomKeyboardModifierTextField(keyboardType: keyboardType))
}
}

public extension TextEditor {
public extension View {
func customKeyboard(_ keyboardType: CustomKeyboard) -> some View {
self
.modifier(CustomKeyboardModifierTextEditor(keyboardType: keyboardType))
.modifier(CustomKeyboardModifier(keyboardType: keyboardType))
}
}

public struct CustomKeyboardModifierTextEditor: ViewModifier {
public struct CustomKeyboardModifier: ViewModifier {
@Environment(\.onSubmit) var onSubmit
@StateObject var keyboardType: CustomKeyboard

Expand All @@ -52,25 +39,8 @@ public struct CustomKeyboardModifierTextEditor: ViewModifier {
.introspect(.textEditor, on: .iOS(.v15...)) { uiTextView in
uiTextView.inputView = keyboardType.keyboardInputView
}
}
}

public struct CustomKeyboardModifierTextField: ViewModifier {
@Environment(\.onSubmit) var onSubmit
@StateObject var keyboardType: CustomKeyboard

public init(keyboardType: CustomKeyboard) {
self._keyboardType = StateObject(wrappedValue: keyboardType)
}

public func body(content: Content) -> some View {
content
.onAppear {
keyboardType.onSubmit = onSubmit
}
.introspect(.textField, on: .iOS(.v15...)) { uiTextField in
uiTextField.inputView = keyboardType.keyboardInputView
}
}
}

}

0 comments on commit c10a077

Please sign in to comment.