cocos复制文字到系统粘贴板

10 篇文章 0 订阅
10 篇文章 0 订阅

cocos creator 里面需要将固定文本复制到系统粘贴板:

测试2.4和3.4都可以使用,具体需要微调

记录一下

下面代码为creator 2.4版本:

带有一点微信平台的,安卓和ios的还没时间测试就不放上来了

//复制到系统剪切板
        webCopyString: function (str) {
            console.log('复制ing---cc.sys.platform=', cc.sys.platform);

            if (cc.sys.platform == cc.sys.WECHAT_GAME) {

                // console.log('复制---微信小游戏--cc.sys.platform=', cc.sys.platform);
                wx.setClipboardData({
                    data: str, //公众号id
                    success: function(res) {
                        // wx.getClipboardData({
                        //     success: function(res) {
                        //         console.log("复制成功:", res.data);
                        //         return true;
                        //     }
                        // });
                        console.log("success复制成功:", res.data);
                        return true;
                    }
                });

                return;
            }
        

            var input = str + '';
            const el = document.createElement('textarea');
            el.value = input;
            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();
            var originalRange = null;
            if (selection.rangeCount > 0) {
                originalRange = selection.getRangeAt(0);
            }
            document.body.appendChild(el);
            el.select();
            el.selectionStart = 0;
            el.selectionEnd = input.length;
        
            var success = false;
            try {
                success = document.execCommand('copy');
            }
            catch (err) {

            }
        
            document.body.removeChild(el);
        
            if (originalRange) {
                selection.removeAllRanges();
                selection.addRange(originalRange);
            }

            if(success){
                console.log("复制成功");
            }
            else{
                console.log("复制失败");
            }
            return success;
        },
        

creator3.4版本测试正常,ts版本加了一个回调:



    //复制到系统剪切板
    public static webCopyString(str: string, cb?: Function) {

        var input = str + '';
        const el = document.createElement('textarea');
        el.value = input;
        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()!;
        var originalRange = null;
        if (selection.rangeCount > 0) {
            originalRange = selection.getRangeAt(0);
        }
        document.body.appendChild(el);
        el.select();
        el.selectionStart = 0;
        el.selectionEnd = input.length;

        var success = false;
        try {
            success = document.execCommand('copy');
        }
        catch (err) {

        }

        document.body.removeChild(el);

        if (originalRange) {
            selection.removeAllRanges();
            selection.addRange(originalRange);
        }

        cb && cb(success);
        if (success) {
            console.log("复制成功");
        }
        else {
            console.log("复制失败");
        }
        return success;
    }

3.4调用:

        Utils.webCopyString("https://www.taptap.com/", (res: boolean) => {
            if (res) {
                console.log("友情链接已复制到系统,请通过社交软件粘贴发送");
            }
            else {
                console.log("当前平台邀请失败");
            }
        })

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值