C# 在Xamarin.iOS中为整个iOS应用程序设置默认字体?

C# 在Xamarin.iOS中为整个iOS应用程序设置默认字体?,c#,ios,xamarin,xamarin.forms,xamarin.ios,C#,Ios,Xamarin,Xamarin.forms,Xamarin.ios,我可以像这样为标签、条目等(Xamarin.Forms UI字段)启用字体 <Style TargetType="Label"> <Setter Property="FontFamily"> <Setter.Value> <OnPlatform x:TypeArguments

我可以像这样为标签、条目等(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>
Android对话框和字体修复程序-

你可以创造一个机会来实现这一点

在iOS解决方案中创建CustomLabelRenderer

[assembly: ExportRenderer(typeof(Label), typeof(CustomLabelRenderer))]
namespace XamarinTableView.iOS
{
    public class CustomLabelRenderer: LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            Control.Font = UIFont.FromName("Montserrat-Regular", (nfloat)Element.FontSize);
        }
    }
}
然后渲染器代码就可以工作了

此外,您可以使用
typeof(CustomLabel)
为特殊
标签
使用特殊效果

==========================================================更新#2================================

如果需要为UIAlertController工作,请尝试以下方法。但是,在iOS中无法修改
按钮
的字体,它只适用于
标题
消息

Device.BeginInvokeOnMainThread(() => 
{
    UIAlertController alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
    var titleAttributedString = new NSAttributedString(title,
        new CTStringAttributes()
        {
            ForegroundColorFromContext = true,
            Font = new CTFont("Montserrat-Regular", 24)
        });
    alert.SetValueForKey(titleAttributedString, new NSString("attributedTitle"));
    var messageAttributedString = new NSAttributedString(message,
        new CTStringAttributes()
        {
            ForegroundColorFromContext = true,
            Font = new CTFont("Montserrat-Regular", 24)
        });
    alert.SetValueForKey(messageAttributedString, new NSString("attributedMessage"));
    UIAlertAction cancleAction = UIAlertAction.Create("Cancle", UIAlertActionStyle.Cancel, a => Console.WriteLine("cancle"));
    alert.AddAction(cancleAction);
    UIAlertAction okAction = UIAlertAction.Create("OK", UIAlertActionStyle.Cancel, a => Console.WriteLine("OK"));
    alert.AddAction(okAction);
    UIViewController vc = GetViewController(UIApplication.SharedApplication.KeyWindow.RootViewController);
    if (TargetIdiom.Tablet == Device.Idiom)
    {
        vc.ModalPresentationStyle = UIModalPresentationStyle.Popover;
    }
    vc.PresentModalViewController(alert, true);
});
其效果是:


您不能覆盖默认字体,您现在需要在各个组件上设置字体,如果我找到了更好的解决方案,我会告诉您的

我会试试这个&让您知道,但我想这不是解决方案,因为我对android扩展LabelRenderer设置字体也做了同样的事情。它只更改了标签组件的字体,但实际上,android对话框的消息部分使用TEXTVIEW,而不是标签,所以对话框的字体没有更改。这同样适用于以编程方式创建的自定义列表,无法更改LabelRenderer为其设置的字体实际上我对iOS不太了解,所以idk在iOS中的对话框和列表中使用了哪些组件。。。这就是我对安卓系统所做的,@Blu好的,我看到了你更新的问题,我会检查一下。谢谢,如果你发现它有任何更新,请告诉我:)@Blu我更新了答案,你有时间可以试试。
[assembly: ExportRenderer(typeof(Label), typeof(CustomLabelRenderer))]
namespace XamarinTableView.iOS
{
    public class CustomLabelRenderer: LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            Control.Font = UIFont.FromName("Montserrat-Regular", (nfloat)Element.FontSize);
        }
    }
}
<key>UIAppFonts</key>
<array>
    <string>Montserrat-Regular.ttf</string>
</array>
Device.BeginInvokeOnMainThread(() => 
{
    UIAlertController alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);
    var titleAttributedString = new NSAttributedString(title,
        new CTStringAttributes()
        {
            ForegroundColorFromContext = true,
            Font = new CTFont("Montserrat-Regular", 24)
        });
    alert.SetValueForKey(titleAttributedString, new NSString("attributedTitle"));
    var messageAttributedString = new NSAttributedString(message,
        new CTStringAttributes()
        {
            ForegroundColorFromContext = true,
            Font = new CTFont("Montserrat-Regular", 24)
        });
    alert.SetValueForKey(messageAttributedString, new NSString("attributedMessage"));
    UIAlertAction cancleAction = UIAlertAction.Create("Cancle", UIAlertActionStyle.Cancel, a => Console.WriteLine("cancle"));
    alert.AddAction(cancleAction);
    UIAlertAction okAction = UIAlertAction.Create("OK", UIAlertActionStyle.Cancel, a => Console.WriteLine("OK"));
    alert.AddAction(okAction);
    UIViewController vc = GetViewController(UIApplication.SharedApplication.KeyWindow.RootViewController);
    if (TargetIdiom.Tablet == Device.Idiom)
    {
        vc.ModalPresentationStyle = UIModalPresentationStyle.Popover;
    }
    vc.PresentModalViewController(alert, true);
});