Fontello glyph可以像Xamarin窗体按钮一样用于Winforms按钮吗?

liwlm1x9  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(121)

Xamarin.Forms中,使用Fontello字体中的字形非常简单:
1.下载字体,例如smiley.ttf
1.以Embedded Resource

的形式添加到项目
1.导出字体:
[assembly: ExportFont("smiley.ttf", Alias = "smiley")]
1.将xaml中的标志符号用于Text属性:

<StackLayout BackgroundColor="#eeeeee">
    <!--Uses glyph #E800 from smiley.ttf-->
    <Button BorderColor="Aqua"
            BackgroundColor="Yellow"
            BorderWidth="5"
            CornerRadius="10"
            FontSize="150"
            FontFamily="smiley"
            Text="&#xE800;"
            TextColor="Black"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="CenterAndExpand"
            HeightRequest="200"
            WidthRequest="200" />
</StackLayout>

然后一眨眼的功夫:

我想在Winforms中做同样的事情。下面是我尝试的:

public MainForm()
{
    InitializeComponent();

    // For the sake of simplicity, the TTF is copied to output directory...
    var path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "Fonts", "smiley.ttf");
    // ... and loaded here.
    privateFontCollection.AddFontFile(path);

    var fontFamily = privateFontCollection.Families[0];
    Debug.Assert(fontFamily.Name == "smiley", "Expecting 'smiley' is the font family name");

    button1.Font = new Font(fontFamily, 12F);
    button1.UseCompatibleTextRendering = true;

    // Shows 'A'
    // button1.Text = "A";

    // Shows nothing.
    button1.Text = "\u0E00";
}

PrivateFontCollection privateFontCollection = new PrivateFontCollection();

这样的事情甚至可能吗?我尝试了button1.UseCompatibleTextRendering = trueApplication.SetCompatibleTextRenderingDefault(true)的各种设置都没有成功。

von4xj4u

von4xj4u1#

问题的答案是:Fontello glyph可以像Xamarin Forms按钮一样用于Winforms按钮吗?
感谢Jimi指出我的错字,也感谢他提到我也不知道处理的必要性。

下面是工作代码:

public partial class MainForm : Form
{
    // https://stackoverflow.com/a/36509042/5438626
    // https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-create-a-private-font-collection?view=netframeworkdesktop-4.8

    public MainForm()
    {
        InitializeComponent();

        // For the sake of simplicity, the TTF is copied to output directory...
        var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fonts", "smiley.ttf");
        // ... and loaded here.
        privateFontCollection.AddFontFile(path);

        var fontFamily = privateFontCollection.Families[0];
        Debug.Assert(fontFamily.Name == "smiley", "Expecting 'smiley' is the font family name");

        button1.Font = new Font(fontFamily, 12F);
        button1.UseCompatibleTextRendering = true;

        button1.Text = "\uE800";
        button1.BackColor = Color.Yellow;
    }

    PrivateFontCollection privateFontCollection = new PrivateFontCollection();

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (components != null)
            {
                components.Dispose();
            }
            privateFontCollection.Dispose();
        }
        base.Dispose(disposing);
    }
}

相关问题