繁体   English   中英

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

[英]Set a default font for whole iOS app in Xamarin.iOS?

我可以像这样为标签、条目等(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 解决方案

斯威夫特 5 解决方案

另一种解决方案

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

编辑 -

对话框是特定于设备的,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);
            });

您可以创建一个自定义标签渲染器来实现这一点。

在 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 Forms 的所有标签。

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

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

例如:

在此处输入图片说明

并且还需要在info.plist 中添加这个.ttf如下:

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

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

在此处输入图片说明

此外,您可以使用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,
        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);
});

效果:

在此处输入图片说明

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM