使用了自定义字体的注册功能,但是字体无法正确显示?
问题现象1(注册字体失败):

问题现象2(字体显示与字体资源文件中的字形显示不一致):


问题代码如下:
import { text } from '@kit.ArkGraphics2D';
@Entry
@Component
struct Index {
// Hello1234밠
message: string = 'Hello1\u{0032}34\u{2BC20}';
buttonName: string = '跳转至正确显示页面';
private pathStack: NavPathStack = new NavPathStack();
aboutToAppear(): void {
let fontCollection = text.FontCollection.getGlobalInstance();
// 注册自定义字体
// 注册成功场景
fontCollection.loadFontSync('testSuccess', $rawfile('Test.ttf'));
// 注册失败场景
fontCollection.loadFontSync('testError', $rawfile('TestError.ttf'));
// 日志显示注册成功,实际未注册成功
fontCollection.loadFontSync('testSuccess', $rawfile('Test02.ttf'));
}
build() {
Navigation(this.pathStack) {
Column({ space: 60 }) {
Text(this.message)
.id('testSuccess')
.fontFamily('testSuccess')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Normal);
Divider();
Text(this.message)
.id('testError')
.fontFamily('testError')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Normal);
Divider();
Text(this.message)
.id('testSuccess')
.fontFamily('testSuccess')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Normal);
Button(this.buttonName).onClick(() => {
this.pathStack.pushPathByName('Solution', null);
});
};
}
.height('100%')
.width('100%');
}
} // 注册成功场景
fontCollection.loadFontSync('testSuccess', $rawfile('Test.ttf'));
// 注册失败场景
fontCollection.loadFontSync('testError', $rawfile('TestError.ttf'));
// 日志显示注册成功,实际未注册成功
fontCollection.loadFontSync('testSuccess', $rawfile('Test02.ttf')); // 获取所有已经注册的字体(包括系统字体)
let promise = text.getSystemFontFullNamesByType(text.SystemFontType.CUSTOMIZED);
await promise.then((data) => {
console.info(`then font list size: ${data.length}`);
data.forEach((fontItem) => {
if (fontItem.match('testSuccess')) {
this.registerFontName = 'test02';
}
console.info(fontItem);
});
}).catch((error: BusinessError) => {
console.error(`Failed to get font fullNames by type, error: ${JSON.stringify(error)}`);
}); // 根据字体名称和类型获取字体描述符
let promise2 = text.getFontDescriptorByFullName('testSuccess', text.SystemFontType.CUSTOMIZED);
await promise2.then((fontDescriptor) => {
console.info(`find fontName=testSuccess.desc: ${JSON.stringify(fontDescriptor)}`);
}).catch((error: BusinessError) => {
console.error(`Failed to get fontDescriptor by fullName, error: ${JSON.stringify(error)}`);
}); 完整代码如下:
import { text } from '@kit.ArkGraphics2D';
@Builder
export function SolutionBuilder() {
Solution();
}
@Entry
@Component
struct Solution {
// Hello1234밠
message: string = 'Hello1\u{0032}34\u{2BC20}';
@State registerFontName: string = 'testSuccess';
aboutToAppear(): void {
// 注册自定义字体:先检测name为test01是否已经注册,若已经注册,则注册test02的字体
this.registerTestFont();
}
async registerTestFont() {
// 获取所有已经注册的字体(包括系统字体)
let promise = text.getSystemFontFullNamesByType(text.SystemFontType.CUSTOMIZED);
await promise.then((data) => {
console.info(`then font list size: ${data.length}`);
data.forEach((fontItem) => {
if (fontItem.match('testSuccess')) {
this.registerFontName = 'test02';
}
console.info(fontItem);
});
}).catch((error: BusinessError) => {
console.error(`Failed to get font fullNames by type, error: ${JSON.stringify(error)}`);
});
// 根据字体名称和类型获取字体描述符
let promise2 = text.getFontDescriptorByFullName('testSuccess', text.SystemFontType.CUSTOMIZED);
await promise2.then((fontDescriptor) => {
console.info(`find fontName=testSuccess.desc: ${JSON.stringify(fontDescriptor)}`);
}).catch((error: BusinessError) => {
console.error(`Failed to get fontDescriptor by fullName, error: ${JSON.stringify(error)}`);
});
// 实际需要自定义注册的字体
text.FontCollection.getGlobalInstance().loadFontSync(this.registerFontName, $rawfile('Test02.ttf'));
}
build() {
NavDestination() {
Column() {
Text(this.message)
.id('testSuccess')
.fontFamily('testSuccess')
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Normal);
Divider();
Text(this.message)
.id(this.registerFontName)
.fontFamily(this.registerFontName)
.fontSize($r('app.float.page_text_font_size'))
.fontWeight(FontWeight.Normal);
};
}
.height('100%')
.width('100%');
}
} Q:使用drawTextBlob接口,部分字符不显示,是什么原因?
A:使用的是drawTextBlob接口,若构造blob的字体不支持待绘制的字符,则该部分字符无法绘制。drawSingleCharacter接口支持绘制字体退化功能。若是系统字体支持,则可以使用drawSingleCharacter接口。
Q:文件转换后HarmonyOS下无法正常显示对应的宋体字体?
A:可以使用system/fonts目录下ShuS-SC.ttf。