簡體   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