Xamarin forms: Custom Fonts in UWP and Windows 8.1

假装没事ソ 提交于 2019-12-11 06:08:20

问题


I am developing an app in which I am using a font.otf file. My app will run on android, ios, windows 10 and windows 8.1. I need to create styles for my labels to set font family. For android and ios I referenced this link

I tried in xaml page like this-

<Label.FontFamily>
        <OnPlatform x:TypeArguments="x:String">
            <OnPlatform.iOS></OnPlatform.iOS>
            <OnPlatform.Android>Lobster-Regular.ttf#Lobster-Regular</OnPlatform.Android>
            <OnPlatform.WinPhone></OnPlatform.WinPhone>
        </OnPlatform>
    </Label.FontFamily>

new Label {
    Text = "Hello, Forms!",
    FontFamily = Device.OnPlatform (
        null,
        null,
        @"\Assets\Fonts\Lobster-Regular.ttf#Lobster-Regular"
                 // Windows Phone will use this custom font
    )
}

But when I run my app Font does not set for Windows 10 and 8.1.

How can I set font family for windows 10 and 8.1. Or is there more efficient way to apply font family with covering all the platforms?


回答1:


Note: there is a bug where custom fonts do not work if you are using a NavigationPage

You do not need a custom renderer for custom fonts on Windows 8.1/UWP; the Xamarin sample code simply has a couple of mistakes:

  • Use a forward slash ('/') instead
  • The path should be in the form of [font file]#[font name] (without the font style, e.g. 'regular')

So the path in this case should actually be

"Assets/Fonts/Lobster-Regular.ttf#Lobster"

And you can also use this in Xaml

<OnPlatform.WinPhone>Assets/Fonts/Lobster-Regular.ttf#Lobster</OnPlatform.WinPhone>

Make sure your font file is included in your project with BuildAction:Content and it should work.




回答2:


You could try create a CustomRenderer which overrides the Xamarin.Forms.Label on the Windows platform like so:

[assembly: ExportRenderer(typeof(Xamarin.Forms.Label), typeof(MyLabelRenderer))]
namespace MyApp.CustomRenderers.Controls
{
    public class MyLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                var font = new FontFamily(@"\Assets\Fonts\Lobster-Regular.ttf#Lobster-Regular");

                if (e.NewElement != null)
                {
                    switch (e.NewElement.FontAttributes)
                    {
                        case FontAttributes.None:
                            break;
                        case FontAttributes.Bold:
                            //set bold font etc
                            break;
                        case FontAttributes.Italic:
                            //set italic font etc
                            break;
                        default:
                            break;
                    }
                }
                Control.FontFamily = font;
            }
        }     
    }


来源:https://stackoverflow.com/questions/41701098/xamarin-forms-custom-fonts-in-uwp-and-windows-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!