如何在 SwiftUI TextEditor 中获取用户选择的文本

Posted

技术标签:

【中文标题】如何在 SwiftUI TextEditor 中获取用户选择的文本【英文标题】:How to get user selected text in SwiftUI TextEditor 【发布时间】:2022-01-20 09:35:50 【问题描述】:

我有这段代码,我需要从 TextEditor 获取用户选择的文本。我如何在 SwiftUI 中做这样的事情?

struct ContentView: View 
    @Binding var document: AppDocument

    var body: some View 
        TextEditor(text: $document.text)
            .disableAutocorrection(true)
    

【问题讨论】:

欢迎来到 Stack Overflow!请拨打tour 并查看:How do I ask a good question? 和How to create a Minimal, Reproducible Example。你在 SwiftUI 中尝试过什么?这不是代码编写服务。 【参考方案1】:

ios 15 中你可以使用.textSelection

@available(iOS 15.0, *)
struct SelectedTextView: View 
    @State var editor: String = "When the sunlight strikes raindrops in the air"
    @State var textField: String = "blank"
    var body: some View 
        VStack
            TextEditor(text: $editor).textSelection(.enabled)
            TextField("", text: $textField).textSelection(.enabled)
        
    


@available(iOS 15.0, *)
struct SelectedTextView_Previews: PreviewProvider 
    static var previews: some View 
        SelectedTextView()
    

https://developer.apple.com/documentation/swiftui/view/textselection(_:)

【讨论】:

【参考方案2】:

虽然在 iOS 15 中添加的 .textSelection 修饰符使应用程序的最终用户能够选择和复制文本,但它并不能帮助开发人员获得用户选择的文本或选择范围。我不认为,截至 2022 年初,有一种方法可以在 SwiftUI 中本地实现。

但是,UIKit 的 UITextView 具有 selectedRange 属性,而 UITextViewDelegate 具有 textViewDidChangeSelection(_:) 方法,每次用户更改选择时都会触发该方法。要在 SwiftUI 中使用它,我们需要使用 UIViewRepresentable 协议构建一个桥,如下所示:

struct ContentView: View 
    @State private var text = ""
    
    var body: some View 
        UITextViewRepresentable(text: $text)
    


struct UITextViewRepresentable: UIViewRepresentable 
    let textView = UITextView()
    @Binding var text: String
    
    func makeUIView(context: Context) -> UITextView 
        textView.delegate = context.coordinator
        return textView
    
    
    func updateUIView(_ uiView: UITextView, context: Context) 
        // SwiftUI -> UIKit
        uiView.text = text
    
    
    func makeCoordinator() -> Coordinator 
        Coordinator(text: $text)
    
    
    class Coordinator: NSObject, UITextViewDelegate 
        @Binding var text: String
        
        init(text: Binding<String>) 
            self._text = text
        
        
        func textViewDidChange(_ textView: UITextView) 
            // UIKit -> SwiftUI
            _text.wrappedValue = textView.text
        
        
        func textViewDidChangeSelection(_ textView: UITextView) 
            // Fires off every time the user changes the selection.
            print(textView.selectedRange)
        
    

【讨论】:

以上是关于如何在 SwiftUI TextEditor 中获取用户选择的文本的主要内容,如果未能解决你的问题,请参考以下文章