New to Telerik UI for WinForms? Download free 30-day trial

How to Achieve Underline Text in RadSyntaxEditor

Environment

Product Version Product Author
2020.3.1020 RadSyntaxEditor for WinForms Desislava Yordanova

Description

RadSyntaxEditor works with the underlying document with the help of taggers. Taggers are used to identify spans of text and assign them a specific tag if they match a specific condition. Thus, you can color different words in the document.

This article demonstrates a sample approach how to underline some text along with applying a specific fore color to it.

underline-text-in-syntax-editor 001

Solution

It can be achieved by using a custom tagger to classify the desired words (e.g. "Telerik") and mark these words as specific. Since RadSyntaxEditor renders its elements on different layers based on the types of tags that are used, we will need a custom layer for the underline style.


public Form1()
{
    InitializeComponent();

    this.radSyntaxEditor1.TaggersRegistry.RegisterTagger(new TelerikTagger(this.radSyntaxEditor1.SyntaxEditorElement));
    this.radSyntaxEditor1.SyntaxEditorElement.UILayersBuilder = new CustomUILayersBuilder();
    this.radSyntaxEditor1.TextFormatDefinitions.AddLast(TelerikTagger.TelerikClassificationType, 
        new TextFormatDefinition(new SolidBrush(System.Drawing.Color.Red), null,
                                  new UnderlineInfo(new SolidBrush(System.Drawing.Color.Green), UnderlineDecorations.Line),
                                  new Telerik.WinForms.Controls.SyntaxEditor.UI.Pen(new SolidBrush(System.Drawing.Color.Green), 2))); 

}
public class TelerikTagger : WordTaggerBase
{
    public static readonly ClassificationType TelerikClassificationType = new ClassificationType("Telerik");
    public static readonly Dictionary<string, ClassificationType> WordsToClassificationType = new Dictionary<string, ClassificationType>();

    public TelerikTagger(RadSyntaxEditorElement editor) : base(editor)
    {
        WordsToClassificationType.Add("Telerik", TelerikClassificationType);
    }

    protected override Dictionary<string, ClassificationType> GetWordsToClassificationTypes()
    {
        return TelerikTagger.WordsToClassificationType;
    }
}

public class CustomUILayersBuilder : Telerik.WinForms.Controls.SyntaxEditor.UI.Layers.UILayersBuilder
{
    public override void BuildUILayers(UILayerStack uiLayers)
    {
        base.BuildUILayers(uiLayers);
        uiLayers.AddLast(new MyTextUnderlineUILayer());
    }
}
public class MyTextUnderlineUILayer : LineBasedUILayer<ClassificationTag>
{
    public override string Name => "MyTextUnderlineUILayer";

    protected override FrameworkElement GetLinePartUIElement(ClassificationTag tag,
                                                             Telerik.WinForms.SyntaxEditor.Core.Text.Span span,
                                                             UIUpdateContext updateContext)
    {
        if (tag.ClassificationType != TelerikTagger.TelerikClassificationType)
        {
            return null;
        }

        TextFormatDefinition textFormatting = updateContext.Editor.TextFormatDefinitions.
            GetTextFormatDefinition(TelerikTagger.TelerikClassificationType);
        Telerik.WinControls.SyntaxEditor.UI.Rect rect = updateContext.Editor.GetLinePartBoundingRectangle(span, true);

        Rect underlineRect = new Rect
        {
            Y = rect.Height,
            Width = rect.Width,
            Height = updateContext.Editor.EditorFontSize / 16
        };

        FrameworkElement underline = null;
        if (textFormatting.Underline != null)
        {
            underline = textFormatting.Underline.CreateUnderline(underlineRect);
        }

        return underline;
    }

    protected override void ResetPooledElementProperties(object element)
    {
    }
}