0

In Xamarin.Forms using a glyph from a Fontello font is simple:

  1. Download a font e.g. smiley.ttf.

  2. Add to project as Embedded Resource Add embedded resource graphic

  3. Export the font:

    [assembly: ExportFont("smiley.ttf", Alias = "smiley")]

  4. Use the glyph in xaml for the Text property:

<StackLayout BackgroundColor="#eeeeee">
    <!--Uses glyph #E800 from smiley.ttf-->
    <Button BorderColor="Aqua"
            BackgroundColor="Yellow"
            BorderWidth="5"
            CornerRadius="10"
            FontSize="150"
            FontFamily="smiley"
            Text="&#xE800;"
            TextColor="Black"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="CenterAndExpand"
            HeightRequest="200"
            WidthRequest="200" />
</StackLayout>

And presto: Smiley button

I would like to do the same thing in Winforms. Here's what I tried:

public MainForm()
{
    InitializeComponent();

    // For the sake of simplicity, the TTF is copied to output directory...
    var path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "Fonts", "smiley.ttf");
    // ... and loaded here.
    privateFontCollection.AddFontFile(path);

    var fontFamily = privateFontCollection.Families[0];
    Debug.Assert(fontFamily.Name == "smiley", "Expecting 'smiley' is the font family name");

    button1.Font = new Font(fontFamily, 12F);
    button1.UseCompatibleTextRendering = true;

    // Shows 'A'
    // button1.Text = "A";

    // Shows nothing.
    button1.Text = "\u0E00";
}

PrivateFontCollection privateFontCollection = new PrivateFontCollection();

Is such a thing even possible? I tried various settings of button1.UseCompatibleTextRendering = true and Application.SetCompatibleTextRenderingDefault(true) without success.

IVSoftware
  • 5,732
  • 2
  • 12
  • 23
  • 1
    Set `UseCompatibleTextRendering = true` before you set the new Font, since it's not a real True Type Font. It looks like you should have `button1.Text = "\uE800";`, not `"\u0E00"` – Jimi Jun 08 '22 at 21:13
  • 1
    BTW, remember to call `Dispose()` on that PrivateFontCollection (when the Form closes), in case you're not; it's very important. – Jimi Jun 08 '22 at 21:21
  • Oh good grief! Thanks for pointing out the typo @Jimi! Works just fine once that is fixed. Would you like to post an answer or should I answer my own? – IVSoftware Jun 08 '22 at 21:21
  • Well, since it's just a typo, it should be closed as `Non reproducible or was caused by a typo`. But if you want to post an answer that you think may be useful to someone, then do it. – Jimi Jun 08 '22 at 21:23
  • Perhaps it _might_ be a useful question. I mean, an hour ago I had this question and didn't know the answer. So for now I suppose I'll leave it. Thx again. – IVSoftware Jun 08 '22 at 21:38
  • Also wanted to mention these answers put me on the right track: [Embedded resource font in C# does not work correctly](https://stackoverflow.com/a/36509042/5438626) and also [Unicode glyphs not combined properly on Windows Forms](https://stackoverflow.com/questions/45529938/unicode-glyphs-not-combined-properly-on-windows-forms). I don't see this question being a duplicate of anything but I'm open to suggestions :) – IVSoftware Jun 08 '22 at 21:56

1 Answers1

0

The answer to the question: Can Fontello glyph be used for Winforms button in a similar way as for a Xamarin Forms button? is YES.

Thanks to Jimi for the comment pointing out my typo, and also for mentioning the necessity of disposal which I was also not aware of.

smiley winforms button

Here is the working code:

public partial class MainForm : Form
{
    // https://stackoverflow.com/a/36509042/5438626
    // https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-create-a-private-font-collection?view=netframeworkdesktop-4.8

    public MainForm()
    {
        InitializeComponent();

        // For the sake of simplicity, the TTF is copied to output directory...
        var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Fonts", "smiley.ttf");
        // ... and loaded here.
        privateFontCollection.AddFontFile(path);

        var fontFamily = privateFontCollection.Families[0];
        Debug.Assert(fontFamily.Name == "smiley", "Expecting 'smiley' is the font family name");

        button1.Font = new Font(fontFamily, 12F);
        button1.UseCompatibleTextRendering = true;

        button1.Text = "\uE800";
        button1.BackColor = Color.Yellow;
    }

    PrivateFontCollection privateFontCollection = new PrivateFontCollection();

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (components != null)
            {
                components.Dispose();
            }
            privateFontCollection.Dispose();
        }
        base.Dispose(disposing);
    }
}
IVSoftware
  • 5,732
  • 2
  • 12
  • 23
  • This [answer](https://stackoverflow.com/a/72999017/5438626) presents a more evolved version of this basic idea. – IVSoftware Jul 15 '22 at 22:09