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

nszi6y05  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(142)

我需要更改应用程序的默认字体,更改标签和NavigationBar的字体。


的数据

ylamdve6

ylamdve61#

要更改NavigationBar中的标题字体,请阅读此文档,在每个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);
            }

        }
    }
}


结果如下:


的数据
一个sample project已经上传到这里,你可以检查它。

相关问题