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
Parameter | Description | Required | Type |
---|---|---|---|
text | Text content of the layout to be calculated | YES | string |
options | Options | YES | Options |
Options
Properties | Description | Required | Type | API_LEVEL |
---|---|---|---|---|
text_size | Text size | YES | number | 2.0 |
text_width | Width of a single line of text | YES | number | 2.0 |
wrapped | whether the text is line feed, 0 : no line feed; 1 : line feed | NO | number | 2.0 |
rows_max | Limit 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 limit | NO | number | 3.0 |
result: object
Properties | Description | Type | API_LEVEL |
---|---|---|---|
width | Width pixel value | number | 2.0 |
height | Height pixel value | number | 2.0 |
rows | The text displays the number of lines. When the wrapped field is false , the value of rows is 1 . | number | 2.0 |
result | Calculation result, -1 - error, 0 - success, 1 - success, characters truncated and ellipses added | number | 2.0 |
text | When the calculation is successful, the truncated and ellipsed text content is returned, which can be used for the display of actual UI widgets | string | 2.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)