gpt4 book ai didi

c# - 如何从 html/css 样式字符串(即 : Futura, Verdana、Arial)在 C# winforms 控件(例如 TextBox)中设置 FontFamily

转载 作者:行者123 更新时间:2023-11-30 22:21:52 26 4
gpt4 key购买 nike

有没有办法在 C# 中设置 Windows 窗体控件的字体以接受逗号分隔的字体列表?我想要类似于浏览器解释 CSS 字体系列的方式,它在列表中向下移动,直到找到计算机上安装的第一个字体。

示例:

string fontList = "Obscure Font1, Obscure Font2, Verdana"
textBox1.Font = new Font( FontFamilyFromHtml(fontList), FontStyle.Bold);

.NET 中是否内置了任何内容,或者您​​是否需要创建一种方法来以逗号分隔字符串,然后为每个字符串测试已安装的字体列表,直到找到匹配项?

最佳答案

没有开箱即用的 API 调用,因此您必须拆分字符串并搜索已安装的字体。

这是一个使用 InstalledFontCollection 的实现这样做:

private FontFamily FindFontByCSSNames(string cssNames)
{
string[] names = cssNames.Split(',');
System.Drawing.Text.InstalledFontCollection installedFonts = new System.Drawing.Text.InstalledFontCollection();
foreach (var name in names)
{

var matchedFonts = from ff in installedFonts.Families where ff.Name == name.Trim() select ff;

if (matchedFonts.Count() > 0)
return matchedFonts.First();
}

// No match, return a default
return new FontFamily("Arial");
}

关于c# - 如何从 html/css 样式字符串(即 : Futura, Verdana、Arial)在 C# winforms 控件(例如 TextBox)中设置 FontFamily,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14077168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com