简体   繁体   中英

How can I convert UIContentSizeCategory to a numerical font scale value?

Summary

I have text that I draw on a SkiaSharp canvas which does not automatically adjust to the Larger Accessibilty Sizes setting in iOS. I would like to scale the font when I draw it according to the user's setting, but I do not know how to convert UIContentSizeCategory to a numerical value. Is there a way to convert UIContentSizeCategory value to a numerical value?

Details

The user is able to change the font scale on their iPhone as shown by going to Settings -> Display & Text Size -> Larger Text -> enabling Larger Accessibility Sizes and dragging the slider to the right.

在此处输入图像描述

The value matching this slider can be obtained as follows (in Xamarin C#)

string sizeCategoryAsString = 
    UIKit.UIApplication.SharedApplication.PreferredContentSizeCategory;

var sizeCategoryAsEnum = 
    UIContentSizeCategoryExtensions.GetValue(UIApplication.SharedApplication.PreferredContentSizeCategory);

Unfortunately I do not see a way to convert this enumeration value to a scale value such as 1.5 .

I would like a numerical value to multiply my text rendering in a separate rendering system (SkiaSharp in this case, but it could be anything else like a game). I also need to adjust my layout in SkiaSharp to accommodate for the larger font.

I know that this is possible on Xamarin Android using the following code:

var globalFontScale = Resources.Configuration.FontScale;

Is there an equivalent value in Xamarin iOS?

I found a similar question posted here:

https://social.msdn.microsoft.com/Forums/en-US/5a95e5d6-1263-4ed4-bc03-dd6f54d7df04/accessibility-font-size

Unfortunately the code listed there stops at ExtraExtraExtraLarge (a value of 7), but larger "Accessibility" values are available if the user checks the Larger Accessibility Sizes option. Therefore, I did my own measurements and got these values. Note that these differ slightly from the values in the link above.

private double SizeEnumToValue(UIContentSizeCategory sizeCategory)
{
    double fontscale = 1;
    switch (sizeCategory)
    {
        case UIContentSizeCategory.ExtraSmall:
            fontscale = 0.882758620689655;
            break;
        case UIContentSizeCategory.Small:
            fontscale = 0.937931034482759;
            break;
        case UIContentSizeCategory.Medium:
            fontscale = 1;
            break;
        case UIContentSizeCategory.Large:
            fontscale = 1.0448275862069;
            break;
        case UIContentSizeCategory.ExtraLarge:
            fontscale = 1.16551724137931;
            break;
        case UIContentSizeCategory.ExtraExtraLarge:
            fontscale = 1.24827586206897;
            break;
        case UIContentSizeCategory.ExtraExtraExtraLarge:
            fontscale = 1.36551724137931;
            break;
        case UIContentSizeCategory.AccessibilityMedium:
            fontscale = 1.64137931034483;
            break;
        case UIContentSizeCategory.AccessibilityLarge:
            fontscale = 1.93793103448276;
            break;
        case UIContentSizeCategory.AccessibilityExtraLarge:
            fontscale = 2.33793103448276;
            break;
        case UIContentSizeCategory.AccessibilityExtraExtraLarge:
            fontscale = 2.77931034482759;
            break;
        case UIContentSizeCategory.AccessibilityExtraExtraExtraLarge:
            fontscale = 2.99310344827586;
            break;
    }
    return fontscale;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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