Godot 使用字体图标 例如: Iconfont、FontAwesome

Fork Me On Github
zodream 游戏 C# 2023年04月

Godot 使用字体图标 例如: Iconfont、FontAwesome

新增一个控件 例如 IconLabl

新建一个场景,继承至用户界面,命名为 IconLabel

增加一个子控件 Label 设置Font .ttf 增加脚本

C# 版

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);
    }
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970

支持 Xaml 和 十六进制写法

text
  
&#xe001;
\ue001
12
点击查看全文
标签: godot
0 190 0