kyle-n / HighlightedTextEditor

A SwiftUI view for dynamically highlighting user input
MIT License
726 stars 69 forks source link

How can I add a Toolbar to the keyboard #60

Closed Urkman closed 2 years ago

Urkman commented 2 years ago

I would like to add a Toolbar to the keyboard for the TextView. How can I solve this?

Using this is not working:

HighlightedTextEditor(text: $viewModel.text, highlightRules: .markdown)
    .toolbar {
        ToolbarItemGroup(placement: .keyboard) {
             Button("Click me!") {
             print("Clicked")
        }
    }
}
kyle-n commented 2 years ago

Thank you for bringing this up. Looks like there would have to be extensive work done to HLTE to add support for the .toolbar modifier. I'm still deciding if this should be a supported feature.

In the mean time, I would recommend adding the button through UIKit.

HighlightedTextEditor(text: $text, highlightRules: .markdown)
            .introspect { editor in
                let controller = UIHostingController(
                    rootView: Button("Click me") { print("clicked") }
                        .padding(.bottom)
                        .border(Color.red)
                )
                editor.textView.inputAccessoryView = controller.view
            }

For more, look up how to add buttons to the keyboard in UIKit.

Urkman commented 2 years ago

Finally I found some time to test this. But it is not working for me. The Button is not tapable :(

kyle-n commented 2 years ago

You're right. Apologies for posting bad sample code. To be honest, I'm not great with UIKit and am not the best resource for help with this. I'd recommend researching how to add UIButtons to the inputAccessoryView (e.g. not using UIHostingController).

Closing this since UIKit work is outside the scope of this library.