【问题标题】:How to get user selected text in SwiftUI TextEditor如何在 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)
    }
}

【问题讨论】:

标签: swift macos 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)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-04
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多