Home > database >  FontAwesome icon with normal text in Xamarin
FontAwesome icon with normal text in Xamarin

Time:11-18

I want to make a button that has a small icon (from FontAwesome) and text on it in my Xamarin app. I know I can just make a button but the problem is that I will require two fonts (the standard font for text and FontAwesome for the icon). Would anyone happen to know how I can do this, or if there is another way to achieve what I want?

Thanks!

CodePudding user response:

As the json mentioned, I just made a simple implementation.

Create a new class inherit from Label, set FormattedText to combine the string(standard and icon), and add tap gesture on it .

 public class MyLabel : Label
    {
        public delegate void MyHandler(object sender, EventArgs e);
        public event MyHandler myEvent;

        public MyLabel(string _myIcon, string _myText)
        {

            //build the UI
            FormattedString text = new FormattedString();
            text.Spans.Add(new Span { Text = _myIcon ,FontFamily= "FontAwesome5Free-Regular" });
            text.Spans.Add(new Span { Text = _myText, TextColor = Color.Red ,BackgroundColor = Color.Blue });
            FormattedText = text;

            //tap event
            TapGestureRecognizer tap = new TapGestureRecognizer();
            tap.Tapped  = (sender,e) => {
                myEvent(sender,e);
            };
        }
    }

Usage

MyLabel label = new MyLabel("", "test");
label.myEvent  = (sener,e) =>
{
    //do something when tapping 
};
Content = label;

For how to integrate FontAwesome in Xamarin.Forms ,refer to

https://montemagno.com/xamarin-forms-custom-fonts-everywhere/.

  • Related