Cómo asegurarse de una fuente, existe antes de utilizar .NET
Pregunta
Tengo un VB.NET proyecto de Formularios Windows forms que en un punto de pinturas de texto directamente en el formulario en tiempo de ejecución.Antes de pintar con la fuente, sin embargo, yo quiero para asegurarse de que el tipo de letra y tamaño de fuente que existe en la máquina del usuario.Si no, voy a probar un par de otras fuentes similares, eventualmente morosos con letra Arial o algo.
¿Cuál es la mejor manera de poner a prueba y validar una fuente en la computadora de un usuario?
Solución
A partir de un artículo de MSDN, titulada "Cómo:Enumerar los tipos de letra Instalados", me encontré con este código:
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
// Get the array of FontFamily objects.
FontFamily[] fontFamilies = installedFontCollection.Families;
Otros consejos
Aquí es una solución, en c#:
public partial class Form1 : Form
{
public Form1()
{
SetFontFinal();
InitializeComponent();
}
/// <summary>
/// This method attempts to set the font in the form to Cambria, which
/// will only work in some scenarios. If Cambria is not available, it will
/// fall back to Times New Roman, so the font is good on almost all systems.
/// </summary>
private void SetFontFinal()
{
string fontName = "Cambria";
Font testFont = new Font(fontName, 16.0f, FontStyle.Regular,
GraphicsUnit.Pixel);
if (testFont.Name == fontName)
{
// The font exists, so use it.
this.Font = testFont;
}
else
{
// The font we tested doesn't exist, so fallback to Times.
this.Font = new Font("Times New Roman", 16.0f,
FontStyle.Regular, GraphicsUnit.Pixel);
}
}
}
Y aquí es un método en VB:
Public Function FontExists(FontName As String) As Boolean
Dim oFont As New StdFont
Dim bAns As Boolean
oFont.Name = FontName
bAns = StrComp(FontName, oFont.Name, vbTextCompare) = 0
FontExists = bAns
End Function
Ver también este la misma pregunta que los resultados en este código:
private bool IsFontInstalled(string fontName) {
using (var testFont = new Font(fontName, 8)) {
return 0 == string.Compare(
fontName,
testFont.Name,
StringComparison.InvariantCultureIgnoreCase);
}
}
Arial Negrita Cursiva es poco probable que sea una fuente.Es una subclase de la familia Arial.
Trate de mantenerlo simple y la prueba de 'Arial'.