Class (TextBlob)
由一个或多个具有相同字体的字符组成的字块。
说明:
本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块使用屏幕物理像素单位px。
本模块为单线程模型策略,需要调用方自行管理线程安全和上下文状态的切换。
导入模块
import { drawing } from '@kit.ArkGraphics2D';
makeFromPosText12+
static makeFromPosText(text: string, len: number, points: common2D.Point[], font: Font): TextBlob
使用文本创建TextBlob对象,TextBlob对象中每个字形的坐标由points中对应的坐标信息决定。
系统能力: SystemCapability.Graphics.Drawing
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| text | string | 是 | 绘制字形的文本内容。 |
| len | number | 是 | 字形个数,由countText获取,该参数为整数。 |
| points | common2D.Point[] | 是 | 点数组,用于指定每个字形的坐标,长度必须为len。 |
| font | Font | 是 | 字型对象。 |
返回值:
| 类型 | 说明 |
|---|---|
| TextBlob | TextBlob对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types. |
示例:
import { RenderNode } from '@kit.ArkUI';
import { drawing,common2D} from '@kit.ArkGraphics2D';
class DrawingRenderNode extends RenderNode {
draw(context : DrawContext) {
const canvas = context.canvas;
let text : string = 'makeFromPosText';
let font : drawing.Font = new drawing.Font();
font.setSize(100);
let length = font.countText(text);
let points : common2D.Point[] = [];
for (let i = 0; i !== length; ++i) {
points.push({ x: i * 35, y: i * 35 });
}
let textblob : drawing.TextBlob =drawing.TextBlob.makeFromPosText(text, points.length, points, font);
canvas.drawTextBlob(textblob, 100, 100);
}
}
uniqueID12+
uniqueID(): number
获取该TextBlob对象的唯一的非零标识符。
系统能力: SystemCapability.Graphics.Drawing
返回值:
| 类型 | 说明 |
|---|---|
| number | 返回TextBlob对象的唯一的非零标识符。 |
示例:
import {drawing} from "@kit.ArkGraphics2D";
let text : string = 'TextBlobUniqueId';
let font : drawing.Font = new drawing.Font();
font.setSize(100);
let textBlob = drawing.TextBlob.makeFromString(text, font, 0);
let id = textBlob.uniqueID();
console.info("uniqueID---------------" +id);
makeFromString
static makeFromString(text: string, font: Font, encoding?: TextEncoding): TextBlob
将string类型的值转化成TextBlob对象。
系统能力: SystemCapability.Graphics.Drawing
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| text | string | 是 | 绘制字形的文本内容。 |
| font | Font | 是 | 字型对象。 |
| encoding | TextEncoding | 否 | 编码类型,默认值为TEXT_ENCODING_UTF8。当前只有TEXT_ENCODING_UTF8生效,其余编码类型也会被视为TEXT_ENCODING_UTF8。 |
返回值:
| 类型 | 说明 |
|---|---|
| TextBlob | TextBlob对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
示例:
import { RenderNode } from '@kit.ArkUI';
import { drawing } from '@kit.ArkGraphics2D';
class DrawingRenderNode extends RenderNode {
draw(context : DrawContext) {
const canvas = context.canvas;
const brush = new drawing.Brush();
brush.setColor({alpha: 255, red: 255, green: 0, blue: 0});
const font = new drawing.Font();
font.setSize(20);
const textBlob = drawing.TextBlob.makeFromString("drawing", font, drawing.TextEncoding.TEXT_ENCODING_UTF8);
canvas.attachBrush(brush);
canvas.drawTextBlob(textBlob, 20, 20);
canvas.detachBrush();
}
}
makeFromRunBuffer
static makeFromRunBuffer(pos: Array<TextBlobRunBuffer>, font: Font, bounds?: common2D.Rect): TextBlob
基于RunBuffer信息创建TextBlob对象。
系统能力: SystemCapability.Graphics.Drawing
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| pos | Array<TextBlobRunBuffer> | 是 | TextBlobRunBuffer数组。 |
| font | Font | 是 | 字型对象。 |
| bounds | common2D.Rect | 否 | 可选,如果不设置,则无边界框。 |
返回值:
| 类型 | 说明 |
|---|---|
| TextBlob | TextBlob对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
示例:
import { RenderNode } from '@kit.ArkUI';
import { common2D, drawing } from '@kit.ArkGraphics2D';
class DrawingRenderNode extends RenderNode {
draw(context : DrawContext) {
const canvas = context.canvas;
const font = new drawing.Font();
font.setSize(20);
let runBuffer : Array<drawing.TextBlobRunBuffer> = [
{ glyph: 65, positionX: 0, positionY: 0 },
{ glyph: 227, positionX: 14.9, positionY: 0 },
{ glyph: 283, positionX: 25.84, positionY: 0 },
{ glyph: 283, positionX: 30.62, positionY: 0 },
{ glyph: 299, positionX: 35.4, positionY: 0}
];
const textBlob = drawing.TextBlob.makeFromRunBuffer(runBuffer, font, null);
const brush = new drawing.Brush();
brush.setColor({alpha: 255, red: 255, green: 0, blue: 0});
canvas.attachBrush(brush);
canvas.drawTextBlob(textBlob, 20, 20);
canvas.detachBrush();
}
}
bounds
bounds(): common2D.Rect
获取文字边界框的矩形区域。
系统能力: SystemCapability.Graphics.Drawing
返回值:
| 类型 | 说明 |
|---|---|
| common2D.Rect | 文字边界框的矩形区域。 |
示例:
import { common2D, drawing } from '@kit.ArkGraphics2D';
const font = new drawing.Font();
font.setSize(20);
const textBlob = drawing.TextBlob.makeFromString("drawing", font, drawing.TextEncoding.TEXT_ENCODING_UTF8);
let bounds = textBlob.bounds();
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 capi-drawing-gpu-context-h
openharmony 鸿蒙 capi-drawing-nativepixelmap-
openharmony 鸿蒙 capi-nativevsync-oh-nativevsync-expectedraterange
openharmony 鸿蒙 capi-drawing-oh-drawing-brush
openharmony 鸿蒙 capi-drawing-oh-drawing-image-info
openharmony 鸿蒙 capi-drawing-text-linetypography-h
openharmony 鸿蒙 capi-oh-nativeimage-oh-nativeimage
openharmony 鸿蒙 capi-drawing-typeface-h