iOS16-使用UIPasteControl获取粘贴板内容

2022-10-14  本文已影响0人  CocoaJason
let pas = UIPasteboard.general
        if (pas.hasStrings && pasteChangeCount != pas.changeCount) {
            CJCustomPasteView .show(window)
        } 

        pasteChangeCount = pas.changeCount

class CJCustomPasteView: UIView {

    lazy var textView: UITextView = {
        let textView = UITextView(frame: .zero)
        textView.isUserInteractionEnabled = false
        textView.backgroundColor = .red
        textView.delegate = self
        return textView
    }()

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    static func show(_ window: UIWindow?) {
        guard let win = window else {
            return
        }

        let view = CJCustomPasteView(frame: CGRectMake(15, CGRectGetHeight(win.bounds) - 88 , CGRectGetWidth(win.bounds) - 30, 53))
        view.backgroundColor = .init(white: 0, alpha: 0.6)
        win.addSubview(view)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.addSubview(textView)
        textView.frame = CGRect(x: 5, y: 5, width: CGRectGetWidth(frame) - 98, height: 44)

        let configuration = UIPasteControl.Configuration()
        configuration.baseBackgroundColor = .red
        configuration.baseForegroundColor = .magenta
        configuration.cornerStyle = .capsule
        configuration.displayMode = .iconAndLabel

        let pasteButton = UIPasteControl(configuration: configuration)
        pasteButton.frame = CGRect(x: CGRectGetMaxX(textView.frame) + 10, y: 5, width: 78, height: 44)

        self.addSubview(pasteButton)

        pasteButton.target = textView
    }



    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension CJCustomPasteView: UITextViewDelegate {
    func textViewDidEndEditing(_ textView: UITextView) {
        print(textView.text)
    }
}

上一篇下一篇

猜你喜欢

热点阅读