Search code examples
wpfavalonedit

How to center text in AvalonEdit?


I'm using ICSharpCode's AvalonEdit text editor, and I have a custom DocumentColorizingTransformer.

I would like to center certain lines of text inside of ColorizeLine. Is this possible?

I've been able to figure out how to change the line's text in every other way I want (color, italics, underline, etc), but I can't figure out how to change the line's text alignment...


Solution

  • I ended up having to dig into the editor's source for this:

    First, in VisualLineTextParagraphProperties.cs, I had to expose the TextAlignment via a new internal property (defaulted to LeftAligned), so I could set it (and, of course, return it from the getter of the public property).

    Then, in TextView.cs, in the BuildVisualLine method, I had to test the text of documentLine for the conditions under which I wanted the line centered, then change the paragraphProperties object accordingly.

    Do note that I had to make a copy of paragraphProperties - otherwise the centered alignment would bleed over into the next line (because the given paragraphProperties object seems to be reused).

    Also note that I could not find a way to use a VisualLineElementGenerator to do this - even though it still looks to me like that would be the place to change something like TextAlignment...