簡體   English   中英

無法使用自定義字體打開RTF文件

[英]cannot open my RTF file with my custom fonts

我為UWP程序制作了自己的自定義.ttf字體,並將它們放在Assets文件夾中(相對於2017/2019)。 在RichEditBox中處理文檔時,它們運行良好。 但是,當我保存RTF文件然后打開它時,我的自定義字體將被忽略。 如果我預先將自定義字體安裝到Windows \\ Fonts文件夾中,則“打開文件”將使用我的自定義字體加載文檔。 看起來好像沒有安裝我的自定義字體,程序不會將它們鏈接到文檔。

再次-我用RichEditBox和我的自定義字體編寫了一個程序。 處理后-更改字體,樣式等,一切都按設計進行。 當我使用該程序保存RTF文件,並使用該程序(相同)打開該RTF時-顏色表還可以,盡管使用該程序編譯了字體,但我的字體沒有顯示(BuildAction-Content; CopyToOutputDirectory-Always Copy)。 為了簡化-我用文件包含的信息做了按鈕。 盡管已編譯字體(位於Assets文件夾中),但程序不會將其鏈接到文檔。

實際上,使用該按鈕時,我嘗試重現此處描述的內容: 設置Rtf文本時,RichEditBox(UWP)忽略字體和前景。但是在我的情況下,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" />



天哪,這樣您就可以將至少一種字體應用於該.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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM