简体   繁体   English

如何将 UIContentSizeCategory 转换为数字字体比例值?

[英]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.我有我在 SkiaSharp canvas 上绘制的文本,它不会自动调整到 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.我想在绘制时根据用户的设置缩放字体,但我不知道如何将 UIContentSizeCategory 转换为数值。 Is there a way to convert UIContentSizeCategory value to a numerical value?有没有办法将 UIContentSizeCategory 值转换为数值?

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.用户可以通过转到设置->显示和文本大小->更大的文本-> 启用更大的辅助功能大小并将 slider 向右拖动来更改 iPhone 上的字体比例。

在此处输入图像描述

The value matching this slider can be obtained as follows (in Xamarin C#)与这个slider匹配的值可以得到如下(在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 .不幸的是,我看不到将这个枚举值转换为比例值的方法,例如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).我想要一个数值来将我的文本渲染乘以一个单独的渲染系统(在这种情况下是 SkiaSharp,但它可以是其他任何东西,比如游戏)。 I also need to adjust my layout in SkiaSharp to accommodate for the larger font.我还需要调整 SkiaSharp 中的布局以适应更大的字体。

I know that this is possible on Xamarin Android using the following code:我知道这在 Xamarin Android 上是可能的,使用以下代码:

var globalFontScale = Resources.Configuration.FontScale;

Is there an equivalent value in Xamarin iOS? 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 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.不幸的是,那里列出的代码在ExtraExtraExtraLarge (值为 7)处停止,但如果用户选中较大的可访问性大小选项,则可以使用较大的“可访问性”值。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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