更改Xamarin Forms应用程序的默认字体。

3
我需要更改应用程序的默认字体,以更改选项卡和导航栏的字体。

enter image description here


请参考以下链接。 - Anand
https://xamarinhelp.com/custom-fonts-xamarin-forms/ - Anand
https://devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms/ - Anand
1个回答

4
要更改NavigationBar中的标题字体,请阅读此doc,在每个contentPage中自定义Shell.TitleView
<Shell.TitleView>
    <Label Text="customTitle" FontSize="30"/>
</Shell.TitleView>

要更改选项卡栏标题字体,您需要使用自定义渲染器:
[assembly: ExportRenderer(typeof(AppShell), typeof(MyShellRenderer))]
namespace App30.Droid
{
    public class MyShellRenderer : ShellRenderer
    {
        public MyShellRenderer(Context context) : base(context)
        {
        }

        protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
        {
            return new CustomBottomNavAppearance();
        }
    }

    public class CustomBottomNavAppearance : IShellBottomNavViewAppearanceTracker
    {
        public void Dispose()
        {

        }

        public void ResetAppearance(BottomNavigationView bottomView)
        {

        }

        public void SetAppearance(BottomNavigationView bottomView, ShellAppearance appearance)
        {

            IMenu menu = bottomView.Menu;
            for (int i = 0; i < bottomView.Menu.Size(); i++)
            {
                IMenuItem menuItem = menu.GetItem(i);
                var title = menuItem.TitleFormatted;
                SpannableStringBuilder sb = new SpannableStringBuilder(title);

                int a = sb.Length();
                
                //here I set fontsize 20
                sb.SetSpan(new AbsoluteSizeSpan(20,true), 0, a, SpanTypes.ExclusiveExclusive);

                menuItem.SetTitle(sb);
            }

        }
    }
}

这是结果:

enter image description here

这里上传了一个示例项目,您可以查看。


非常感谢你,Jack。这很有用,但我想更改选项卡的字体,而不是大小。对不起,我是一个初学者。 - Omar Der
1
@DeraDera,看一下这个帖子,或许会有所帮助。 - nevermore

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接