简体   繁体   English

Skia Sharp DrawText 不考虑 Android 辅助功能设置(字体大小)

[英]Skia Sharp DrawText not considering Android accessibility settings (Font Size)

If user changes the Font Size from default to large or largest in androids accessibility settings all labels are scaled up.如果用户在 android 辅助功能设置中将字体大小从默认更改为大或最大,则所有标签都会按比例放大。 But drawing text with DrawText method on a canvas has no effect.但是在画布上用 DrawText 方法绘制文本没有效果。

Is this the expected behaviour?这是预期的行为吗? I thought that text which was drawn with skia sharp also scales up.我认为用 skia sharp 绘制的文本也会按比例放大。
What is the correct way to work with android accessibility changes in skia sharp?在 skia sharp 中使用 android 辅助功能更改的正确方法是什么?

Thank you谢谢

The only way I know to find the scaling is via a deprecated Android API:我知道找到缩放比例的唯一方法是通过已弃用的 Android API:

// First, test this in YourApp.Android project's MainActivity.cs.

// Returns "1" at default scale. Larger value when font should be larger.
public float CurrentFontScale()
{
    Android.Util.DisplayMetrics metrics = new Android.Util.DisplayMetrics();
    // Deprecated. But if it compiles, it works.
    WindowManager.DefaultDisplay.GetMetrics(metrics);
    float scaledDensity = metrics.ScaledDensity;
    return scaledDensity;
}

// Call it from here:
protected override void OnCreate(Bundle bundle){
{
    ...
    base.OnCreate(bundle);

    float fontScale = CurrentFontScale();

    ...
}

MULTIPLY all Skia font sizes by this.乘以所有 Skia 字体大小。

After confirming the code works in MainActivity.cs , to use this in a cross-platform Xamarin project, create a Xamarin.Forms DependencyService .确认代码在MainActivity.cs中有效后,要在跨平台 Xamarin 项目中使用它,请创建Xamarin.Forms DependencyService How to do that is beyond scope of this answer.如何做到这一点超出了这个答案的范围。


As a practical matter, consider "limiting" how large a given font size grows.作为一个实际问题,考虑“限制”给定字体大小的增长。 Otherwise, layout becomes difficult.否则,布局变得困难。 Eg myFontSize = Math.Min(20 * fontScale, 48);例如myFontSize = Math.Min(20 * fontScale, 48); for a text string that would be too large at fontsize greater than 48.对于字体大小大于 48 时太大的文本字符串。

An alternative is to use SKPaint.MeasureText(somestring) , after creating an skpaint with desired font size, and limit the fontSize if text gets too long:另一种方法是使用SKPaint.MeasureText(somestring) ,在创建具有所需字体大小的 skpaint 之后,如果文本太长则限制字体大小:

// --- Auto-fit Skia text. ---
// These are typically parameters.
string somestring = "Hello, World";
float MaxAllowedWidth = 100;   // Pixels.
float fontSize = 20 * fontScale;

SKFont font = new SKFont(typeface, fontSize);
SKPaint textPaint = new SKPaint(font);
float textWidth = textPaint.MeasureText(somestring);
if (textWidth > MaxAllowedWidth)
{
    // --- Shrink as needed. ---
    // "Floor" to avoid being slightly too wide sometimes.
    fontSize = Math.Floor(fontSize * MaxAllowedWidth / textWidth);
    font = new SKFont(typeface, fontSize);
    textPaint = new SKPaint(font);

    // OPTIONAL VERIFY: This should now be less than MaxAllowedWidth.
    textWidth = textPaint.MeasureText(somestring);
}

... paint the text ...

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

相关问题 Android - 如果使用 androidx,则不应用可访问性字体大小的系统设置 - Android - System settings for accessibility font size are not applied if using androidx android canvas drawText从宽度设置字体大小? - android canvas drawText set font size from width? 如何使用canvas.drawText android匹配TextView的字体大小 - How to match the font size of a TextView with canvas.drawText android Android应用程序字体大小根据设置中定义的字体大小而变化 - Android applications font size changes according to the defined font size in settings 覆盖android系统设置的字体大小 - Override the font size of the android system settings 用户更改系统/辅助功能设置字体大小时,是否可以让系统通知我的应用程序? - Is it possible to let system notify my app when user change system/accessibility settings font size? 在更改显示设置中的字体大小时,Android TextSwitcher大小会增加 - Android TextSwitcher size increasing on changing the font size in the display settings 自定义字体大小设置 - Custom font size settings 如何在 Xamarin.Forms 中获取 iOS 和 Android 的辅助功能字体大小,以便我可以在 HTML WebView 中更改字体大小? - How to get the accessibility font size for iOS and Android in Xamarin.Forms so I can change font-size in a HTML WebView? Android应用:如何阅读设置下的字体大小? - Android App: how to read Font Size under Settings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM