如何检查字体是否支持特定的样式?

时间:2022-01-30 15:52:26

I'm getting the following exception when changing my application font, because I use a strike out in a part of my application, and some fonts don't support it:

我在更改应用程序字体时遇到以下异常,因为我在应用程序的某个部分中使用了删除操作,有些字体不支持:

如何检查字体是否支持特定的样式?

I change my application font using a font dialog. I need to check if the selected font supports the strikeout style after assigning it to my application.

我使用字体对话框更改应用程序字体。我需要在分配到应用程序后检查所选字体是否支持删除样式。

What is the recommended way to do this? I know I could create a font with the style and catch the exception, but is there a more elegant way to do it?

推荐的方法是什么?我知道我可以创建带有样式的字体并捕获异常,但是有更优雅的方法吗?

Thanks in advance.

提前谢谢。


EDIT: The user selects a font, not necesary strikeout. In that moment I need to check if the font supports the style strikeout, because I create a strikeout font in a part of my application. If the font don't support the strikeout style would not allow the user to choose that font.

编辑:用户选择字体,而不是必需的删除线。此时,我需要检查字体是否支持样式strikeout,因为我在应用程序的一部分中创建了一个strikeout字体。如果字体不支持删除样式,则不允许用户选择该字体。

3 个解决方案

#1


3  

Updated : (to reflect update in the initial post):

更新:(反映最初的更新):

InstalledFontCollection ifc = new InstalledFontCollection();
for (int i = 0; i < ifc.Families.Length; i++)
    {
         if (ifc.Families[i].IsStyleAvailable(FontStyle.StrikeOut))
         {
             //add particular font with this family to your "font selector"
         }
    }

#2


0  

If you are using the standard Font class, then you can use the Font.Strikeout property:

如果您正在使用标准字体类,那么您可以使用该字体。三振型的属性:

//Gets a value that indicates whether this Font specifies a horizontal line through the font.
public bool Strikeout { get; }

#3


0  

Finally I used the following:

最后我使用了以下内容:

    private bool SupportStrikeout(Font font)
    {
        try
        {
            using (Font strikeout = new Font(font, FontStyle.Strikeout))
            {
                return true;
            }
        }
        catch (ArgumentException)
        {
            return false;
        }
    }

#1


3  

Updated : (to reflect update in the initial post):

更新:(反映最初的更新):

InstalledFontCollection ifc = new InstalledFontCollection();
for (int i = 0; i < ifc.Families.Length; i++)
    {
         if (ifc.Families[i].IsStyleAvailable(FontStyle.StrikeOut))
         {
             //add particular font with this family to your "font selector"
         }
    }

#2


0  

If you are using the standard Font class, then you can use the Font.Strikeout property:

如果您正在使用标准字体类,那么您可以使用该字体。三振型的属性:

//Gets a value that indicates whether this Font specifies a horizontal line through the font.
public bool Strikeout { get; }

#3


0  

Finally I used the following:

最后我使用了以下内容:

    private bool SupportStrikeout(Font font)
    {
        try
        {
            using (Font strikeout = new Font(font, FontStyle.Strikeout))
            {
                return true;
            }
        }
        catch (ArgumentException)
        {
            return false;
        }
    }