繁体   English   中英

Telerik RadRichTextBox:如何更改代码块中的字体大小?

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

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

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

问题:虽然我可以设置关键字和注释的样式,但我不知道如何为“普通代码”设置字体样式。

正如您在下面看到的,我为每个可能的“分类类型”设置了一个样式,但“普通代码”保持无样式。

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

我的问题:如何在“普通代码”的代码块中设置字体样式?

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

在此处输入图像描述

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

(,) 之后,您已将任何代码块添加到 RadRichTextBox 文档。 文档的 StyleRepository 中将有 2 个新的 styles。 这些 styles 是由 RadRichTextBox 添加的。

它们被称为“代码块”和“代码块线”。

您可以像这样查找和更改它们:

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

我无法在任何地方找到它,只能通过在运行时调试文档属性找到它。

我希望,它在将来的某个时候对某人有所帮助。 干杯!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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