Godot 使用字体图标 例如: Iconfont、FontAwesome
新增一个控件 例如 IconLabl
新建一个场景,继承至用户界面,命名为 IconLabel
增加一个子控件 Label
设置Font .ttf
增加脚本
C# 版
using Godot;
using System;
using System.Text.RegularExpressions;
[Tool]
public partial class IconLabel : Control
{
private string text;
[Export]
public string Text
{
get { return text; }
set {
text = value;
ApplyText();
}
}
private int fontSize = 16;
[Export]
public int FontSize
{
get { return fontSize; }
set {
fontSize = value;
ApplyFontSize();
}
}
private Label IconTb;
public override Vector2 _GetMinimumSize()
{
return new Vector2(FontSize, FontSize);
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
IconTb = GetNode<Label>("Label");
ApplyFontSize();
ApplyText();
}
private void ApplyFontSize() {
if (IconTb is null) {
return;
}
if (fontSize > 0) {
IconTb.AddThemeFontSizeOverride("font_size", FontSize);
}
}
private void ApplyText() {
if (IconTb is null) {
return;
}
if (string.IsNullOrWhiteSpace(Text)) {
IconTb.Text = string.Empty;
return;
}
var text = Regex.Replace(Text, @"(&#x|\\u)([0-9a-f]+);?", match => {
return Convert.ToChar(Convert.ToInt32(match.Groups[2].Value, 16)).ToString();
}, RegexOptions.IgnoreCase);
IconTb.Text = text;
CustomMinimumSize = new Vector2(FontSize * text.Length, FontSize);
}
}
支持 Xaml 和 十六进制写法
转载请保留原文链接: https://www.zodream.cn/blog/id/241.html