c# - 将当前平台字体传递给 SKTypeface?

标签 c# fonts xamarin.forms skiasharp

尝试呈现中文(或其他符号)文本时。 SkiSharp 将呈现框而不是正确的汉字。显然,Skia 默认使用的字体不支持这些字符。所以我们必须使用支持这些字符的字体来分配我们自己的 SKTypeface。

我最初的策略是简单地包含必要的字体来呈现这些字符,效果很好。然而,当使用自己的字体支持多种不同的符号语言时,应用程序的大小会急剧增加(每种字体大约 15 MB)。

所以再考虑一下......默认的平台字体似乎可以很好地支持这些符号字符中的任何。我的意思是,default 使用的字体完美呈现按钮、标签和标题。

所以我目前的想法是,为什么我不能将任何字体传递到 SKTypeface 中供我控制?

问题是我不知道如何获取后备或默认字体,以便用它创建新的 SKTypeface。

我的问题

我如何使用可以很好地呈现这些按钮、标签和标题的相同字体创建 SKTypeface?


note: if you need anything from me to help you understand the problem or solve the problem just let me know.

最佳答案

您应该能够使用 SKFontManager.MatchCharacter或其重载之一,以便:

Use the system fallback to find a typeface for the given character.

下面是一个基于 SkiaSharp WindowsSample's TextSample 的例子:

public class TextSample : SampleBase
{
    // ...
    protected override void OnDrawSample(SKCanvas canvas, int width, int height)
    {
        canvas.DrawColor(SKColors.White);
        const string text = "象形字";
        var x = width / 2f;
        // in my case the line below returns typeface with FamilyName `Yu Gothic UI`
        var typeface = SKFontManager.Default.MatchCharacter(text[0]);
        using (var paint = new SKPaint())
        {
            paint.TextSize = 64.0f;
            paint.IsAntialias = true;
            paint.Color = (SKColor)0xFF4281A4;
            paint.IsStroke = false;
            paint.Typeface = typeface; // use typeface for the first one
            paint.TextAlign = SKTextAlign.Center;

            canvas.DrawText(text, x, 64.0f, paint);
        }

        using (var paint = new SKPaint())
        {
            // no typeface here
            paint.TextSize = 64.0f;
            paint.IsAntialias = true;
            paint.Color = (SKColor)0xFF9CAFB7;
            paint.IsStroke = true;
            paint.StrokeWidth = 3;
            paint.TextAlign = SKTextAlign.Center;

            canvas.DrawText(text, x, 144.0f, paint);
        }
    }
}

下面是输出

Example output

关于c# - 将当前平台字体传递给 SKTypeface?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48053615/

相关文章:

c# - SQL Server 2012 几何 - 使用 SqlDataAdapter 加载模式

c# - 在 ASP.NET MVC Core 2 中使用 MetadataPropertyHandling 模型绑定(bind) JSON 数据

css - 不知从哪里加载字体

css - 禁止将字体渲染为表情符号

xamarin - Xamarin Forms 中不同类型的导航

c# - Xamarin Studio 中的断点未命中

c# - 如何从数据表中获取列值

c# - 使用 session 自定义 RoleProvider

html - CSS - 为什么中文网站要用英文font-family

xaml - 如何禁用 ListView 中的单击? xamarin.形式