【发布时间】:2020-09-07 13:49:18
【问题描述】:
我可以像这样为标签、条目等(Xamarin.Forms UI 字段)启用字体...
<Style TargetType="Label">
<Setter Property="FontFamily">
<Setter.Value>
<OnPlatform x:TypeArguments="x:String">
<On Platform="iOS" Value="Montserrat_Regular" />
<On Platform="Android" Value="Montserrat_Regular.ttf#Montserrat_Regular" />
</OnPlatform>
</Setter.Value>
</Setter>
</Style>
但它不会为对话框的标题和内容应用字体,在选择器中列出更多类似的地方(内部 iOS UI 字段)。
我覆盖了android的默认字体来实现上述问题,但问题出在iOS上,我在C#中找不到任何解决方案。 在objective-c & swift中有很多解决方案。
有人可以帮我转换这些代码或提供任何其他解决方案吗?
编辑-
对话框是特定于设备的,Xamarin.Forms 代码不能单独在上面工作。
iOS 对话框 -
Device.BeginInvokeOnMainThread( () =>
{
UIAlertController alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create(cancel, UIAlertActionStyle.Cancel, a => task.SetResult(false)));
alert.AddAction(UIAlertAction.Create(ok, UIAlertActionStyle.Default, a => task.SetResult(true)));
UIViewController vc = GetViewController(UIApplication.SharedApplication.KeyWindow.RootViewController);
if (TargetIdiom.Tablet == Device.Idiom)
{
vc.ModalPresentationStyle = UIModalPresentationStyle.Popover;
}
vc.PresentModalViewController(alert, true);
});
带有字体修复的 Android 对话框 -
Device.BeginInvokeOnMainThread( () =>
{
AlertDialog dialog = new AlertDialog.Builder(Forms.Context, Resource.Style.Base_Animation_AppCompat_Tooltip).SetTitle(title).SetMessage(content).SetPositiveButton(ok, delegate { task.SetResult(true); })
.SetNegativeButton(cancel, delegate { task.SetResult(false); }).Show();
TextView textView = (TextView)dialog.FindViewById(Android.Resource.Id.Message);
textView.SetTypeface(Typeface.CreateFromAsset(Forms.Context.Assets, "Montserrat_Regular.ttf"), TypefaceStyle.Normal);
var _buttonOK = (Button)dialog.FindViewById(Android.Resource.Id.Button1);
_buttonOK.SetTypeface(Typeface.CreateFromAsset(Forms.Context.Assets, "Montserrat_Regular.ttf"), TypefaceStyle.Normal);
var _buttonCancel = (Button)dialog.FindViewById(Android.Resource.Id.Button2);
_buttonCancel.SetTypeface(Typeface.CreateFromAsset(Forms.Context.Assets, "Montserrat_Regular.ttf"), TypefaceStyle.Normal);
});
【问题讨论】:
标签: c# ios xamarin xamarin.forms xamarin.ios