0

Having trouble with using a font. Works fine when I to the following down below.

FontFamily="{StaticResource IconFonts}" Text=""

But it doesn't work if I try to dynamically bind the text property.

FontFamily="{StaticResource IconFonts}" Text="{Binding Comments}"

It shows up all weird. It looks like it tries to do something but doesn't completely render. Here is the full code with my testing included down below. Thanks in advance.

<FlexLayout x:Name="FlexFood"
        Wrap="Wrap"
        Direction="Row"
        JustifyContent="Start"
        Padding="0, 10"
        BindableLayout.ItemsSource="{Binding FoodCollection}">
<BindableLayout.ItemTemplate>
    <DataTemplate>
        <StackLayout Padding="5, 0">

                    <Frame.GestureRecognizers>

                        <TapGestureRecognizer Tapped="OnTypesSelected"></TapGestureRecognizer>

                    </Frame.GestureRecognizers>

                    <Frame.Content>
                        <StackLayout x:Name="LayoutFood">

                            <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
                                <Label x:Name="LabelTitle" Padding="0, 10">
                                <Label.FormattedText>
                                    <FormattedString>
                                        <Span FontSize="Subtitle" Text="{Binding Title}" FontAttributes="Bold"></Span>
                                        <Span Text=" "></Span>
                                        <Span FontSize="Subtitle" Text="{Binding Version}" FontAttributes="Bold"></Span>
                                    </FormattedString>
                                </Label.FormattedText>
                            </Label>
                            </StackLayout>

                            <StackLayout Orientation="Horizontal">

                            <StackLayout HorizontalOptions="StartAndExpand">

                                <Label Text="{Binding Description}" FontSize="Body"></Label>-->
                                <Label FontSize="Caption" TextColor="#LELELE">
                                    <Label.FormattedText>
                                        <FormattedString>
                                            <Span Text="{Binding fDepartment}"></Span>
                                            <Span Text="{Binding pType}"></Span>
                                        </FormattedString>
                                    </Label.FormattedText>
                                </Label>

                            </StackLayout>

                            <StackLayout HorizontalOptions="End">

                            <Label Text="{Binding Comments}"></Label>

                            <Label FontFamily="{StaticResource IconFonts}" Text="&#xf139;"></Label>

                            <Label x:Name="LabelTypeTest" FontFamily="{StaticResource IconFonts}"
                                    Text="{Binding Comments, StringFormat='&#xf123;'}"></Label>

                            <Label FontFamily="{StaticResource IconFonts}" Text="{Binding Comments}"></Label>

                            <Image x:Name="ImageTypeTest">
                                <Image.Source>
                                    <FontImageSource Size="48" FontFamily="{StaticResource IconFonts}" Color="#000000"></FontImageSource>
                                </Image.Source>
                            </Image>

                            </StackLayout>

                            </StackLayout>

                        </StackLayout>
                    </Frame.Content>

            </helper:LayoutGradient>
        </StackLayout>
    </DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>

Here is the static resource in app

<ResourceDictionary>

    <OnPlatform x:Key="IconFonts" x:TypeArguments="x:String">

        <On Platform="iOS" Value="Material Design Icons"></On>

        <On Platform="Android" Value="IconFonts.ttf#Material Design Icons"></On>

        <On Platform="UWP" Value="/Assets/IconFonts.ttf#Material Design Icons"></On>

    </OnPlatform>

</ResourceDictionary>
Aaron B
  • 41
  • 1
  • 7
  • I can't reproduce this issue, but I am wondering about "Material Design Icons" value. That does not seem like a valid font family name, I don't think there should be spaces? Also, is this issue occurring on all platforms? – jgoldberger - MSFT Mar 31 '20 at 19:50
  • Oh, if these are font icons, did you set them as per this document? https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/fonts#display-font-icons , IOW it looks like they should be used in an Image, not a label – jgoldberger - MSFT Mar 31 '20 at 20:00
  • Can you please share us a Minimal, Reproducible Example with the font resources? I can't reproduce it on my side. – nevermore Apr 01 '20 at 06:09
  • i followed the instructions on a tutorial. Will try to find the source link. Ok will try out the image versus a label and that you mention it, it works on other area but I have to check to see if they are all under an image. – Aaron B Apr 01 '20 at 20:05
  • Ok, I will check once you share the source link:). – nevermore Apr 03 '20 at 03:24
  • Here is the source link https://montemagno.com/using-font-icons-in-xamarin-forms-goodbye-images-hello-fonts/ – Aaron B Apr 06 '20 at 15:26

1 Answers1

0

You should return the escape sequence of \uf139 from your binding instead of &#xf139;

You can read the Access from Bindings and Access Directly in XAML section in the source link for more information.

Refer: xamarin-forms-fontawesome-doesnt-work-with-bound-properties

nevermore
  • 15,432
  • 1
  • 12
  • 30
  • I've tried either and it still doesn't show the correct icon. I'm gonna go back to the drawing board with the original source I used to see where I'm messing up at https://montemagno.com/using-font-icons-in-xamarin-forms-goodbye-images-hello-fonts/ – Aaron B Apr 08 '20 at 19:31
  • Well, it should work with \uf139 and I will test it later. – nevermore Apr 09 '20 at 07:56
  • Couldn't figure out what I did wrong but I re did everything and it work thanks all – Aaron B Apr 20 '20 at 14:48