Skip to main content
Version: v3+

TEXT

Start from API_LEVEL 2.0. Please refer to API_LEVEL.

text_sample

Text widget for displaying text. Support setting text size, color, alignment, font.

Create UI widget​

import { createWidget, widget } from '@zos/ui'

const text = createWidget(widget.TEXT, Param)

Type​

Param: object​

PropertiesDescriptionRequiredType
xThe x-axis coordinate of the widgetYESnumber
yThe y-axis coordinate of the widgetYESnumber
wThe width of the widgetYESnumber
hThe height of the widgetYESnumber
colorThe color of the textNOnumber
align_hThe alignment of the horizontal axis (see ALIGN for values)NOALIGN
align_vAlignment of the vertical axis (see ALIGN for values)NOALIGN
textTextNOstring
text_sizeThe size of the fontNOnumber
text_styleText overlength handling, default is scrolling text (see TEXT_STYLE for values)NOTEXT_STYLE
line_spaceRow spacingNOnumber
char_spaceCharacter spacingNOnumber
fontFont path, resource storage path reference Folder StructureNOstring
text_i18nMulti-language text support, refer to the code example, where the 'en-US' field is required. When the current country language is not configured, the value of 'en-US' will be used. When passed in this way, the text attribute is disabledNOobject
start_angleArc layout starting angleNOnumber
end_angleArc layout ending angle (start_angle < end_angle)NOnumber
modeArc layout mode, default 0
0: inner
1: outer
NOnumber
radiusControls the arc layout radius, defaults to half of the widget's width and heightNOnumber

ALIGN alignment​

ValueDescription
align.LEFTHorizontal axis-left aligned
align.RIGHTHorizontal axis-right aligned
align.CENTER_HHorizontal axis-centered
align.TOPVertical axis-top
align.BOTTOMVertical axis-bottom
align.CENTER_VVertical axis-centered

TEXT_STYLE Text layout​

ValueDescription
text_style.ELLIPSISSingle line overflow character display...
text_style.NONEScrolling text
text_style.WRAPLine wrap

Property Access Support List​

PropertysetPropertygetPropertysettergetter
xYYYY
yYYYY
wYYYY
hYYYY
colorYYYY
align_hYYYY
align_vYYYY
textYYYY
text_sizeYYYY
fontYYYY
text_styleYYYY
line_spaceYYYY
char_spaceYYYY
text_i18nNNYY
start_angleNNNN
end_angleNNNN
modeNNNN
radiusNNNN

Cursor and text editing​

Start from API_LEVEL 4.0.

Call these methods on the text widget instance returned by createWidget(widget.TEXT, Param). Call enableCursor(true) to enable the cursor before using these methods.

enableCursor​

enableCursor(enable: boolean): void

Enables or disables the text cursor. Set enable to true to enable it or false to disable it.

getCursorPos​

getCursorPos(index: number): { x: number; y: number }

Gets the cursor position for the character at index. Returns a coordinate object:

PropertyTypeDescription
xnumberHorizontal offset relative to the text widget
ynumberVertical offset relative to the text widget

getCursorIndex​

getCursorIndex(x: number, y: number): number

Gets the cursor character index from coordinates. Both x and y are offsets relative to the text widget, using the same coordinate system as the result of getCursorPos.

getTextCount​

getTextCount(): number

Gets the number of characters in the text.

insertString​

insertString(index: number, str: string): void

Inserts the string str at the character index index.

deleteCharacter​

deleteCharacter(index: number): void

Deletes one character at the character index index.

getCursorHeight​

getCursorHeight(): number

Gets the cursor height in pixels.

getLineHeight​

getLineHeight(): number

Gets the text line height in pixels.

Cursor and text editing example​

This example enables the cursor, inserts and deletes text, then queries the character count, cursor position, and heights.

import { align, createWidget, text_style, widget } from '@zos/ui'

Page({
build() {
const text = createWidget(widget.TEXT, {
x: 40,
y: 120,
w: 320,
h: 160,
color: 0xffffff,
text_size: 28,
align_h: align.LEFT,
align_v: align.TOP,
text_style: text_style.WRAP,
text: 'Hello',
})

text.enableCursor(true)
text.insertString(5, ', Zepp OS')
text.deleteCharacter(5)

// The text is now "Hello Zepp OS".
console.log(text.getTextCount())

const position = text.getCursorPos(0)
console.log(position.x, position.y)
console.log(text.getCursorIndex(position.x, position.y))
console.log(text.getCursorHeight(), text.getLineHeight())
},
})

Code example​

import { createWidget, widget, align, prop, text_style, event } from '@zos/ui'

Page({
build() {
const text = createWidget(widget.TEXT, {
x: 96,
y: 120,
w: 288,
h: 46,
color: 0xffffff,
text_size: 36,
align_h: align.CENTER_H,
align_v: align.CENTER_V,
text_style: text_style.NONE,
text: 'HELLO, Zepp OS'
})

text.addEventListener(event.CLICK_DOWN, (info) => {
text.setProperty(prop.MORE, {
y: 200
})
})

const textWithFont = createWidget(widget.TEXT, {
x: 96,
y: 300,
w: 288,
h: 46,
color: 0xffffff,
text_size: 36,
align_h: align.CENTER_H,
align_v: align.CENTER_V,
text_style: text_style.NONE,
font: 'fonts/custom.ttf',
text_i18n: {
'en-US': 'Hello Zepp OS',
'zh-CN': '你好 Zepp OS'
}
})
}
})

Additional Examples​

Example 1​

import { align, createWidget, widget } from '@zos/ui'

createWidget(widget.TEXT, {
x: 0,
y: 0,
w: 200,
h: 200,
text: 'hello',
color: 0x34e073,
align_h: align.LEFT,
font: 'fonts/Pacifico-Regular.ttf',
})

Example 2​

import { align, createWidget, widget } from '@zos/ui'

const rootContainer = createWidget(widget.VIRTUAL_CONTAINER, {
layout: { x: '10vw', y: '10vh', width: '80vw', height: '50vh' },
})

const groupRoot = createWidget(widget.GROUP, {
parent: rootContainer,
layout: {
display: 'flex',
'flex-flow': 'row wrap',
'column-gap': '20',
'row-gap': '10',
'justify-content': 'space-evenly',
'align-items': 'center',
width: '100%',
height: '100%',
},
})

for (let i = 0; i < 3; i += 1) {
groupRoot.createWidget(widget.TEXT, {
text: 'hello zepp os',
color: 0x34e073,
align_v: align.CENTER_V,
layout: {
width: '30%',
height: '25%',
'font-size': '16',
},
})
}