Basically the title. If a Text field has textSelection enabled, is there anyway to get the text that the user has highlighted?
Text("My text here")
.textSelection(.enabled)
Ultimately, my goal is to be able to highlight text and apply style to individual characters. Using a TextField would give my app the ability to know what's been highlighted, but then I can't style individual elements. I'd like to be able to do this in SwiftUI without having to drop into TextKit.
struct MyTextEditorView: View {
@State var myText: String = ""
@State var textSelection: TextSelection? = nil
var body: some View {
TextField("Placehodler",
text: $myText,
selection: $textSelection)
}
}
I don't see any way to retrieve selected text from SwiftUI.Text
or render attributed text using TextField
, and so would suggest that you file a feedback report – If you do so, please share your report ID here for folks to track.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.