0

我为我的 UWP 程序制作了自己的自定义 .ttf 字体,并将它们放在 Assets 文件夹中(与 2017/2019 相比)。在 RichEditBox 中处理 Document 时,它们运行良好。但是,当我保存 RTF 文件然后打开它时,我的自定义字体将被忽略。如果我事先将自定义字体安装到 Windows\Fonts 文件夹中,则打开文件会使用我的自定义字体加载文档。看起来如果没有安装我的自定义字体,程序不会将它们链接到文档。

再次 - 我用 RichEditBox 和我在该程序中的自定义字体编写了一个程序。处理后 - 字体更改,样式更改等 - 一切都按设计进行。当我用那个程序保存 RTF 文件,并用那个(相同的)程序打开那个 RTF 时 - 颜色表没问题,但我的字体没有显示,尽管字体是用那个程序编译的(BuildAction - 内容;CopyToOutputDirectory - 总是复制)。为了简化 - 我用文件包含的信息制作了按钮。尽管字体已编译(位于 Assets 文件夹中),但程序不会将它们链接到文档。

实际上,使用该按钮,我试图重现此处描述的内容:RichEditBox (UWP) 在设置 Rtf 文本时忽略字体和前景但是在我的情况下,RichEditBox 仅显示安装在 Windows\Fonts 目录中的字体。如何克服这个问题并使用我的程序编译的本地字体的链接或让安装程序将字体安装到 Windows\Fonts 目录?我如何使用我的自定义字体(将它们链接到文档)而不安装它们,或者我需要做什么我的 UWP 程序在安装自身时将我的自定义字体安装到用户的设备上?

这是我用来显示文本的按钮的代码:

private void Page_Click(object sender, RoutedEventArgs e)
{
    string myRtfString = @"{\rtf1\fbidis\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil MyFont;}{\f1\fnil MyFont1;}{\f2\fnil MyFont2;}} {\colortbl ;\red0\green0\blue0;\red255\green255\blue255;\red255\green100\blue0;} {\*\generator Riched20 10.0.18362}\viewkind4\uc1 \pard\sl480\slmult1\qj\cf1\highlight2\f0\fs36 tt\highlight3\f1 g\f0 acgt\f2 c\highlight2\f0 tt\highlight0\par}";
    editor.Document.SetText(TextSetOptions.FormatRtf, myRtfString);
}

这是 RichEditBox 的 XAML:

<RichEditBox 
    x:Name="editor"
    Height="200"
    FontFamily="Assets/Fonts/MyFont.ttf#MyFont"
    FontSize="24" RelativePanel.Below="openFileButton"
    RelativePanel.AlignLeftWithPanel="True"
    RelativePanel.AlignRightWithPanel="True" />



4

1 回答 1

1

Gosha,通过这种方式,您可以将至少一种字体应用到该 .rtf 文件 - 见下文。对于其他人,我认为,您需要使用该 .rtf 中的地图信息或制作您自己的附加地图。那将是一些“trabajo”,但你能做什么?

         private void applyMyFonts()
    {
            string TextOut;
            MyRichEditBox.Document.GetText(TextGetOptions.None, out TextOut);
            MyRichEditBox.Document.Selection.SetRange(0, TextOut.Length);
    MyRichEditBox.Document.Selection.CharacterFormat.Name = "Assets/Fonts/MyFont.ttf#MyFont";   
    }

private async void OpenButton_Click(object sender, RoutedEventArgs e)
    {
        Windows.Storage.Pickers.FileOpenPicker open =
           new Windows.Storage.Pickers.FileOpenPicker();
        open.SuggestedStartLocation =
            Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
        open.FileTypeFilter.Add(".rtf");

        Windows.Storage.StorageFile file = await open.PickSingleFileAsync();

        if (file != null)
        {
            try
            {
                Windows.Storage.Streams.IRandomAccessStream randAccStream =
            await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

                // Load the file into the Document property of the RichEditBox.
                MyRichEditBox.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, randAccStream);
            }
            catch (Exception)
            {
                ContentDialog errorDialog = new ContentDialog()
                {
                    Title = "File open error",
                    Content = "Sorry, I couldn't open the file.",
                    PrimaryButtonText = "Ok"
                };

                await errorDialog.ShowAsync();
            }
        }

        applyMyfonts();
}
于 2019-07-03T17:10:00.150 回答