FontLoader的说明
FontLoader可以通过名字、本地路径、网络url路径三种方式加载字体。
//通过名字,加载系统安装的字体
FontLoader { id: fixedFont; name: "Courier" }
//加载本地的一个字体文件
FontLoader { id: localFont; source: "content/fonts/tarzeau_ocr_a.ttf" }
//加载网络url路径上的字体
FontLoader { id: webFont; source: "http://www.princexml.com/fonts/steffmann/Starburst.ttf" }
举例:
import QtQuick 2.0
Column {
FontLoader { id: fixedFont; name: "Courier" }
FontLoader { id: webFont; source: "http://www.mysite.com/myfont.ttf" }
Text { text: "Fixed-size font"; font.family: fixedFont.name }
Text { text: "Fancy font"; font.family: webFont.name }
}
使用的时候,只需要
font.family: webFont.name
通过font.family来指定就可以。
FontLoader 的几种状态
FontLoader.Null - 没有指定字体
FontLoader.Ready - 字体已经被加载
FontLoader.Loading - 字体正在被加载
FontLoader.Error - 加载字体时发生错误
举例:
FontLoader { id: webFont; source: "http://www.princexml.com/fonts/steffmann/Starburst.ttf" }
Text {
text: {
if (webFont.status == FontLoader.Ready) myText
else if (webFont.status == FontLoader.Loading) "Loading..."
else if (webFont.status == FontLoader.Error) "Error loading font"
}
color: "lightsteelblue"
width: parent.width
wrapMode: Text.WordWrap
font.family: webFont.name; font.pixelSize: 20
}
这样可以判断加载网络路径上的字体资源的状态,是成功还是失败。
这个例子是从Qt帮助文档中找到的——“Qt Quick Examples - Text ”
运行效果:
代码:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 320; height: 480
color: "steelblue"
title: qsTr("text")
property string myText: "The quick brown fox jumps over the lazy dog."
FontLoader { id: fixedFont; name: "Courier" }
FontLoader { id: localFont; source: "content/fonts/tarzeau_ocr_a.ttf" }
FontLoader { id: webFont; source: "http://www.princexml.com/fonts/steffmann/Starburst.ttf" }
Column {
anchors { fill: parent; leftMargin: 10; rightMargin: 10; topMargin: 10 }
spacing: 15
Text {
text: myText
color: "lightsteelblue"
width: parent.width
wrapMode: Text.WordWrap
font.family: "Times"
font.pixelSize: 20
}
Text {
text: myText
color: "lightsteelblue"
width: parent.width
wrapMode: Text.WordWrap
horizontalAlignment: Text.AlignHCenter
font { family: "Times"; pixelSize: 20; capitalization: Font.AllUppercase }
}
Text {
text: myText
color: "lightsteelblue"
width: parent.width
horizontalAlignment: Text.AlignRight
wrapMode: Text.WordWrap
font { family: fixedFont.name; pixelSize: 20; weight: Font.Bold; capitalization: Font.AllLowercase }
}
Text {
text: myText
color: "lightsteelblue"
width: parent.width
wrapMode: Text.WordWrap
font { family: fixedFont.name; pixelSize: 20; italic: true; capitalization: Font.SmallCaps }
}
Text {
text: myText
color: "lightsteelblue"
width: parent.width
wrapMode: Text.WordWrap
font { family: localFont.name; pixelSize: 20; capitalization: Font.Capitalize }
}
Text {
text: {
if (webFont.status == FontLoader.Ready) myText
else if (webFont.status == FontLoader.Loading) "Loading..."
else if (webFont.status == FontLoader.Error) "Error loading font"
}
color: "lightsteelblue"
width: parent.width
wrapMode: Text.WordWrap
font.family: webFont.name; font.pixelSize: 20
}
}
}
注意:如果字体文件较大,几MB的话,会造成编译时间长,这时候就可以把字体文件放到构建目录下,然后用加载硬盘资源的方式,这样可以加快编译速度。
FontLoader { id: localFont; source: "file:font/tarzeau_ocr_a.ttf" }
构建目录是指:
在构建目录中新建一个font文件夹,里面放你的ttf字体文件。
在发布的时候,该font目录和编译的exe在一个目录就可以运行了。
参考:https://blog.csdn.net/ubuntutouch/article/details/50453787