• Cocos Creator 点击按钮复制到剪切版


    建一个ts文件,js文件不行,复制下面的代码,在场景中给button绑定事件

    // Learn TypeScript:
    //  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
    // Learn Attribute:
    //  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
    // Learn life-cycle callbacks:
    //  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
    
    const {ccclass, property} = cc._decorator;
    
    @ccclass
    export default class CopyText extends cc.Component {
    
        @property(cc.Label)
        _textDisplayArea: cc.Label = null;
    
        start () {
            this._textDisplayArea = cc.find("Canvas/文字展示区/ScrollView/view/content/item").getComponent(cc.Label);
        }
          //拷贝文本
          CopyTextEvent () {
            let input = this._textDisplayArea.string;
    
            const el = document.createElement('textarea');
    
            el.value = input;
    
            // Prevent keyboard from showing on mobile
            el.setAttribute('readonly', '');
    
            el.style.contain = 'strict';
            el.style.position = 'absolute';
            el.style.left = '-9999px';
            el.style.fontSize = '12pt'; // Prevent zooming on iOS
    
            const selection = getSelection();
            let originalRange;
            if (selection.rangeCount > 0) {
                originalRange = selection.getRangeAt(0);
            }
    
            document.body.appendChild(el);
            el.select();
    
            // Explicit selection workaround for iOS
            el.selectionStart = 0;
            el.selectionEnd = input.length;
    
            let success = false;
            try {
                success = document.execCommand('copy');
            } catch (err) {}
    
            document.body.removeChild(el);
    
            if (originalRange) {
                selection.removeAllRanges();
                selection.addRange(originalRange);
            }
    
    
            console.log("拷贝文本");
        }
    }
    

      

  • 相关阅读:
    kafka原理
    互斥和条件变量区别
    多线程-----Thread类与Runnable接口的区别
    String、StringBilder和StringBuffer之间的区别
    react native与原生的交互
    Typescript 常见写法
    react项目中的注意点
    js中的正则表达式
    前端总结(一)
    前端性能的优化
  • 原文地址:https://www.cnblogs.com/Jason-c/p/12891340.html
Copyright © 2020-2023  润新知