Skip to main content
Version: v1.0

getTextLayout(text_string, options)

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: string, options: object) => result

Parameters​

ParameterDescriptionRequiredType
text_stringText content of the layout to be calculatedYESstring
optionsOptionsYESOptions

Options​

PropertiesDescriptionRequiredType
text_sizeText sizeYESnumber
text_widthWidth of a single line of textYESnumber
wrappedwhether the text is line feed, 0: no line feed; 1: line feedNOnumber

result: object​

PropertiesDescriptionType
widthWidth pixel valuenumber
heightHeight pixel valuenumber

Code example​

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

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

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