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

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)
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容