在 TextField SwiftUI 上捕获粘贴的文本

问题描述 投票:0回答:1

我创建了一个 PasteBoardObserver 类来捕获粘贴的文本并更改 isCodePasted Bool 但它无法检测它是否被粘贴。 PasteboardObserver 内的任何打印语句都不会被触发。但是粘贴的 onReceive(pasteboardObserver.$pastedCode) foo 里面的那个被打印了

    import SwiftUI
import UIKit
import Combine

class PasteboardObserver: ObservableObject {
    @Published var pastedCode: String = ""
    @Published var isCodePasted: Bool = false

private var cancellable: AnyCancellable?

init() {
    self.cancellable = NotificationCenter.default.publisher(for: UIPasteboard.changedNotification)
        .sink { _ in
            DispatchQueue.main.async {
                print("Pasteboard changed")
                if let string = UIPasteboard.general.string {
                    print("Pasted string: \(string)")
                    self.pastedCode = string
                    self.isCodePasted = true
                } else {
                    print("No string found in pasteboard")
                }
            }
        }
}
}

这样我就可以检测到更改并从我的文本字段中获取粘贴的代码,如下所示。

struct CUINDPasteValidationTextField: View {
 var body: some View {
        VStack(alignment: .leading) {
            HStack {
                    CUINDTextField(text: $text,
                                   focused: $focused,
                                   placeholder: placeholder,
                                   fontColor: style.fontColor
                    )
                    .onReceive(pasteboardObserver.$pastedCode) { pastedCode in
                        print("foo pasted")
                        print(pastedCode)
                    }
            }

现在它确实可以理解何时粘贴某些内容,但它始终是“”。如何正确捕获粘贴的文本并更改 isPastedCode 的状态?

swiftui nsnotificationcenter observedobject
1个回答
0
投票

看来你的

self.cancellable = NotificationCenter.default.publisher ...
可能无法正常工作。

您可以尝试这种简单的替代方法, 使用

NotificationCenter.default.addObserver
, 和
.onChange(..)
catch the pasted text and change the status of isPastedCode

示例代码:

 import Foundation
 import SwiftUI
 

class PasteboardObserver: ObservableObject {
    @Published var pastedCode = ""
    @Published var isCodePasted = false
    
    init() {
        NotificationCenter.default.addObserver(forName: UIPasteboard.changedNotification, object: nil, queue: .main) { _ in
            if let string = UIPasteboard.general.string {
                self.pastedCode = string
            }
        }
    }
}

struct ContentView: View {
    @StateObject var pboard = PasteboardObserver()
    @State private var txt1 = "xyz"
    @State private var txt2 = ""
    
    var body: some View {
        VStack(spacing: 55) {
            Text("pastedCode: \(pboard.pastedCode)")
            // for testing highlight then copy the text, Cmd+c
            TextField("type and copy text", text: $txt1).border(.red)
            
            // for testing paste the text, Cmg+v
            TextField("paste some text", text: $txt2).border(.blue)
                .onChange(of: txt2) {
                    // for testing
                    if txt2 == pboard.pastedCode {
                        pboard.isCodePasted = true
                        print("---> onChange pastedCode: \(pboard.pastedCode)")
                    }
                }
        }
    }
    
}
© www.soinside.com 2019 - 2024. All rights reserved.