建一个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("拷贝文本");

}

}

标签:复制到,el,Cocos,selection,Creator,cc,style,originalRange,document

来源: https://www.cnblogs.com/Jason-c/p/12891340.html

Logo

这里是一个专注于游戏开发的社区,我们致力于为广大游戏爱好者提供一个良好的学习和交流平台。我们的专区包含了各大流行引擎的技术博文,涵盖了从入门到进阶的各个阶段,无论你是初学者还是资深开发者,都能在这里找到适合自己的内容。除此之外,我们还会不定期举办游戏开发相关的活动,让大家更好地交流互动。加入我们,一起探索游戏开发的奥秘吧!

更多推荐