简体   繁体   中英

Telerik RadRichTextBox: How can I change the font size in a code block?

I am evaluating the RadRichTextBox control from Telerik. My main goal is to add custom formatted code blocks.

Therefore I have created my own styles and registered them. (see code below)

The problem: While I can set the style for keywords and comments, I don't know how to style the font for "normal code".

As you can see below, I have set a style for each possible "ClassificationType", but "normal code" keeps unstyled.

The result is, that keywords and comments have the desired font-size and color, but "normal code" has no style.

My question: How do I set the font-style in a code-block for "normal-code"?

        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);

在此处输入图像描述

To speed things up, I answer my own question.

After (,) you have added any code block to a RadRichTextBox document. there will be 2 new styles inside the StyleRepository of the document. These styles are added by the RadRichTextBox.

They are called "CodeBlock" and "CodeBlockLine".

You can find and change them like this:

    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 may update the whole document after the first time, to see the changes:

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

I could not find this anywhere and only found it by debugging the document properties at runtime.

I hope, it helps someone somewhen in the future. Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM