您好,我想在 Canvas 元素中使用 Fontloader qml 对象中设置的字体,其中我有一个弯曲的文本。有可能做到吗?一般来说,可以更改 Canvas 文本的字体系列吗? 这是我的代码:
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
FontLoader { id: webFont;name: "OpenSans"; source: "qrc:/OpenSans-Bold.ttf" }
Canvas{
property string nameFont: webFont.name
function drawTextAlongArc(context, str, centerX, centerY, radius, angle)
{
context.save();
context.translate(centerX, centerY);
context.rotate(-1 * angle / 2);
context.rotate(-1 * (angle / str.length) / 2);
for (var n = 0; n < str.length; n++) {
context.rotate(angle / str.length);
context.save();
context.translate(0, -1 * radius);
var char1 = str[n];
context.fillText(char1, 0, 0);
context.restore();
}
context.restore();
}
anchors.fill: parent
onPaint: {
var ctx = getContext("2d");
ctx.fillStyle = Qt.rgba(1, 1, 1, 1);
ctx.fillRect(0, 0, width, height);
ctx.font='50px OpenSans'
ctx.textAlign = "center";
var centerX = width / 2;
var centerY = height/2; //height - 30;
var angle = Math.PI * 0.8; // radians
var radius = 180;
ctx.fillStyle="#000000"
drawTextAlongArc(ctx, "Hello World", centerX, centerY, radius, angle);
}
}
}
我想使用我按照说明设置的 Verdana 字体:
ctx.font='50px Verdana'
字体设置为 FontLoader { id:webFont;来源:“qrc:/OpenSans-Bold.ttf”}
我该怎么做?
最佳答案
正如您在评论中所说,问题确实是字体名称中的空格。
如上所述in the doc :
Note: The font-size and font-family properties are mandatory and must be in the order they are shown in above. In addition, a font family with spaces in its name must be quoted.
所以应该通过加载字体来解决问题
FontLoader { id: webFont; source: "qrc:/OpenSans-Bold.ttf" }
并使用
ctx.font='50px "Open Sans"'
或者更好:
ctx.font='50px "%1"'.arg(webFont.name)
关于javascript - 我如何在canvas qml元素中使用FontLoader qml对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50045083/