[英]How to draw Japanese text with SkiaSharp on Xamarin
Now I'm using SkiaSharp on Xamarin Android. 现在我在Xamarin Android上使用SkiaSharp。
I wanna draw Japanese text with SKCanvas.DrawText ,but japanese characters were garbled. 我想用SKCanvas.DrawText绘制日文文本,但是日文字符是乱码。
I saw this similar question ,so I tried to convert text to UTF-8, but result is same. 我看到了类似的问题 ,所以我尝试将文本转换为UTF-8,但结果是一样的。
I tried like below. 我尝试过如下。
var s = "abcあいう123壱弐参";
var buf = System.Text.Encoding.UTF8.GetBytes(s);
var utf8s = Encoding.UTF8.GetString(buf);
skcanvas.DrawText(utf8s, 50, 50, paint);
another one. 另一个。
var s = "abcあいう123壱弐参";
var dest = Encoding.UTF8;
var src = Encoding.Default;
var buf = src.GetBytes(s);
var buf2 = Encoding.Convert(src,dest, buf);
var utf8s = dest.GetString(buf2);
skcanvas.DrawText(utf8s, 50, 50, paint);
Both result are same. 两个结果都是一样的。 "abc" and "123" is drawn well,but Japanese character are garbled.
“abc”和“123”画得很好,但是日文字符是乱码。
Any idea? 任何的想法?
Assuming you are loading a custom font, like Uzumasa Honmaru Gothic , you can add it to each of your native platform project and then use it via that native project or a Xamarin.Forms
-based one: 假设您正在加载自定义字体,例如Uzumasa Honmaru Gothic ,您可以将其添加到每个本机平台项目中,然后通过该本机项目或基于
Xamarin.Forms
的项目使用它:
string fontName = "UzumasaFontMini.otf";
string fontPath = Path.Combine (CacheDir.AbsolutePath, fontName);
using (var asset = Assets.Open (fontName))
using (var dest = File.Open (fontPath, FileMode.Create)) {
asset.CopyTo (dest);
}
string customFontPath = fontPath;
string fontName = "UzumasaFontMini.otf";
string customFontPath = NSBundle.MainBundle.PathForResource (Path.GetFileNameWithoutExtension (fontName), Path.GetExtension (fontName));
string text = "abcあいう123";
using (var paint = new SKPaint ()) {
canvas.Clear (SKColors.Black);
paint.IsAntialias = true;
using (var tf = SKTypeface.FromFile (customFontPath)) {
paint.Color = SKColors.White;
paint.TextSize = 20;
paint.Typeface = tf;
canvas.DrawText (text, 50, 50, paint);
}
}
Android: 安卓:
iOS: iOS版:
You can use "matchCharacter()", input param might be just a Japanese character, like "あ" 你可以使用“matchCharacter()”,输入参数可能只是一个日文字符,比如“あ”
var japanese = fontManager.MatchCharacter("Courier New", '年')
paint.Typeface = japanese;
canvas.DrawText("abcあいう123", x, 300, paint);
Refer link: https://github.com/mono/SkiaSharp/blob/master/samples/SkiaSharpSample.Shared/Samples/UnicodeTextSample.cs 参考链接: https : //github.com/mono/SkiaSharp/blob/master/samples/SkiaSharpSample.Shared/Samples/UnicodeTextSample.cs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.