微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Xamarin.iOS中为整个iOS应用设置默认字体吗?

如何解决在Xamarin.iOS中为整个iOS应用设置默认字体吗?

我可以像这样为Label,Entry等(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有很多解决方案。

Objective-c Solution

Swift 5 solution

Another Solution

有人可以帮助我转换这些代码或提供其他解决方案吗?

编辑-

对话框是特定于设备的,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,TypefaceStyle.normal);
                var _buttonCancel = (Button)dialog.FindViewById(Android.Resource.Id.Button2);
                _buttonCancel.SetTypeface(Typeface.CreateFromAsset(Forms.Context.Assets,TypefaceStyle.normal);
            });

解决方法

您可以创建一个Custom Label Renderer来实现。

在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(Label),这意味着它将适用于iOS设备中的所有Xamarin表格标签。

=============================更新================ =================

我已签入本地站点,并使其适用于UILable。您需要在iOS解决方案中正确添加Montserrat-Regular.ttf文件。

例如:

enter image description here

并且还需要将此.ttf添加到 info.plist 中,如下所示:

<key>UIAppFonts</key>
<array>
    <string>Montserrat-Regular.ttf</string>
</array>

然后渲染器代码将起作用。

enter image description here

此外,您可以使用typeof(CustomLabel)将特殊效果用于特殊的Label

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

如果需要用于 UIAlertController ,请尝试以下方法。但是,在iOS中无法修改Button的字体,仅适用于TitleMessage

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,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",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);
});

效果:

enter image description here

,

您无法覆盖默认字体,您现在需要在各个组件上设置字体,如果发现更好的解决方案,我会通知您

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。