Skip to content

文本(Text)

文本元素用于在画布上显示文字内容,支持字号、对齐方式和字体样式。

安装

typescript
import { TextPlugin } from 'vkedit'

host.installPlugin('text-plugin', TextPlugin)

安装后,工具栏会显示文本按钮。点击按钮后在画布上点击即可创建文本元素。

通过代码创建

typescript
import { AddElementCommand } from 'vkedit'

const elementManager = host.getPlugin('element-manager-plugin')
const text = elementManager.createElement('text')

// 设置位置和尺寸(毫米)
text.xmm = 10
text.ymm = 10
text.wmm = 60
text.hmm = 10

// 设置文本属性
text.text = 'Hello vkedit'
text.fontSize = 20
text.align = 'center'
text.fontStyle = 'bold'

host.executeCommand(new AddElementCommand(host, text))

属性

基础属性(继承自 BaseGraphicElement)

属性类型默认值说明
xmmnumber5X 坐标(毫米)
ymmnumber5Y 坐标(毫米)
wmmnumber20宽度(毫米)
hmmnumber3高度(毫米)
rotationnumber0旋转角度(度)
visiblebooleantrue是否可见
lockedbooleanfalse是否锁定
draggablebooleantrue是否可拖拽

文本特有属性

属性类型默认值说明
textstring'新建文本'文本内容
fontSizenumber20字号(像素)
align'left' | 'center' | 'right' | 'justify''left'水平对齐方式
verticalAlign'top' | 'middle' | 'bottom''bottom'垂直对齐方式
fontStyle'normal' | 'italic' | 'bold' | '500' | 'italic bold''normal'字体样式

fontSize 单位

fontSize 以像素为单位(基于当前 DPM 渲染)。例如 DPM 为 8 时,fontSize: 20 渲染为 20px。

中文字体

使用中文字体时,请确保目标打印设备或用户系统已安装对应字体。常见中文字体如 'SimSun'(宋体)、'Microsoft YaHei'(微软雅黑)。

下一步

基于 MIT 许可发布