繁体   English   中英

fabric.js - 如何设置文字宽度?

[英]fabric.js - How to set text width?

我发现将textAlign设置为fabric.js Text对象是没用的,因为Text字段的宽度会自动分配以完全适合文本的宽度。

我在API中找不到任何可以设置其他宽度的内容。 我尝试过scaleToWidth ,但它也没有。

this.text = new fabric.Text(this.name, 
{
        fontFamily: 'Calibri Light',
        fontSize: 17,
        backgroundColor: 'blue', // using this to confirm that the width is actually just set to the text width
        textAlign: 'center',
        top: 15,
        scaleToWidth: 1.1,
})

我使用了一个漂亮的hacky解决方案让它以理想的方式出现。

引用fabric.js网站:

textAlign设置

使用多行文本时,文本对齐非常有用。 使用单行文本,边界框的宽度始终与该行的宽度完全匹配,因此无需对齐。 允许的值为“left”,“center”和“right”。

有谁知道怎么解决这个问题?

不确定你是否在你第一次提出问题后的两年内找到答案,但我遇到了同样的问题,并且能够通过将文本对象添加到画布然后设置它的宽度然后触发renderAll()来找到解决方案画布。

var text = new fabric.Text('sample text', {fill: '#fff', textAlign: 'center'});
canvas.add(text);
text.width = 180;
canvas.renderAll();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM