object canvasrenderingcontext2d.measuretext(string text)
基础库 或更高版本; 若版本较低,建议采取 兼容处理。
主体: 企业支付宝小程序 、 个人支付宝小程序
测量文本尺寸和计量信息。
string text
要测量的文本的内容。
object
文本尺寸和计量信息。
属性 | 类型 | 描述 |
---|---|---|
width | number | 文字的宽度。基于当前上下文计算,以 css 像素为单位。 |
actualboundingboxleft | number | 从 canvasrenderingcontext2d.textalign 属性设定的对齐点,到文本的「bounding rectangle」左侧的距离。以 css 像素为单位。 |
actualboundingboxright | number | 从 canvasrenderingcontext2d.textalign 属性设定的对齐点,到文本的「bounding rectangle」右侧的距离。以 css 像素为单位。 |
fontboundingboxascent | number | 从 canvasrenderingcontext2d.textbaseline 属性设定的水平的文本基线,到用于渲染文本的、字体中最高的「bounding rectangle」顶部的距离。以 css 像素为单位。 |
fontboundingboxdescent | number | 从 canvasrenderingcontext2d.textbaseline 属性设定的水平的文本基线,到用于渲染文本的、字体中最低的「bounding rectangle」底部的距离。以 css 像素为单位。 |
actualboundingboxascent | number | 从 canvasrenderingcontext2d.textbaseline 属性设定的水平的文本基线,到用于渲染文本的「bounding rectangle」顶部的距离。以 css 像素为单位。 |
actualboundingboxdescent | number | 从 canvasrenderingcontext2d.textbaseline 属性设定的水平的文本基线,到用于渲染文本的「bounding rectangle」底部的距离。以 css 像素为单位。 |
emheightascent | number | 从 canvasrenderingcontext2d.textbaseline 属性设定的水平的文本基线,到用于渲染文本的、「line box」里的「em square」顶部的距离。以 css 像素为单位。 |
emheightdescent | number | 从 canvasrenderingcontext2d.textbaseline 属性设定的水平的文本基线,到用于渲染文本的、「line box」里的「em square」底部的距离。以 css 像素为单位。 |
代码示例
.js
.axml
page({
oncanvasready() {
my.createselectorquery().select('#canvas').node().exec((res) => {
const canvas = res[0].node
const ctx = canvas.getcontext('2d')
console.log(ctx.measuretext('hello'))
})
}
})
<canvas id="canvas" type="2d" onready="oncanvasready">canvas>