Telerik RadRichTextBox。我如何在代码块中改变字体大小?

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

我正在评估Telerik的RadRichTextBox控件。我的主要目标是添加自定义格式的代码块。

因此,我已经创建了自己的样式并注册了它们。(见下面的代码)

问题是 我可以为关键字和注释设置样式 但我不知道如何为 "普通代码 "设置字体样式。

正如你所看到的,我为每个可能的 "分类类型 "设置了一个样式,但是 "普通代码 "一直没有设置样式。

结果是,关键字和注释有所需的字体大小和颜色,但 "普通代码 "没有样式。

我的问题是 我如何在代码块中为 "正常代码 "设置字体样式?

        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

c# telerik styles
1个回答
0
投票

为了加快进度,我回答我自己的问题。

(!)你已经添加了任何代码块到一个RadRichTextBox文档中,在文档的StyleRepository中会有2个新的样式。这些样式是由RadRichTextBox添加的,它们被称为 "CodeBlock "和 "CodeBlockLine"。

它们被称为 "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);
            }
        }
    }

你可以在第一次更新后更新整个文档以查看变化。

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

我在任何地方都找不到这一点,只有在运行时调试文档属性时才发现。

我希望,它能帮助到未来的某个人。干杯!

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