在QML中加载不同字体

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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值