简体   繁体   English

Xamarin表单上的SkiaSharp文字大小

[英]SkiaSharp Text Size on Xamarin Forms

文字大小比较

How does the TextSize property on an SKPaint object relate to the 'standard' Xamarin Forms FontSize? SKPaint对象上的TextSize属性如何与“标准” Xamarin Forms FontSize相关联?

In the image you can see the difference between size 40 on a label and as painted. 在图像中,您可以看到标签上的尺寸40和涂漆的尺寸之间的差异。 What would I need to do to make them the same size? 我需要怎么做才能使它们大小相同?

As @hankide mentioned, it has to do with the fact that the native OS has scaling for UI elements so the app "looks the same size" on different devices. 正如@hankide提到的,这与以下事实有关:本机OS可以缩放UI元素,因此该应用程序在不同设备上“看起来相同”。

This is great for buttons and all that as the OS is drawing them. 这对于按钮以及操作系统绘制按钮时的所有操作非常有用。 So if the button is bigger, the OS just scales up the text. 因此,如果按钮较大,则操作系统只会放大文本。 However, with SkiaSharp, we have no idea what you are drawing so we can't do any scaling. 但是,使用SkiaSharp,我们不知道您在绘制什么,因此我们无法进行任何缩放。 If we were to scale, the image would become blurry or pixelated on the high resolution screens. 如果要缩放,则在高分辨率屏幕上图像会变得模糊或像素化。

One way to get everything the same size is to do a global scale before drawing anything: 获得所有相同大小的一种方法是在绘制任何东西之前先进行全局缩放:

var scale = canvasWidth / viewWidth;
canvas.Scale(scale);

And this is often good enough, but sometimes you really want to draw items differently on a high resolution screen. 这通常足够好,但有时您确实希望在高分辨率屏幕上以不同的方式绘制项目。 An example would be a tiled background. 一个例子就是平铺的背景。 Instead of stretching the image on a bigger canvas, you may want to just tile it - preserving the pixels. 与其在更大的画布上拉伸图像,不如将其平铺-保留像素。

In the case of this question, you can either scale the entire canvas before drawing, or you can just scale the text: 对于此问题,您可以在绘制之前缩放整个画布,也可以只缩放文本:

var paint = new SKPaint {
    TextSize = 40 * scale
};

This way, the text size is increased, but the rest of the drawing is on a larger canvas. 这样,文本大小会增加,但是图形的其余部分在更大的画布上。

I have an example on GitHub: https://github.com/mattleibow/SkiaSharpXamarinFormsDemo 我在GitHub上有一个示例: https : //github.com/mattleibow/SkiaSharpXamarinFormsDemo

This compares Xamarin.Forms, SkiaSharp and Native labels. 这将比较Xamarin.Forms,SkiaSharp和Native标签。 (They should all be exactly the same size) (它们都应该完全一样大小)

I think that the problem is in the way Xamarin.Forms handles font sizes. 我认为问题出在Xamarin.Forms处理字体大小的方式上。 For example on Android, you could define the font size in pixels (px), scale-independent pixels (sp), inches (in), millimeters and density-independent pixels (dp/dip). 例如,在Android上,您可以以像素(px),与比例无关的像素(sp),英寸(in),毫米和与密度无关的像素(dp / dip)定义字体大小。

I can't remember how Xamarin.Forms handles the sizes (px,sp or dp) but the difference you see here is because of that. 我不记得Xamarin.Forms是如何处理大小(px,sp或dp)的,但是您在这里看到的不同是因为这一点。 What you could do, is create an Effect that changes the font size handling on the native control and try to match the sizing provided by SkiaSharp. 您可以做的是创建一个效果,该效果可以更改本机控件上的字体大小处理,并尝试匹配SkiaSharp提供的大小。

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

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