如何使用Skia或SkiaSharp绘制富文本 [英] How to draw rich text with Skia or SkiaSharp

查看:1096
本文介绍了如何使用Skia或SkiaSharp绘制富文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 SkiaSharp 绘制丰富的文本,例如iOS的属性文本或Xamarin.Forms的FormattedString,但我找不到方法.

I want to draw rich text like iOS's Attributed Text or Xamarin.Forms's FormattedString with SkiaSharp, but I can't find how to.

我找到了DrawText方法,但这是用于使用一种颜色和一种字体进行简单文本呈现的方法.没有混合的颜色和/或字体,没有粗体,斜体,删除线或下划线等样式.

I found the DrawText method, but it's for simple text rendering with one color and one font. No mixed colors and/or fonts and no styles like bold, italic, strike-through, or underline.

我必须使用自己的RTF呈现逻辑吗?

Do I have to do it with my own rich text rendering logic?

推荐答案

使用SKPaint类型可以做到这一点.基本上,在绘制时,您可以在绘画上设置属性:

This is doable using the SKPaint type. Basically, as you draw, you set the properties on the paint:

var paint = new SKPaint();
paint.StrikeThruText = true;
paint.TextSize = 24;
paint.Color = SKColors.Yellow;
paint.UnderlineText = true;
paint.Typeface = SKTypeface.FromFamilyName(
    "Arial", 
    SKFontStyleWeight.Bold, 
    SKFontStyleWidth.Normal, 
    SKFontStyleSlant.Italic);

然后您可以绘制:

canvas.DrawText("Fancy Text", 30, 30, paint);

我希望这会有所帮助!

对于其他效果,可以使用SKShaderSKMaskFilter类型.这会造成模糊:

For other effects, you can use the SKShader and SKMaskFilter types. This does a blur:

path.MaskFilter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 5);

编辑

一段时间后,看来我们实际上有更好的方式来绘制文本-而不仅仅是基本的下划线.我会全力推荐Brad Robinson编写的该库: https://github.com/toptensoftware/RichTextKit

After some time, it seems we actually have a much better way to draw text - and not just the basic underlines. I would whole-heartedly recommend this library by Brad Robinson: https://github.com/toptensoftware/RichTextKit

我的意思是,只看那美丽的东西!

I mean, just look at that beautiful thing!

这篇关于如何使用Skia或SkiaSharp绘制富文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆