美文网首页
iOS16-使用UIPasteControl获取粘贴板内容

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

作者: CocoaJason | 来源:发表于2022-10-14 10:15 被阅读0次
    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)
        }
    }
    
    

    相关文章

      网友评论

          本文标题:iOS16-使用UIPasteControl获取粘贴板内容

          本文链接:https://www.haomeiwen.com/subject/idnqzrtx.html