C# 在 Winforms 上的标签上使用自定义字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1297264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:50:07  来源:igfitidea点击:

Using custom fonts on a Label on Winforms

c#fontslabel

提问by Sergio Tapia

I have a label on my Winform and I want to use a custom font called XCalibur to make it appear more shnazzy.

我的 Winform 上有一个标签,我想使用一种名为 XCalibur 的自定义字体,让它看起来更时髦。

If I use a custom font on a label and then build the solution and then .ZIP the files in \bin\Release will the end user see the labels with my custom app I used regardless if they have that font installed or not?

如果我在标签上使用自定义字体,然后构建解决方案,然后对 \bin\Release 中的文件进行 .ZIP 压缩,最终用户是否会看到我使用的自定义应用程序的标签,无论他们是否安装了该字体?

If this isn't the case, what's the proper way to use Custom Fonts on Labels.Text?

如果不是这种情况,在 Labels.Text 上使用自定义字体的正确方法是什么?

采纳答案by Adam Hughes

Embed the font as a resource (or just include it in the bin directory), and then use the PrivateFontCollectionto load the font (See the AddFontFileand AddMemoryFontfunctions). You then use the font normally like it was installed on the machine.

将字体嵌入为资源(或仅将其包含在 bin 目录中),然后使用PrivateFontCollection加载字体(请参阅AddFontFileAddMemoryFont功能)。然后,您可以像安装在机器上一样正常使用该字体。

The PrivateFontCollection class allows applications to install a private version of an existing font without the requirement to replace the system version of the font. For example, GDI+ can create a private version of the Arial font in addition to the Arial font that the system uses. PrivateFontCollection can also be used to install fonts that do not exist in the operating system.

PrivateFontCollection 类允许应用程序安装现有字体的私有版本,而无需替换字体的系统版本。例如,除了系统使用的 Arial 字体之外,GDI+ 还可以创建 Arial 字体的私有版本。PrivateFontCollection 还可用于安装操作系统中不存在的字体。

Source

来源

回答by P.K

I think the solution is to embed the desired font into you application.

我认为解决方案是将所需的字体嵌入到您的应用程序中。

Try this link:

试试这个链接:

http://www.emoreau.com/Entries/Articles/2007/10/Embedding-a-font-into-an-application.aspx

http://www.emoreau.com/Entries/Articles/2007/10/Embedding-a-font-into-an-application.aspx

回答by Siddhant

After looking through possibly 30-50 posts on this, I have finally been able to come up with a solution that actually works! Please follow the steps sequentially:

在浏览了可能 30-50 篇关于此的帖子后,我终于能够想出一个真正有效的解决方案!请按顺序执行以下步骤:

1.) Include your font file (in my case, ttf file) in your application resources. To do this, double-click on the "Resources.resx" file.

1.) 在你的应用程序资源中包含你的字体文件(在我的例子中是 ttf 文件)。为此,请双击“ Resources.resx”文件。

enter image description here

在此处输入图片说明

2.) Highlight the "Add resource" option and click the down-arrow. Select "Add existing file" option. Now, search out your font file, select it, and click OK. Save the "Resources.resx" file.

2.) 突出显示“添加资源”选项并单击向下箭头。选择“添加现有文件”选项。现在,搜索您的字体文件,选择它,然后单击“确定”。保存“Resources.resx”文件。

enter image description here

在此处输入图片说明

3.) Create a function (say, InitCustomLabelFont() ), and add the following code in it.

3.) 创建一个函数(比如 InitCustomLabelFont() ),并在其中添加以下代码。

        //Create your private font collection object.
        PrivateFontCollection pfc = new PrivateFontCollection();

        //Select your font from the resources.
        //My font here is "Digireu.ttf"
        int fontLength = Properties.Resources.Digireu.Length;

        // create a buffer to read in to
        byte[] fontdata = Properties.Resources.Digireu;

        // create an unsafe memory block for the font data
        System.IntPtr data = Marshal.AllocCoTaskMem(fontLength);

        // copy the bytes to the unsafe memory block
        Marshal.Copy(fontdata, 0, data, fontLength);

        // pass the font to the font collection
        pfc.AddMemoryFont(data, fontLength);

Your custom font has now been added to the PrivateFontCollection.

您的自定义字体现已添加到 PrivateFontCollection。

4.) Next, assign the font to your Label, and add some default text into it.

4.) 接下来,将字体分配给您的标签,并在其中添加一些默认文本。

        //After that we can create font and assign font to label
        label1.Font = new Font(pfc.Families[0], label1.Font.Size);
        label1.Text = "My new font";

5.) Go to your form layout and select your label. Right-click it and select "Properties". Look for the property "UseCompatibleTextRendering" and set it to "True".

5.) 转到您的表单布局并选择您的标签。右键单击它并选择“属性”。查找属性“ UseCompatibleTextRendering”并将其设置为“ True”。

6.) If necessary you can release the font after you are sure that it can never be used again. Call the PrivateFontCollection.Dispose() method, you can then also call Marshal.FreeCoTaskMem(data) safely. It is pretty common to not bother and leave the font loaded for the life of the app.

6.) 如有必要,您可以在确定永远不会再使用该字体后释放该字体。调用PrivateFontCollection.Dispose() 方法,您也可以安全地调用 Marshal.FreeCoTaskMem(data) 。不打扰并在应用程序的生命周期内加载字体是很常见的。

7.) Run your application. You shall now see that you custom font has been set for the given label.

7.) 运行您的应用程序。您现在将看到已为给定标签设置了自定义字体。

Cheers!

干杯!

回答by Reza Taibur

Add the font you want to use.

添加您要使用的字体。

enter image description here

在此处输入图片说明

`

`

    PrivateFontCollection modernFont = new PrivateFontCollection();

    modernFont.AddFontFile("Font.otf");

    label.Font = new Font(modernFont.Families[0], 40);`

I made a method as well.

我也做了一个方法。

 void UseCustomFont(string name, int size, Label label)
    {

        PrivateFontCollection modernFont = new PrivateFontCollection();

        modernFont.AddFontFile(name);

        label.Font = new Font(modernFont.Families[0], size);


    }

enter image description here

在此处输入图片说明