AvalonEdit WPF TextEditor (SharpDevelop): How to highlight a specific range of text?

后端 未结 4 936
温柔的废话
温柔的废话 2020-12-30 14:24

The incredibly awesome AvalonEdit WPF TextEditor control seems to lack an important feature, or at least i can\'t figure it out. Given an offset and a length, highli

4条回答
  •  灰色年华
    2020-12-30 15:13

    Did you see this in this article - it seems to be exactly what are you asking for:

    public class ColorizeAvalonEdit : DocumentColorizingTransformer
    {
    protected override void ColorizeLine(DocumentLine line)
    {
        int lineStartOffset = line.Offset;
        string text = CurrentContext.Document.GetText(line);
        int start = 0;
        int index;
        while ((index = text.IndexOf("AvalonEdit", start)) >= 0) {
            base.ChangeLinePart(
                lineStartOffset + index, // startOffset
                lineStartOffset + index + 10, // endOffset
                (VisualLineElement element) => {
                    // This lambda gets called once for every VisualLineElement
                    // between the specified offsets.
                    Typeface tf = element.TextRunProperties.Typeface;
                    // Replace the typeface with a modified version of
                    // the same typeface
                    element.TextRunProperties.SetTypeface(new Typeface(
                        tf.FontFamily,
                        FontStyles.Italic,
                        FontWeights.Bold,
                        tf.Stretch
                    ));
                });
            start = index + 1; // search for next occurrence
    }   }   }
    

    It highlights word AvalonEdit with bold.

提交回复
热议问题