多语言模块中的LanguageLabel支持TTF格式字体

This commit is contained in:
dgflash
2023-07-26 18:04:28 +08:00
parent fe9181a6ec
commit ae3a9e9858
2 changed files with 35 additions and 45 deletions

View File

@@ -1,5 +1,6 @@
import { CCString, Component, error, Label, RichText, warn, _decorator } from "cc";
import { CCString, Component, Label, RichText, TTFFont, _decorator, warn } from "cc";
import { EDITOR } from "cc/env";
import { oops } from "../../../core/Oops";
import { LanguageData } from "./LanguageData";
const { ccclass, property, menu } = _decorator;
@@ -67,35 +68,12 @@ export class LanguageLabel extends Component {
this._needUpdate = true;
}
/** 初始字体尺寸 */
initFontSize: number = 0;
onLoad() {
this._needUpdate = true;
if (!this.getComponent(Label) && !this.getComponent(RichText)) {
error(this.node.name, this._dataID);
return;
}
if (this.getComponent(RichText)) {
this.initFontSize = this.getComponent(RichText)!.fontSize;
}
if (this.getComponent(Label)) {
this.initFontSize = this.getComponent(Label)!.fontSize;
}
}
/**
* 默认文本的系统字体名字
*/
public getLabelFont(lang: string): string {
switch (lang) {
case "zh":
case "tr": {
return "SimHei";
}
}
return "Helvetica";
this.updateContent();
}
/**
@@ -124,27 +102,34 @@ export class LanguageLabel extends Component {
update() {
if (this._needUpdate) {
this.updateLabel();
this.updateContent();
this._needUpdate = false;
}
}
updateLabel() {
do {
if (!this._dataID) {
break;
}
let spcomp: any = this.getComponent(Label);
if (!spcomp) {
spcomp = this.getComponent(RichText);
if (!spcomp) {
warn("[LanguageLabel], 该节点没有cc.Label || cc.RichText组件");
break;
}
}
updateContent() {
var label = this.getComponent(Label);
var richtext = this.getComponent(RichText);
var path = oops.language.pack.json + "/" + oops.language.current;
var font: TTFFont | null = oops.res.get(path, TTFFont);
spcomp.string = this.string;
if (label) {
if (font && !label.useSystemFont) {
label.font = font;
}
label.string = this.string;
this.initFontSize = label.fontSize;
}
else if (richtext) {
if (font && !richtext.useSystemFont) {
richtext.font = font;
}
this.initFontSize = richtext.fontSize;
richtext.string = this.string;
this.initFontSize = richtext.fontSize;
}
else {
warn("[LanguageLabel], 该节点没有cc.Label || cc.RichText组件");
}
while (false);
}
}

View File

@@ -2,9 +2,9 @@
* @Author: dgflash
* @Date: 2021-07-03 16:13:17
* @LastEditors: dgflash
* @LastEditTime: 2023-07-25 11:53:51
* @LastEditTime: 2023-07-26 17:20:19
*/
import { director, error, JsonAsset, warn } from "cc";
import { director, error, JsonAsset, TTFFont, warn } from "cc";
import { Logger } from "../../../core/common/log/Logger";
import { oops } from "../../../core/Oops";
import { LanguageData } from "./LanguageData";
@@ -91,7 +91,12 @@ export class LanguagePack {
return;
}
Logger.logConfig(path, "下载语言包 json 资源");
resolve(null);
oops.res.load(path, TTFFont, (err: Error | null) => {
if (err == null) Logger.logConfig(path, "下载语言包 ttf 资源");
resolve(null);
});
})
});
}