getTextLayout(text_string, options)
API_LEVEL
2.0
开始支持,API 兼容性请参考 API_LEVEL。
计算出目标文本布局完成之后的高度和宽度,并不会实际进行渲染,只进行布局计算。
可用于计算固定宽度下多行文本排布的高度,或者单行文本排布的宽度。
类型
(text_string: string, options: object) => result
参数
参数 | 说明 | 必填 | 类型 |
---|---|---|---|
text_string | 待计算布局的文本内容 | 是 | string |
options | 支持传入的选项参数 | 是 | Options |
Options
属性 | 说明 | 必填 | 类型 |
---|---|---|---|
text_size | 文字大小 | 是 | number |
text_width | 单行文本的宽度,如果设置 wrapped:0 ,text_width 需要传 0 | 是 | number |
wrapped | 文本是否换行,0 :不换行;1 : 换行 | 否 | number |
result: object
属性 | 说明 | 类型 |
---|---|---|
width | 宽度像素值 | number |
height | 高度像素值 | number |
代码示例
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)