How To Get Path Of Font File From Asset Folder And How To Use That Path To Set Sktypeface In Xamarin Android?
I am working in native xamarin andorid. I am facing issue to set custom font. I am using 'SkiaSharp' plugin to generate canvas view. That plugin is inside PCL so that can be use in
Solution 1:
You typically don't use the path for the android assets as they are located inside the app package. Rather you get a stream and load that - however asset streams are not seekable, so they have to be copied first:
SKTypeface typeface;
using (var asset = Assets.Open("Fonts/CONSOLA.TTF"))
{
var fontStream = new MemoryStream();
asset.CopyTo(fontStream);
fontStream.Flush();
fontStream.Position = 0;
typeface = SKTypeface.FromStream(fontStream);
}
...
paint.Typeface = typeface;
I also want to make a note that this is relatively "slow" so you probably want to do this once and then just re-use the typeface. Avoid loading files directly in the paint methods as the paint happens on the UI thread and will block.
When loading assets, do not include the root "Assets" folder as this is assumed.
EDIT
In the new v1.59.2 release, the existing FromStream
method will work as expected, regardless of the stream.
Post a Comment for "How To Get Path Of Font File From Asset Folder And How To Use That Path To Set Sktypeface In Xamarin Android?"