【问题标题】:Telerik RadRichTextBox: How can I change the font size in a code block?Telerik RadRichTextBox:如何更改代码块中的字体大小?
【发布时间】:2020-05-05 21:04:16
【问题描述】:

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

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

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

如下所示,我为每个可能的“分类类型”设置了一个样式,但“普通代码”没有设置样式。

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

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

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

【问题讨论】:

    标签: c# telerik styles


    【解决方案1】:

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

    (!) 您将任何代码块添加到 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);
                }
            }
        }
    

    您可以在第一次之后更新整个文档,以查看更改:

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-05-20
      • 2016-10-18
      • 2015-06-16
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多