0

我有一个 Xamarin Forms Shell 项目,在 .NET Standard 项目(嵌入式资源)中添加了自定义字体并像这样导出:

[assembly: ExportFont("Ubuntu-Regular.ttf", Alias = "UbuntuRegular")]
[assembly: ExportFont("Ubuntu-Bold.ttf", Alias = "UbuntuBold")]
[assembly: ExportFont("Ubuntu-Italic.ttf", Alias = "UbuntuItalic")]

我在我的 shell 中使用 TabBar 并想设置字体 Ubuntu。我正在使用自定义渲染器,对于 iOS 来说很简单,我只需要这样做

var normalTextAttributes = new UITextAttributes();
normalTextAttributes.Font = UIFont.FromName("Ubuntu-Regular", 10.0F);

uiTabBarItem.SetTitleTextAttributes(normalTextAttributes, UIControlState.Normal);

在我IShellTabBarAppearanceTracker继承的类的 UpdateLayout 方法中。但是我们如何在 Android 中访问这些字体,我尝试了类似的方法

var title = menuItem.TitleFormatted;
var typeface = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, "Ubuntu-Regular.ttf");
var sb = new SpannableStringBuilder(title);

sb.SetSpan(new CustomTypefaceSpan("", typeface), 0, sb.Length(), SpanTypes.InclusiveInclusive);
menuItem.SetTitle(sb);

此解决方案基于此帖子。但它不起作用。

4

0 回答 0