1

I have a list of icons that need to be dynamically bound to a Xamarin page.

The Xaml is:

<Label
    Grid.Row="2"
    Grid.ColumnSpan="4"
    HorizontalOptions="Start"
    Style="{StaticResource IconLabelStyle}"
    Text="{Binding Features}"/>

where Features is a comma separated list of Fontawesome icons. Hardcoded hex values work

&#xf236;  &#xf1eb;

Unicode values just render as

"\uf236  \uf1eb"

How can I get the list of icons to render as a complete list?

Ok this is really strange.

This works

 string features = "\uf236  \uf1eb \uf540  \uf2e7  \uf2cc \uf084 \uf26c \uf001 \ue065 \uf206";
FeaturesList = features;

This doesnt

FeaturesList = model.Features;

Where Features list is the bound list on the page. Model.features contains exactly the same values as the string features. Thanks for all the help

amun1000
  • 453
  • 1
  • 8
  • 18

2 Answers2

2

Based on your statement

where Features is a comma separated list of Fontawesome icons. Hardcoded hex values work

I will assume the below code interpretation of Features, you will need to:

  • Split each icon's code and remove extra spaces.
  • Assign divided icon's codes to a List<string> property, in order to bind to the UI.
string Features = "\uf236, \uf1eb, \uf540, \uf2e7, \uf2cc, \uf084, \uf26c, \uf001, \ue065, \uf206";
public List<string> IconsList { get; set; }

    public MainPage()
    {
        BindingContext = this;
        IconsList = Features.Trim().Split(',').Select(x => x.Trim()).ToList();
        InitializeComponent();
    }

In your UI you can use bind IconsList property using BindableLayout, CollectionView or ListView:

<StackLayout BindableLayout.ItemsSource="{Binding IconsList}" Orientation="Horizontal">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Label FontFamily="FontAwesome" Text="{Binding .}"/>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

Note

If you are adding/removing (not only during first appearing of the page), then you may need to change the type List<string> to ObservableCollection<string>.

This answer doesn't cover how to set-up fonteawesome icons for that take a look at How to use Font Awesome icons in project as an icon of ImageButton

Docs

Cfun
  • 8,442
  • 4
  • 30
  • 62
  • you should be able to show multiple font icons in a single label without splitting them into a list – Jason Jan 18 '22 at 19:55
  • Yes right but I am not sure what the OP needs exactly is just an assumption maybe he needs to use one single Label or maybe he has other stuff to add before/after each icon!? – Cfun Jan 18 '22 at 22:02
  • @amun1000 make sure to specify `FontFamily` on your Label, also make sure your embedded font setup is fine, you can take a look at the link i provided – Cfun Jan 19 '22 at 21:28
0

You can also use a single Label to implement it .

Custom Label

public class MyLabel : Label
{
    public static readonly BindableProperty MyTextProperty =
      BindableProperty.Create("MyText", typeof(string), typeof(MyLabel), null, propertyChanged: BindingPropertyChangedDelegate);

    public string MyText
    {
        get { return (string)GetValue(MyTextProperty); }
        set { SetValue(MyTextProperty, value); }
    }

    static void BindingPropertyChangedDelegate(BindableObject bindable, object oldValue, object newValue)
    {
        if (newValue == null) return;

        var IconsList = ((string)newValue).Trim().Split(',').Select(x => x.Trim()).ToList();

        if (IconsList == null || IconsList.Count == 0) return;

        FormattedString text = new FormattedString();

        foreach(var str in IconsList)
        {
            text.Spans.Add(new Span { Text = "  " });
            text.Spans.Add(new Span { Text = str, FontFamily = "FontAwesomeSolid"});
        }

        ((Label)bindable).FormattedText = text;
    }
}

Xaml Usage

xmlns:local="clr-namespace:MyForms.View"

<local:MyLabel MyText="{Binding Features}"/>

Code behind

Features = "\uf164,\uf140,\uf236,\uf1eb,\uf5e1,\uf14a,\uf14b,\uf520,\uf14d,\uf14e,\uf578,\uf15c";
this.BindingContext = this;

enter image description here

Refer to

https://devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms/

ColeX
  • 14,062
  • 5
  • 43
  • 240
  • Thanks for both solutions but neither seemed to work. Still getting hard coded characters being displayed for the BindableLayout and no text on the custom control - Label. Is there a working solution of the above ? - thanks – amun1000 Jan 19 '22 at 08:00
  • Did you follow the link I provided to display font awesome ? – ColeX Jan 19 '22 at 08:11
  • Test on a normal Label to see if it displays like [this](https://devblogs.microsoft.com/xamarin/wp-content/uploads/sites/44/2020/04/aeZ94Faz.png) ? – ColeX Jan 19 '22 at 08:12
  • Yep - all followed. Incidentally when i changed the hex to unicode the icons displayed. However when i bind them to the model "string FeatureIcons = model.Features;" with unicode values the values have \\ which need stripping off but i cant Just to be clear the icons displayed using the bindable layout – amun1000 Jan 19 '22 at 08:33
  • Can you show the value of `model.Features` ? – ColeX Jan 19 '22 at 08:47
  • `Label.Text` supports `hex string` but `Span.Text` only supports `unicode string` according to my test . – ColeX Jan 19 '22 at 08:48
  • \uf236 \uf1eb \uf540 \uf2e7 \uf2cc, \uf084, \uf26c, \uf001, \ue065, \uf206 – amun1000 Jan 19 '22 at 10:28
  • You need to add , to separate every single Unicode icon , so that it can divide that into a working list . – ColeX Jan 20 '22 at 04:28
  • Posted an update to initial question – amun1000 Jan 20 '22 at 08:16
  • Can you provide a basic, reproducible project ? Since I can't be able to reproduce it – ColeX Jan 20 '22 at 08:19
  • Hi , did you solve the probelm ? – ColeX Jan 27 '22 at 01:53