突出显示AvalonEdit所选单词的所有实例 [英] Highlight all occurrences of selected word in AvalonEdit
本文介绍了突出显示AvalonEdit所选单词的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要突出AvalonEdit所选单词的所有实例。我创建HihglinghtingRule类的一个实例:
I need to highlight all occurrences of selected word in AvalonEdit. I created an instance of HihglinghtingRule class:
var rule = new HighlightingRule()
{
Regex = regex, //some regex for finding occurences
Color = new HighlightingColor {Background = new SimpleHighlightingBrush(Colors.Red)}
};
我应该后做什么?
感谢。
What should I do after it ? Thanks.
推荐答案
要使用 HighlightingRule
,你必须创建高亮引擎的另一个实例( HighlightingColorizer
等)
To use that HighlightingRule
, you would have to create another instance of the highlighting engine (HighlightingColorizer
etc.)
它更容易和更高效的编写一个 DocumentColorizingTransformer
,突出你的话:
It's easier and more efficient to write a DocumentColorizingTransformer
that highlights your word:
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
}
}
}
这篇关于突出显示AvalonEdit所选单词的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文