I Am The Blu
I Am The Blu

Reputation: 863

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

I can enable fonts for Label, entry, etc(Xamarin.Forms UI fields) like this...

           <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>

But it doesn't apply font for title & content of dialog boxes, list in pickers n many more places like that(Internal iOS UI fields).

I override the default font of android to achieve the above issue, but the problem is with iOS, I can't find any solution for it in C#. There are many solutions present in objective-c & swift.

Objective-c Solution

Swift 5 solution

Another Solution

Can someone help me convert these codes or provide any other solution?

EDIT -

Dialog Boxes are Device Specific, Xamarin.Forms code won't work individually on it.

iOS Dialog box -

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 Dialog box with fix for font -

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

Upvotes: 1

Views: 1120

Answers (2)

Aesthetic Sabine
Aesthetic Sabine

Reputation: 19

you can't override default font, you need to set font on individual components for now, I'll let you know if i found any better solution

Upvotes: 1

Junior Jiang
Junior Jiang

Reputation: 12721

You can creat a Custom Label Renderer to achieve that.

Create the CustomLabelRenderer in iOS solution:

[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);
        }
    }
}

You will see the typeof(Label), it means will work for all the Label of Xamarin Forms in iOS device.

==============================Update=================================

I have checked in local site and make it works for UILable. You need to add Montserrat-Regular.ttf file inside the iOS solution correctly.

For example:

enter image description here

And also need to add this .ttf in info.plist as follows:

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

Then the renderer code will work.

enter image description here

In addition, you can use typeof(CustomLabel) to use the special effect for special Label.

=================================Update #2================================

If need to works for UIAlertController ,have a try with follow ways. However, there is no way to modify the font of Button in iOS, it only works for Title and Message.

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

The effect:

enter image description here

Upvotes: 2

Related Questions