Telerik RadRichTextBox: Как изменить размер шрифта в блоке кода? - PullRequest
0 голосов
/ 06 мая 2020

Я оцениваю элемент управления RadRichTextBox от Telerik. Моя основная цель - добавить блоки кода в произвольном формате.

Поэтому я создал свои собственные стили и зарегистрировал их. (см. код ниже)

Проблема: Хотя я могу установить стиль для ключевых слов и комментариев, я не знаю, как стилизовать шрифт для «обычного кода».

Как вы можете видеть ниже, я установил стиль для каждого возможного "ClassificationType", но "нормальный код" остается без стиля.

В результате ключевые слова и комментарии имеют желаемый размер шрифта и цвет, но «нормальный код» не имеет стиля.

Мой вопрос: Как мне установить стиль шрифта в блоке кода для "нормального кода"?

        StyleDefinition styleKeyWord = new StyleDefinition(cl.GetCodeLanguage.Name + "StyleKeyword", StyleType.Character);
        styleKeyWord.SpanProperties.ForeColor  = DarkTheme.LanguageColorKeyword;
        styleKeyWord.SpanProperties.FontFamily = DarkTheme.LanguageFontFamily;
        styleKeyWord.SpanProperties.FontSize   = Unit.PointToDip(DarkTheme.LanguageFontSize);

        StyleDefinition styleString = new StyleDefinition(cl.GetCodeLanguage.Name + "StyleString", StyleType.Character);
        styleString.SpanProperties.ForeColor = DarkTheme.LanguageColorString;
        styleString.SpanProperties.FontFamily = DarkTheme.LanguageFontFamily;
        styleString.SpanProperties.FontSize = Unit.PointToDip(DarkTheme.LanguageFontSize);

        StyleDefinition styleComment = new StyleDefinition(cl.GetCodeLanguage.Name + "StyleComment", StyleType.Character);
        styleComment.SpanProperties.ForeColor = DarkTheme.LanguageColorComment;
        styleComment.SpanProperties.FontFamily = DarkTheme.LanguageFontFamily;
        styleComment.SpanProperties.FontSize = Unit.PointToDip(DarkTheme.LanguageFontSize);

        StyleDefinition styleMethod = new StyleDefinition(cl.GetCodeLanguage.Name + "styleMethod", StyleType.Character);
        styleMethod.SpanProperties.ForeColor = DarkTheme.LanguageColorString;
        styleMethod.SpanProperties.FontFamily = DarkTheme.LanguageFontFamily;
        styleMethod.SpanProperties.FontSize = Unit.PointToDip(DarkTheme.LanguageFontSize);


        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.Attributes,           codeLanguage, styleComment);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.CharacterLiteral,     codeLanguage, styleComment);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.Comment,              codeLanguage, styleComment); //Ok
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.Constants,            codeLanguage, styleComment);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.Data,                 codeLanguage, styleComment);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.ExcludedCode,         codeLanguage, styleComment);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.Identifier,           codeLanguage, styleComment);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.Keyword,              codeLanguage, styleKeyWord); //Ok
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.Literal,              codeLanguage, styleComment);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.Method,               codeLanguage, styleMethod);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.NumberLiteral,        codeLanguage, styleComment); 
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.Operator,             codeLanguage, styleComment);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.PreprocessorKeyword,  codeLanguage, styleComment);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.StringLiteral,        codeLanguage, styleString);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.Variable,             codeLanguage, styleComment);
        document.CodeFormatter.RegisterClassificationType(ClassificationTypes.WhiteSpace,           codeLanguage, styleComment);

enter image description here

1 Ответ

0 голосов
/ 06 мая 2020

Чтобы ускорить процесс, я отвечаю на свой вопрос.

После того, как (!) Вы добавили какой-либо блок кода в документ RadRichTextBox, внутри StyleRepository документа появятся 2 новых стиля. Эти стили добавляются RadRichTextBox.

Они называются «CodeBlock» и «CodeBlockLine».

Их можно найти и изменить следующим образом:

    public static void AfterAddingCodeBlock(this RadDocument doc)
    {
        if (doc != null && doc.StyleRepository != null)
        {
            StyleDefinition codeBlockStyle = doc.StyleRepository.GetValueOrNull("CodeBlock", false);
            if (codeBlockStyle != null)
            {
                codeBlockStyle.SpanProperties.ForeColor  = DarkTheme.CodeColorDefault;
                codeBlockStyle.SpanProperties.FontFamily = DarkTheme.CodeFontFamily;
                codeBlockStyle.SpanProperties.FontSize   = Unit.PointToDip(DarkTheme.CodeFontSize);
            }
            StyleDefinition codeBlockLineStyle = doc.StyleRepository.GetValueOrNull("CodeBlockLine", false);
            if (codeBlockLineStyle != null)
            {
                codeBlockLineStyle.SpanProperties.ForeColor  = DarkTheme.CodeColorDefault;
                codeBlockLineStyle.SpanProperties.FontFamily = DarkTheme.CodeFontFamily;
                codeBlockLineStyle.SpanProperties.FontSize   = Unit.PointToDip(DarkTheme.CodeFontSize);
            }
        }
    }

You может обновить весь документ после первого раза, чтобы увидеть изменения:

_RadRichTextBox.Document.AfterAddingCodeBlock();
_RadRichTextBox.UpdateEditorLayout();

Я не смог найти его нигде и нашел только путем отладки свойств документа во время выполнения.

Надеюсь , это поможет кому-то когда-нибудь в будущем. Ура!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...