文本(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)
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
xmm | number | 5 | X 坐标(毫米) |
ymm | number | 5 | Y 坐标(毫米) |
wmm | number | 20 | 宽度(毫米) |
hmm | number | 3 | 高度(毫米) |
rotation | number | 0 | 旋转角度(度) |
visible | boolean | true | 是否可见 |
locked | boolean | false | 是否锁定 |
draggable | boolean | true | 是否可拖拽 |
文本特有属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
text | string | '新建文本' | 文本内容 |
fontSize | number | 20 | 字号(像素) |
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'(微软雅黑)。