Files
fc/dev/project/library/imports/89/89681194-1eb0-4353-b4a8-487ff6b8834f.js
T
2026-05-24 10:21:26 +08:00

109 lines
2.8 KiB
JavaScript

"use strict";
cc._RF.push(module, '89681GUHrBDU7SoSH/2uINP', 'nx.fx.local.text');
// Scripts/nx/cmp/flex/nx.fx.local.text.js
"use strict";
/******************************************************************
* Copyright(C) 2019 - 2020 Nx Studio
*
* 组件--本地化文本
*
* 2018.05.18
******************************************************************/
var SCALE = 1.5;
cc.Class({
"extends": cc.Component,
properties: {
useFont: {
"default": true,
displayName: "使用全局字体"
},
openScale: {
"default": true,
displayName: "清晰缩放"
}
},
// 编辑器特性
editor: {
// 允许当前组件在编辑器模式下运行
executeInEditMode: false,
// 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突
disallowMultiple: false,
// menu 用来将当前组件添加到组件菜单中,方便用户查找
menu: "Nx/本地化|文本"
},
// 展示
onLoad: function onLoad() {
var label = nx.gui.getComponent(this, "", cc.Label);
if (!label) {
label = nx.gui.getComponent(this, "", cc.RichText);
if (!label) {
return;
}
}
// 合批开启
// label.cacheMode = cc.Label.CacheMode.BITMAP;
// 全局字体设定
if (this.useFont && game.gfont) {
label.font = game.gfont;
}
// 本地化文本
this.setText(label.string);
// 清晰化骚操作
this.rawScaleX = this.node.scaleX;
this.rawScaleY = this.node.scaleY;
this.rawWidth = this.node.width;
this.rawHeight = label.lineHeight;
this.rawMaxWidth = label.maxWidth;
this.autoScale();
},
// 等比缩放
autoScale: function autoScale() {
if (!this.openScale) {
return;
}
var label = nx.gui.getComponent(this, "", cc.Label);
if (!label) {
label = nx.gui.getComponent(this, "", cc.RichText);
if (!label) {
return;
}
}
label.fontSize = Math.floor(label.fontSize * SCALE);
label.node.scaleX = this.rawScaleX / SCALE;
label.node.scaleY = this.rawScaleY / SCALE;
label.node.width = this.rawWidth * SCALE;
// 高度
if (nx.dt.numPositive(label.lineHeight, false)) {
label.lineHeight = Math.ceil(label.lineHeight * SCALE);
}
// 富文本
if (nx.dt.numPositive(this.rawMaxWidth, false)) {
label.maxWidth = this.rawMaxWidth * SCALE;
}
// 如果父节点是Layout,自动开启比例缩放
var layout = nx.gui.getComponent(this.node.parent, "", cc.Layout);
if (layout) {
layout.affectedByScale = true;
}
},
// 设置文本
setText: function setText(_key) {
var txt = "";
if (nx.dt.strNEmpty(_key)) {
txt = nx.text.getKey(_key);
}
nx.gui.setString(this.node, "", txt);
}
});
cc._RF.pop();