android-如何在Xamarin上用SkiaSharp绘制日语文本
作者:互联网
现在,我在Xamarin Android上使用SkiaSharp.
我想用SKCanvas.DrawText绘制日语文本,但是日语字符乱码.
我看见了这个
similar question,所以我尝试将文本转换为UTF-8,但是结果是相同的.
我尝试如下.
var s = "abcあいう123壱弐参";
var buf = System.Text.Encoding.UTF8.GetBytes(s);
var utf8s = Encoding.UTF8.GetString(buf);
skcanvas.DrawText(utf8s, 50, 50, paint);
另一个.
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);
两者的结果是相同的.很好地绘制了“ abc”和“ 123”,但是日语字符显示为乱码.
任何想法?
解决方法:
假设您正在加载一种自定义字体(如Uzumasa Honmaru Gothic),则可以将其添加到每个本机平台项目中,然后通过该本机项目或基于Xamarin.Forms的项目使用它:
Android:
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;
iOS:
string fontName = "UzumasaFontMini.otf";
string customFontPath = NSBundle.MainBundle.PathForResource (Path.GetFileNameWithoutExtension (fontName), Path.GetExtension (fontName));
DrawText:
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:
标签:xamarin,skiasharp,android 来源: https://codeday.me/bug/20191027/1940685.html