@ohos.graphics.drawing模块可以进行文本绘制。本文总结了如下三种绘制过程中的常见问题:
方法 | 适用场景 |
|---|---|
CustomSpan | 富文本展示(如图文混排)、文本样式自定义。 |
RenderNode | 自定义UI控件或图形绘制、实现动画或复杂布局。 |
方法 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
drawTextBlob | 高效、适合长文本。 | 控制粒度差。 |
|
drawSingleCharacter | 灵活、支持逐字符控制。 | 性能较低、不适合大量字符。 |
|
文本降级是指当系统无法正常渲染某段文本时,自动采用一种替代方案来显示文本,以保证基本的可读性和可用性。若文本未使用降级逻辑降级至系统字体,可能会出现文本缺失的情况。
drawTextBlob不支持字体降级,若需使用降级逻辑,推荐使用drawSingleCharacter进行代替。drawSingleCharacter在文字绘制方面的功能更加完善,可以在检测到当前字型中的字体不支持待绘制字符时,降级至使用系统字体绘制字符。
onDrawWrong(context: DrawContext, drawInfo: CustomSpanDrawInfo): void {
const canvas = context.canvas;
const textBrush = new drawing.Brush();
const localFont = new drawing.Font();
localFont.setSize(gUIContext.vp2px(this.fontSize));
const textBlob =
drawing.TextBlob.makeFromString(this.originText, localFont, drawing.TextEncoding.TEXT_ENCODING_UTF8);
canvas.attachBrush(textBrush);
hilog.info(0x00, 'MyCustomSpan', `drawInfo.baseline: ${drawInfo.baseline}`);
// 绘制
canvas.drawTextBlob(textBlob, drawInfo.x, drawInfo.baseline);
canvas.detachBrush();
} 问题效果预览:

因此在计算文本坐标时要根据字体的bound做出相应调整,bound和baseline的关系如下图所示,从Top到bottom为bound垂直区域,ascent为文本的升部,decender为文本的降部。

根据bound修改后的绘制效果参考代码如下:
import { drawing } from '@kit.ArkGraphics2D'; import hilog from '@ohos.hilog'; let gUIContext: UIContext; class MyCustomSpan extends CustomSpan { originText: string; fontSize?: number; constructor(originText: string) { super(); this.originText = originText; } onMeasure(measureInfo: CustomSpanMeasureInfo): CustomSpanMetrics { this.fontSize = measureInfo.fontSize; let measureUtils = gUIContext.getMeasureUtils(); return { width: gUIContext.px2vp(measureUtils.measureText({ textContent: this.originText, fontSize: measureInfo.fontSize, })), height: undefined // 高度设置为undefined自适应文本 }; } onDraw(context: DrawContext, drawInfo: CustomSpanDrawInfo): void { const canvas = context.canvas; const textBrush = new drawing.Brush(); // 设置文本绘制画刷 const localFont = new drawing.Font(); localFont.setSize(gUIContext.vp2px(this.fontSize)); const textBlob = drawing.TextBlob.makeFromString(this.originText, localFont, drawing.TextEncoding.TEXT_ENCODING_UTF8); // 设置绘制文本 canvas.attachBrush(textBrush); hilog.info(0x00, 'MyCustomSpan', `drawInfo.baseline: ${drawInfo.baseline}`); let bounds = textBlob.bounds(); // 获取文本实际边距 // 修改text的坐标并绘制 canvas.drawTextBlob(textBlob, drawInfo.x - bounds.left, drawInfo.lineTop - bounds.top); canvas.detachBrush(); } onDrawWrong(context: DrawContext, drawInfo: CustomSpanDrawInfo): void { const canvas = context.canvas; const textBrush = new drawing.Brush(); const localFont = new drawing.Font(); localFont.setSize(gUIContext.vp2px(this.fontSize)); const textBlob = drawing.TextBlob.makeFromString(this.originText, localFont, drawing.TextEncoding.TEXT_ENCODING_UTF8); canvas.attachBrush(textBrush); hilog.info(0x00, 'MyCustomSpan', `drawInfo.baseline: ${drawInfo.baseline}`); // 绘制 canvas.drawTextBlob(textBlob, drawInfo.x, drawInfo.baseline); canvas.detachBrush(); } } @Entry @ComponentV2 struct DrawText { private textController1: TextController = new TextController(); private textController2: TextController = new TextController(); aboutToAppear() { gUIContext = this.getUIContext(); } onDidBuild(): void { const style1 = new MutableStyledString(new MyCustomSpan('危乎高哉')); this.textController1.setStyledString(style1); const style2 = new MutableStyledString(new MyCustomSpan('危乎高哉')); style2.appendStyledString(new StyledString('哉')); // 绘制文字后在末尾处添加 this.textController2.setStyledString(style2); } build() { Row() { Text('危乎高哉') .margin(5); Text(undefined, { controller: this.textController1 }) .margin(5); Text(undefined, { controller: this.textController2 }) .margin(5); } .alignItems(VerticalAlign.Center) .height('100%') .width('100%'); } }
效果预览:

rawfile目录没有对外暴露的路径,使用makeFromFile时,使用路径需为应用沙箱路径或真实物理路径。
自API 18起,可以使用makeFromRawfile获取保存在rawfile目录中的字体文件。
更多关于文本绘制的常见问题,如自定义字体或系统字体的注册、文本高度及行间距的测量、多样式文本的绘制与显示等可参考文本开发概述。
Q:如何检测字体中是否支持指定Unicode?
A:目前没有接口支持。若要判断指定Unicode是否支持,可以通过在线字体网站查询,或使用textToGlyphs替代drawing.Typeface,若返回值不为0,则代表可以绘制。