question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FontIcons with glyph name instead of code

See original GitHub issue

Hello, is there any option to write the name of the glyph instead of the code? e.g.

<ui:FontIcon Glyph="&#xE70F;" />

<ui:FontIcon Glyph="Edit" />

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ShankarBUScommented, May 11, 2020

(I’m not sure) UWP has this same behaviour, see FontIcon.Glyph. (You may know) You’ll need to use SymbolIcon, it has a Symbol property which may suit your need (but it is limited)

1reaction
ShankarBUScommented, Jun 9, 2020

Hey @daniel-rck, I’ve kinda found the solution. Basically you can Extend the FontIcon control to suit your need like this ⬇️

using ModernWpf.Controls;
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;

public class FontIconEx : FontIcon
{
    static FontIconEx()
    {
        GlyphProperty.OverrideMetadata(typeof(FontIconEx), new FrameworkPropertyMetadata(string.Empty, OnGlyphChanged));
    }

    public FontIconEx()
    {
        Loaded += FontIconEx_Loaded;
    }

    private static void OnGlyphChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var fontIconEx = (FontIconEx)d;
        if (fontIconEx._textBlock2 != null)
        {
            fontIconEx._textBlock2.Text = GetGlyph((string)e.NewValue);
        }
    }

    private TextBlock _textBlock2;

    private void FontIconEx_Loaded(object sender, RoutedEventArgs e)
    {
        if (_textBlock2 == null)
        {
            _textBlock2 = (TextBlock)typeof(FontIcon).GetField("_textBlock", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this);
        }

        _textBlock2.Text = GetGlyph(Glyph);
    }

    private static string GetGlyph(string value)
    {
        if (Enum.TryParse(value, true, out Symbol symbol))
        {
            return ConvertToString(symbol);
        }
        else
        {
            return value;
        }
    }

    private static string ConvertToString(Symbol symbol)
    {
        return char.ConvertFromUtf32((int)symbol).ToString();
    }
}

This FontIconEx control will parse and display every Symbol like the SymbolIcon control and also display Glyph Codes like the FontIcon control.

Thus you can do both

(You may already know) Note: If you enter text which are neither Symbol names nor Glyph Codes those text will appear as boxes

<local:FontIconEx Glyph="&#xE70F;" />

<local:FontIconEx Glyph="Edit" />

If the existing Symbols are not enough for you, you can create a new enum called SymbolEx with additional glyphs having custom names. Like this ⬇️

public enum SymbolEx
{
    // Copy paste the member form Symbol enum here
    Airplane = 59145,
    Book = 59190,
    HardDrive = 60834,
    // And other custom glyphs goes down here
}

and change Symbol to SymbolEx in the GetGlyph and ConvertToString methods inside the FontIconEx class.

Edit : Wait a minute, this doesn’t work when used outside the ModernWpf project.

I created this code inside ModernWpf project and when I tried to do this in another project this code doesn’t work.

I can’t even create a custom IconElement derivative, it says 'IconElement.IconElement()' is inaccessible due to its protection level

Is this intentional @Kinnara? The code now works 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does the Glyph not working in `FontIcon`?
I was trying another way of using it, I forget that I updated the code, however it doesn't work whether it's OTF or...
Read more >
Using Glyphs | Font Awesome Docs
Open up your desktop design app and create a text box with the font set to Font Aweseome 6 Duotone, then paste in...
Read more >
Use Glyphs | Font Awesome Docs
Since Font Awesome was released, we've supported copy and pasting glyphs of our icons into your favorite desktop apps. It's become the default...
Read more >
Auto-generate static class for icon font glyphs · Issue #3811
When using a font icon it's most helpful to have a static class or other method to access the glyphs by name rather...
Read more >
FontIcon does not Render Glyph that TextBlock Does #7003
I am only using two different Glyphs both in a TextBlock and a FontIcon. If the glyph in the font was bad it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found