• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (TextBlob)
2
3<!--Kit: ArkGraphics 2D-->
4<!--Subsystem: Graphics-->
5<!--Owner: @hangmengxin-->
6<!--Designer: @wangyanglan-->
7<!--Tester: @nobuggers-->
8<!--Adviser: @ge-yafang-->
9
10> **说明:**
11>
12> - 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
13>
14> - 本模块使用屏幕物理像素单位px。
15>
16> - 本模块为单线程模型策略,需要调用方自行管理线程安全和上下文状态的切换。
17
18由一个或多个具有相同字体的字符组成的字块。
19
20## 导入模块
21
22```ts
23import { drawing } from '@kit.ArkGraphics2D';
24```
25
26## makeFromPosText<sup>12+</sup>
27
28static makeFromPosText(text: string, len: number, points: common2D.Point[], font: Font): TextBlob
29
30使用文本创建TextBlob对象,TextBlob对象中每个字形的坐标由points中对应的坐标信息决定。
31
32**系统能力:** SystemCapability.Graphics.Drawing
33
34**参数:**
35
36| 参数名   | 类型                          | 必填 | 说明                                   |
37| -------- | ----------------------------- | ---- | -------------------------------------- |
38| text     | string             | 是   | 绘制字形的文本内容。                   |
39| len      | number             | 是   | 字形个数,由[countText](arkts-apis-graphics-drawing-Font.md#counttext12)获取,该参数为整数。 |
40| points   |[common2D.Point](js-apis-graphics-common2D.md#point12)[]     | 是   |点数组,用于指定每个字形的坐标,长度必须为len。|
41| font     | [Font](arkts-apis-graphics-drawing-Font.md)      | 是   | 字型对象。 |
42
43**返回值:**
44
45| 类型                  | 说明           |
46| --------------------- | -------------- |
47| [TextBlob](arkts-apis-graphics-drawing-TextBlob.md) | TextBlob对象。 |
48
49
50**错误码:**
51
52以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
53
54| 错误码ID | 错误信息 |
55| ------- | --------------------------------------------|
56| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types. |
57
58**示例:**
59
60```ts
61import { RenderNode } from '@kit.ArkUI';
62import { drawing,common2D} from '@kit.ArkGraphics2D';
63
64class DrawingRenderNode extends RenderNode {
65  draw(context : DrawContext) {
66    const canvas = context.canvas;
67    let text : string = 'makeFromPosText';
68    let font : drawing.Font = new drawing.Font();
69    font.setSize(100);
70    let length = font.countText(text);
71    let points : common2D.Point[] = [];
72    for (let i = 0; i !== length; ++i) {
73      points.push({ x: i * 35, y: i * 35 });
74    }
75    let textblob : drawing.TextBlob =drawing.TextBlob.makeFromPosText(text, points.length, points, font);
76    canvas.drawTextBlob(textblob, 100, 100);
77  }
78}
79```
80
81## uniqueID<sup>12+</sup>
82
83uniqueID(): number
84
85获取该TextBlob对象的唯一的非零标识符。
86
87**系统能力:** SystemCapability.Graphics.Drawing
88
89**返回值:**
90
91| 类型                  | 说明           |
92| --------------------- | -------------- |
93| number | 返回TextBlob对象的唯一的非零标识符。 |
94
95**示例:**
96
97```ts
98import {drawing} from "@kit.ArkGraphics2D";
99
100let text : string = 'TextBlobUniqueId';
101let font : drawing.Font = new drawing.Font();
102font.setSize(100);
103let textBlob = drawing.TextBlob.makeFromString(text, font, 0);
104let id = textBlob.uniqueID();
105console.info("uniqueID---------------" +id);
106```
107
108## makeFromString
109
110static makeFromString(text: string, font: Font, encoding?: TextEncoding): TextBlob
111
112将string类型的值转化成TextBlob对象。
113
114**系统能力:** SystemCapability.Graphics.Drawing
115
116**参数:**
117
118| 参数名   | 类型                          | 必填 | 说明                                   |
119| -------- | ----------------------------- | ---- | -------------------------------------- |
120| text     | string                        | 是   | 绘制字形的文本内容。                   |
121| font     | [Font](arkts-apis-graphics-drawing-Font.md)                 | 是   | 字型对象。           |
122| encoding | [TextEncoding](arkts-apis-graphics-drawing-e.md#textencoding) | 否   | 编码类型,默认值为TEXT_ENCODING_UTF8。当前只有TEXT_ENCODING_UTF8生效,其余编码类型也会被视为TEXT_ENCODING_UTF8。 |
123
124**返回值:**
125
126| 类型                  | 说明           |
127| --------------------- | -------------- |
128| [TextBlob](arkts-apis-graphics-drawing-TextBlob.md) | TextBlob对象。 |
129
130**错误码:**
131
132以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
133
134| 错误码ID | 错误信息 |
135| ------- | --------------------------------------------|
136| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
137
138**示例:**
139
140```ts
141import { RenderNode } from '@kit.ArkUI';
142import { drawing } from '@kit.ArkGraphics2D';
143
144class DrawingRenderNode extends RenderNode {
145  draw(context : DrawContext) {
146    const canvas = context.canvas;
147    const brush = new drawing.Brush();
148    brush.setColor({alpha: 255, red: 255, green: 0, blue: 0});
149    const font = new drawing.Font();
150    font.setSize(20);
151    const textBlob = drawing.TextBlob.makeFromString("drawing", font, drawing.TextEncoding.TEXT_ENCODING_UTF8);
152    canvas.attachBrush(brush);
153    canvas.drawTextBlob(textBlob, 20, 20);
154    canvas.detachBrush();
155  }
156}
157```
158
159## makeFromRunBuffer
160
161static makeFromRunBuffer(pos: Array\<TextBlobRunBuffer>, font: Font, bounds?: common2D.Rect): TextBlob
162
163基于RunBuffer信息创建Textblob对象。
164
165**系统能力:** SystemCapability.Graphics.Drawing
166
167**参数:**
168
169| 参数名 | 类型                                               | 必填 | 说明                           |
170| ------ | -------------------------------------------------- | ---- | ------------------------------ |
171| pos    | Array\<[TextBlobRunBuffer](arkts-apis-graphics-drawing-i.md#textblobrunbuffer)>    | 是   | TextBlobRunBuffer数组。        |
172| font   | [Font](arkts-apis-graphics-drawing-Font.md)                                      | 是   | 字型对象。   |
173| bounds | [common2D.Rect](js-apis-graphics-common2D.md#rect) | 否   | 可选,如果不设置,则无边界框。 |
174
175**返回值:**
176
177| 类型                  | 说明           |
178| --------------------- | -------------- |
179| [TextBlob](arkts-apis-graphics-drawing-TextBlob.md) | TextBlob对象。 |
180
181**错误码:**
182
183以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
184
185| 错误码ID | 错误信息 |
186| ------- | --------------------------------------------|
187| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. |
188
189**示例:**
190
191```ts
192import { RenderNode } from '@kit.ArkUI';
193import { common2D, drawing } from '@kit.ArkGraphics2D';
194
195class DrawingRenderNode extends RenderNode {
196  draw(context : DrawContext) {
197    const canvas = context.canvas;
198    const font = new drawing.Font();
199    font.setSize(20);
200    let runBuffer : Array<drawing.TextBlobRunBuffer> = [
201      { glyph: 65, positionX: 0, positionY: 0 },
202      { glyph: 227, positionX: 14.9, positionY: 0 },
203      { glyph: 283, positionX: 25.84, positionY: 0 },
204      { glyph: 283, positionX: 30.62, positionY: 0 },
205      { glyph: 299, positionX: 35.4, positionY: 0}
206    ];
207    const textBlob = drawing.TextBlob.makeFromRunBuffer(runBuffer, font, null);
208    const brush = new drawing.Brush();
209    brush.setColor({alpha: 255, red: 255, green: 0, blue: 0});
210    canvas.attachBrush(brush);
211    canvas.drawTextBlob(textBlob, 20, 20);
212    canvas.detachBrush();
213  }
214}
215```
216
217## bounds
218
219bounds(): common2D.Rect
220
221获取文字边界框的矩形区域。
222
223**系统能力:** SystemCapability.Graphics.Drawing
224
225**返回值:**
226
227| 类型                                               | 说明                   |
228| -------------------------------------------------- | ---------------------- |
229| [common2D.Rect](js-apis-graphics-common2D.md#rect) | 文字边界框的矩形区域。 |
230
231**示例:**
232
233```ts
234import { common2D, drawing } from '@kit.ArkGraphics2D';
235
236const font = new drawing.Font();
237font.setSize(20);
238const textBlob = drawing.TextBlob.makeFromString("drawing", font, drawing.TextEncoding.TEXT_ENCODING_UTF8);
239let bounds = textBlob.bounds();
240```