有没有办法在C#中设置Windows Forms控件的字体,以便接受逗号分隔的字体列表?我想要一些类似于浏览器如何解释CSS字体家族的东西,在列表中向下排列,直到找到安装在计算机上的第一个字体。
示例:
string fontList = "Obscure Font1, Obscure Font2, Verdana"
textBox1.Font = new Font( FontFamilyFromHtml(fontList), FontStyle.Bold);.NET中有什么内置的东西吗?或者你需要创建一个用逗号拆分字符串的方法,然后测试每个字符串的安装字体列表,直到找到匹配的字体?
发布于 2012-12-29 07:54:19
没有现成的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");
}https://stackoverflow.com/questions/14077168
复制相似问题