zoukankan      html  css  js  c++  java
  • 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("拷贝文本");
        }
    }
    

      

  • 相关阅读:
    ArduinoYun教程之ArduinoYun硬件介绍
    Nmap扫描教程之基础扫描详解
    Nmap扫描教程之Nmap基础知识
    Nmap扫描教程之DNS服务类
    Nmap扫描教程之网络基础服务DHCP服务类
    Xamarin iOS教程之自定义视图
    Xamarin iOS教程之警告视图
    Xamarin iOS教程之页面控件
    使用光学鼠标传感器实现旋转(或线性)测量(转)
    基于STM32的uCOS-II移植详解
  • 原文地址:https://www.cnblogs.com/Jason-c/p/12891340.html
Copyright © 2011-2022 走看看