This is a migrated thread and some comments may be shown as answers.

RichTextBox CodeBlock CodeFormatter Styles

4 Answers 94 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Max
Top achievements
Rank 1
Max asked on 10 Jul 2020, 02:38 PM

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?

4 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 14 Jul 2020, 12:07 PM

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

0
Max
Top achievements
Rank 1
answered on 14 Jul 2020, 07:14 PM

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. :-)

0
Dimitar
Telerik team
answered on 15 Jul 2020, 06:54 AM

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

0
Max
Top achievements
Rank 1
answered on 15 Jul 2020, 09:00 AM

Thank you Dimitar,

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

Max

Tags
RichTextBox
Asked by
Max
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Max
Top achievements
Rank 1
Share this question
or