使用本地磁盘中的字体文件

问题描述 投票:2回答:1

我正在使用C#开发一个UWP应用程序来预览用户本地磁盘上的字体文件。我正在使用SharpDX库来读取.ttf文件内容并查询不同的字体属性。我的问题是:

如何将TextBlock FontFamily设置为加载的字体?

当然,使用加载的字体名称设置FontFamily属性不起作用,因为系统上不需要安装加载的字体。

请帮忙。

c# xaml uwp sharpdx directwrite
1个回答
1
投票

只要您没有完整的文件系统权限,就无法加载字体。

但你可以做的是以下几点:

I.让用户从系统中选择一个字体文件(我猜你已经完成了这个任务)

II。将检索到的StorageFile复制到应用程序的沙箱中。

async Task<StorageFile> ImportFont(StorageFile file)
{
  StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("fontcache", CreationCollisionOption.OpenIfExists);
  return await file.CopyAsync(folder, file.Name, NameCollisionOption.ReplaceExisting);
}

III。接下来,创建导入文件的路径(here are all available URI schemes listed

StorageFile importedFile = await ImportFont(userPickedFile);
string fontPath = "ms-appdata:///local/fontcache/" + importedFile.Name;

IV。根据您的路径创建字体系列

string fontName = sharpDXFont.Name; // this is the name SharpDX has extracted
FontFamily family = new FontFamily(fontPath + "#" + fontName);

那就是它!

当不再需要字体时,不要忘记清理本地文件夹。

© www.soinside.com 2019 - 2024. All rights reserved.