Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Windows Phone: Custom font

$
0
0

I have a custom font for an arrow (">") that does not quite work on Windows Phone, but works on both iPhone and Android.

Here is the code:

public class SymbolLabel : Label { };

var arrowLabel = new SymbolLabel
            {
                Text = "\u27a1",
                Font = Device.OnPlatform(Font.OfSize("SSGlyphish-Filled", NamedSize.Small), Font.SystemFontOfSize(NamedSize.Small), Font.SystemFontOfSize(NamedSize.Small)),
                XAlign = TextAlignment.End,
                YAlign = TextAlignment.Center,
            };

To make it work on iPhone, only Font.OfSize("SSGlyphish-Filled", NamedSize.Small) is needed (no custom rendering).

On the Android, I implement a custom renderer and it works:

[assembly: ExportRenderer (typeof (IslandsbankaApp.SymbolLabel), typeof (IslandsbankaApp.Droid.SymbolLabelRenderer))]
namespace IslandsbankaApp.Droid
{
    public class SymbolLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged (e);
            var label = (TextView)Control;
            Typeface font = Typeface.CreateFromAsset (Forms.Context.Assets, "ss-glyphish-filled.ttf");
            label.Typeface = font;
        }

    }
}

On WinPhone, I tried to implement a custom renderer like this:

[assembly: ExportRenderer (typeof (IslandsbankaApp.SymbolLabel), typeof (IslandsbankaApp.WinPhone.SymbolLabelRenderer))]
namespace IslandsbankaApp.WinPhone
{
    public class SymbolLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged (e);
            var label = (TextBlock)Control; 
            label.FontFamily = new FontFamily(@"\Assets\Fonts\ss-glyphish-filled.ttf#SSGlyphish-Filled");
        }
    }
}

and also without the custom renderer but directly referring to the font when setting the Font attribute:

Font = Device.OnPlatform(Font.OfSize("SSGlyphish-Filled", NamedSize.Small), Font.SystemFontOfSize(NamedSize.Small), Font.OfSize (@"\Assets\Fonts\ss-glyphish-filled.ttf#SSGlyphish-Filled", NamedSize.Small)),

Neither works, the font on WinPhone shows up as some default font.

(the .ttf file has Build Action = Content).

What am I doing wrong on WinPhone?


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>