How to disable the font scaling for the Display alert's in Maui - iOS

Vaibhav Methuku 80 Reputation points
2024-05-21T13:40:20.09+00:00

Hello,
Currently I'm disabling the font scaling in iOS by following this.
https://learn.microsoft.com/en-us/answers/questions/1661536/how-can-i-prevent-the-device-font-size-effect-of-a

Now I want to achieve the same functionality for the display alerts as well. how to achieve this MAUI iOS.

Thanks
Vaibhav Methuku.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,045 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,746 Reputation points Microsoft Vendor
    2024-05-22T09:12:07.56+00:00

    Hello,

    You mean the Page.DisplayAlert Method, right?

    On iOS platform, it will display an AlertController, see the source code.

    To avoid it, you have to customize the font on AlertController, please refer to the following code:

    (Note: value for key method will call Apple's private Api. As described on Apple's doc - The view hierarchy for this class is private and must not be modified, you could customize a view/control looks like an Alert)

    {#if IOS
    		PopAlert("333", "111", "OK");
    #else
    //  DisplayAlert("333", "111", "OK");
    #endif
     
        }
     
        void PopAlert(string Title, string Message, string cancle)
    	{
            var alert = UIAlertController.Create(Title, Message, UIAlertControllerStyle.Alert);
            var fontTitle = UIFont.SystemFontOfSize(16);// avoid large text
            var fontMessage = UIFont.SystemFontOfSize(14);
     
            var titleAtrr = new NSAttributedString(Title, fontTitle);
            var messageAtrr = new NSAttributedString(Title, fontMessage);
            var action = UIAlertAction.Create("OK", UIAlertActionStyle.Default, (action) =>
    		{
    			alert.DismissModalViewController(true);
    		});
            ;
            alert.SetValueForKey(titleAtrr, new NSString("attributedTitle"));
            alert.SetValueForKey(messageAtrr, new NSString("attributedMessage"));
    		alert.AddAction(action);
     
            var actionArr = new NSAttributedString("OK", fontTitle);
     
            Microsoft.Maui.ApplicationModel.Platform.GetCurrentUIViewController().PresentViewController(alert, false, () =>
    		{// there is high risk to call the api
                var label = action.ValueForKey(new NSString("__representer"))?.ValueForKey(new NSString("label")) as UILabel;
                label.AttributedText = actionArr;
            });
     
     
        }
    

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful