GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Using TextFormattingCommands() with TextEditor

Forums > SwiftUI

I'm trying to employ TextFormattingCommands on selected text in a TextEditor.

Showing the commands in the apps menu is working fine, but I can't find an way to apply the format from there on a text selection. I have no idea how to "bind" the commands to the TextEditor and its selected text.

struct AnotherAppMain: Scene {
    var body: some Scene {

        WindowGroup("CommandView", id: "CommandView") {
            CommandView()
        } .commands {
            TextFormattingCommands()
        }
    }
}
struct CommandView: View {
    @State private var text: String = "some text"

    var body: some View {
            TextEditor(text: $text)
            //...

I could neither find any code snippets nor was Apples documentation much of a help.

Thanks a lot for your help Mat

   

SwiftUI's text editor is very limited. It doesn't support much more than entering large amounts of plain text. If you want rich text editing, you will have to use either NSTextView or UITextView.

You may find the following collection of text editing resouces helpful:

https://github.com/mattmassicotte/TextEditingReference

   

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.