更改应用程序 Xamarin Forms 的默认字体
Change default font of the application Xamarin Forms
我需要更改应用程序的默认字体,以更改选项卡和导航栏的字体。
对不起,我的英语不好。
要更改 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);
}
}
}
}
结果如下:
此处已上传sample project,您可以查看
我需要更改应用程序的默认字体,以更改选项卡和导航栏的字体。
对不起,我的英语不好。
要更改 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);
}
}
}
}
结果如下:
此处已上传sample project,您可以查看