1

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);
            });
Blu
  • 821
  • 4
  • 17

2 Answers2

2

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

Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
  • i will try this & let you know, but I guess it isn't the solution, cuz i did the same for android extending LabelRenderer to set font. It changed the font for only label components, but in actual, the dialog box of android uses TEXTVIEW for the message part, not the labels, soo the font didn't change for Dialog box. The same applies for the custom list created programmatically, couldnt change the font for it by LabelRenderer – Blu Sep 08 '20 at 06:35
  • Actually i dont know much about iOS, soo idk what components are used in dialog boxes & lists in iOS... This is how i did for Android, https://stackoverflow.com/a/63706206/12768925 – Blu Sep 08 '20 at 06:35
  • @Blu Okey, I have seen your updated question, I will check that. – Junior Jiang Sep 08 '20 at 06:52
  • thanks, let me know if you find any updates on it :) – Blu Sep 08 '20 at 06:54
  • @Blu I have updated answer, you can have a try when you have time. – Junior Jiang Sep 08 '20 at 09:56
  • HI, thanks for it, its working now for dialog box, but it didnt solve my compete problem. I wanted solution to override default present font in iOS system cuz i still got mant places to over ride – Blu Sep 09 '20 at 08:24
  • some kind of solution like i used for android, this way - https://stackoverflow.com/a/63706206/12768925 . OR the solution like links provided in my question....Something like to change complete font globally – Blu Sep 09 '20 at 08:29
  • @Blu Yes, in iOS can not change complete font globally. If the reply is helpful, please do not forget to accept it as answer( click the ✔ in the upper left corner of this answer), it will help others who have similar issue. – Junior Jiang Sep 09 '20 at 08:49
1

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