Skip to main content
Version: v3

getTextLayout(text_string, options)

Start from API_LEVEL 2.0 . Please refer to API_LEVEL.

Calculate the height and width of the target text after the layout is completed, and does not actually render it, only performs the layout calculation.

Can be used to calculate the height of a multi-line text layout with a fixed width, or the width of a single-line text layout.

Type

(text: string, options: object) => result

Parameters

ParameterDescriptionRequiredType
textText content of the layout to be calculatedYESstring
optionsOptionsYESOptions

Options

PropertiesDescriptionRequiredTypeAPI_LEVEL
text_sizeText sizeYESnumber2.0
text_widthWidth of a single line of textYESnumber2.0
wrappedwhether the text is line feed, 0: no line feed; 1: line feedNOnumber2.0
rows_maxLimit the maximum number of lines (when the given text exceeds the maximum number of lines, it will be truncated and followed by an ellipsis). The default value is 0, which means there is no limitNOnumber3.0

result: object

PropertiesDescriptionTypeAPI_LEVEL
widthWidth pixel valuenumber2.0
heightHeight pixel valuenumber2.0
rowsThe text displays the number of lines. When the wrapped field is false, the value of rows is 1.number2.0
resultCalculation result, -1 - error, 0 - success, 1 - success, characters truncated and ellipses addednumber2.0
textWhen the calculation is successful, the truncated and ellipsed text content is returned, which can be used for the display of actual UI widgetsstring2.0

Code example

import { getTextLayout } from '@zos/ui'

const { width, height } = getTextLayout('turn right and go alone the road', {
text_size: 30,
text_width: 200
})

console.log('width', width)
console.log('height', height)
import { getTextLayout } from '@zos/ui'

const { width, height } = getTextLayout('turn right and go alone the road', {
text_size: 30,
text_width: 0,
wrapped: 0
})

console.log('width', width)
console.log('height', height)