[英]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.