RichTextBox CodeBlock CodeFormatter Styles

5 posts, 0 answers
  1. Max
    Max avatar
    6 posts
    Member since:
    May 2020

    Posted 10 Jul Link to this post

    Hello,

    I am trying to give my CodeBlocks inside a RichTextBox.Document a certain style.

    Every character inside the CodeBlock should have the same FontFamily and FontSize.

    To do this I am going the following way:

    1.) Clear all existing styles inside the RichTextBox.CodeFormatter.

    2.) Add my own styles.

    The code looks like this:

                StyleDefinition styleWhite = new StyleDefinition(cl.GetCodeLanguage.Name + "StyleWhite", StyleType.Character);
                styleWhite.SpanProperties.ForeColor = Colors.White;
                styleWhite.SpanProperties.FontFamily = Wpf.Fonts.Fonts.GetFont(eFont.Consolas);
                styleWhite.SpanProperties.FontSize = Unit.PointToDip(fontSize);


                StyleDefinition styleKeyWord = new StyleDefinition(cl.GetCodeLanguage.Name + "StyleKeyword", StyleType.Character);
                styleKeyWord.SpanProperties.ForeColor  = colorKeyWord;
                styleKeyWord.SpanProperties.FontFamily = Wpf.Fonts.Fonts.GetFont(eFont.Consolas);
                styleKeyWord.SpanProperties.FontSize   = Unit.PointToDip(fontSize);

                StyleDefinition styleString = new StyleDefinition(cl.GetCodeLanguage.Name + "StyleString", StyleType.Character);
                styleString.SpanProperties.ForeColor = colorKeyWord;
                styleString.SpanProperties.FontFamily = Wpf.Fonts.Fonts.GetFont(eFont.Consolas);
                styleString.SpanProperties.FontSize = Unit.PointToDip(fontSize);

                StyleDefinition styleMethod = new StyleDefinition(cl.GetCodeLanguage.Name + "styleMethod", StyleType.Character);
                styleMethod.SpanProperties.ForeColor = colorKeyWord;
                styleMethod.SpanProperties.FontFamily = Wpf.Fonts.Fonts.GetFont(eFont.Consolas);
                styleMethod.SpanProperties.FontSize = Unit.PointToDip(fontSize);

                StyleDefinition styleComment = new StyleDefinition(cl.GetCodeLanguage.Name + "StyleComment", StyleType.Character);
                styleComment.SpanProperties.ForeColor = colorKeyWord;
                styleComment.SpanProperties.FontFamily = Wpf.Fonts.Fonts.GetFont(eFont.Consolas);
                styleComment.SpanProperties.FontSize = Unit.PointToDip(fontSize);



                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Comment);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Keyword);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Method);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.StringLiteral);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Attributes);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.CharacterLiteral);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Constants);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Data);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.ExcludedCode);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Identifier);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.NumberLiteral);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Operator);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.PreprocessorKeyword);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Variable);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.WhiteSpace);
                doc.CodeFormatter.UnregisterClassificationType(ClassificationTypes.Literal);

     

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

     

    The result of this code is, that I get different FontSizes inside the CodeBlock. See attached *.gif.

    So my question is: Which styles do I need to change or add, to give CodeBlocks a consitent look?

  2. Dimitar
    Admin
    Dimitar avatar
    2908 posts

    Posted 14 Jul Link to this post

    Hello Max,

    You need to set the font size of the following built-in styles as well:

    var style = this.radRichTextBox.Document.StyleRepository["CodeBlock"];
    style.SpanProperties.FontSize = Unit.PointToDip(fontSize);
    
    style = this.radRichTextBox.Document.StyleRepository["CodeBlockLine"];
    style.SpanProperties.FontSize = Unit.PointToDip(fontSize);
    
    style = this.radRichTextBox.Document.StyleRepository["CodeBlockLineAlt"];
    style.SpanProperties.FontSize = Unit.PointToDip(fontSize);
    

    I hope this helps. Should you have any other questions, do not hesitate to ask.

    Regards,
    Dimitar
    Progress Telerik

  3. Max
    Max avatar
    6 posts
    Member since:
    May 2020

    Posted 14 Jul Link to this post

    Thank you Dimitar,

    this is the perfect answer to my problem. Works like a charm!

    I have to say, that the Document.StyleRepository was already on my radar, before I started this post.

    But even if you stop the application while debugging, you can't see these styles by their name. They are somehow hidden in some sub-sub-styling.

    Anyway - you made my day. :-)

  4. Dimitar
    Admin
    Dimitar avatar
    2908 posts

    Posted 15 Jul Link to this post

    Hello Max,

    I am glad that this works for you. In this case, a better way to determine the used styles is to export the document using the docx or xaml formats. This way you can see which styles are applied. 

    Do not hesitate to contact us if you have other questions.

    Regards,
    Dimitar
    Progress Telerik

  5. Max
    Max avatar
    6 posts
    Member since:
    May 2020

    Posted 15 Jul Link to this post

    Thank you Dimitar,

    to export and look into the results style is a really good hint for the future.

    Max

Back to Top