Skip to content
On this page

Class: Text

文字显示组件

ts
// 创建一个文字节点
const node = new Transform(Text);
// 设置组件宽度
node.size.x = 200;
// 获取文字组件
const text = node.getComponent(Text)!;
// 设置文字间距为5像素
text.letterPacing = 5;
// 设置行间距为5像素
text.lineSpace = 5;
// 文字居中显示
text.algin = TextAlgin.CENTER;
// 设置要显示的文字内容
text.value = '创建了一个文字组件';
// 设置字体跟随系统
text.fontFamily = 'system';
// 设置文字大小
text.fontSize = 20;
// 设置文字为斜体
text.italic = true;
// 设置文字加粗
text.bold = true;
// 设置文字超过宽度后自动换行,前提是设置了节点的宽度
text.autoWarp = true;
// 设置文字显示颜色为红色
text.color = '#FF0000';

Hierarchy

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

new Text()

组件不能单独实例化,必须通过Transform.addComponent创建

ts
const node = new Transform();
const componet = node.addComponent(Component);

Inherited from

Container.constructor

Defined in

src/core/component/Component.ts:37

Properties

emitter

emitter: EventEmitter<string | symbol, any>

事件处理器

Inherited from

Container.emitter

Defined in

src/core/component/Component.ts:27


id

id: number

用于表示组件的唯一标识

Inherited from

Container.id

Defined in

src/core/component/Component.ts:12


name

name: string = ''

组件名

Inherited from

Container.name

Defined in

src/core/component/Component.ts:17


node

node: Transform<Container>

组件挂载节点

Inherited from

Container.node

Defined in

src/core/component/Component.ts:22


redraw

redraw: boolean = false

Defined in

src/core/component/Text.ts:218


texture

Optional texture: Texture<HTMLCanvasElement>

记录文本实际宽度,需要动态计算

Defined in

src/core/component/Text.ts:216

Accessors

algin

get algin(): TextAlgin

水平对齐方式

Returns

TextAlgin

Defined in

src/core/component/Text.ts:90

set algin(val): void

Parameters

NameType
valTextAlgin

Returns

void

Defined in

src/core/component/Text.ts:93


autoWarp

get autoWarp(): boolean

是否自动换行
前提条件,设置了宽度,如果没设置宽度自动换行将不生效

Returns

boolean

Defined in

src/core/component/Text.ts:181

set autoWarp(val): void

Parameters

NameType
valboolean

Returns

void

Defined in

src/core/component/Text.ts:184


bold

get bold(): boolean

文字加粗

Returns

boolean

Defined in

src/core/component/Text.ts:165

set bold(val): void

Parameters

NameType
valboolean

Returns

void

Defined in

src/core/component/Text.ts:168


color

get color(): string

文字颜色

Returns

string

Defined in

src/core/component/Text.ts:196

set color(val): void

Parameters

NameType
valstring

Returns

void

Defined in

src/core/component/Text.ts:199


fontFamily

get fontFamily(): string

字体

Returns

string

Defined in

src/core/component/Text.ts:120

set fontFamily(val): void

Parameters

NameType
valstring

Returns

void

Defined in

src/core/component/Text.ts:123


fontSize

get fontSize(): number

字体大小

Returns

number

Defined in

src/core/component/Text.ts:135

set fontSize(val): void

Parameters

NameType
valnumber

Returns

void

Defined in

src/core/component/Text.ts:138


italic

get italic(): boolean

斜体

Returns

boolean

Defined in

src/core/component/Text.ts:150

set italic(val): void

Parameters

NameType
valboolean

Returns

void

Defined in

src/core/component/Text.ts:153


letterPacing

get letterPacing(): number

字间距

Returns

number

Defined in

src/core/component/Text.ts:60

set letterPacing(val): void

Parameters

NameType
valnumber

Returns

void

Defined in

src/core/component/Text.ts:63


lineSpace

get lineSpace(): number

行间距

Returns

number

Defined in

src/core/component/Text.ts:75

set lineSpace(val): void

Parameters

NameType
valnumber

Returns

void

Defined in

src/core/component/Text.ts:78


value

get value(): string

Returns

string

Defined in

src/core/component/Text.ts:105

set value(val): void

Parameters

NameType
valstring

Returns

void

Defined in

src/core/component/Text.ts:108

Methods

destroy

destroy(): void

消毁

Returns

void

Inherited from

Container.destroy

Defined in

src/core/component/Component.ts:55


onDestroy

onDestroy(): void

组件被销毁时触发

Returns

void

Overrides

Container.onDestroy

Defined in

src/core/component/Text.ts:48


start

start(): void

组件被挂载到节点时触发

Returns

void

Overrides

Container.start

Defined in

src/core/component/Text.ts:44


update

Optional update(time): void

每帧调用

Parameters

NameTypeDescription
timenumber和上一帧的间隔时间 ms

Returns

void

Inherited from

Container.update

Defined in

src/core/component/Component.ts:50