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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

8 Comments

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
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, stackoverflow.com/a/63706206/12768925
@Blu Okey, I have seen your updated question, I will check that.
thanks, let me know if you find any updates on it :)
@Blu I have updated answer, you can have a try when you have time.
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.