突出显示文本的RichTextBox [英] Highlight text in RichTextBox
问题描述
我试图用一个RichTextBox和我的第一感觉:什么是它的复杂使用...
最好的...
I'm trying to use a RichTextBox and my first feeling : "What's it's complicated to use !"... Amazing ...
所以我想强调包含在我的RichTextBox的文本
So I'm trying to highlight a text contained in my RichTextBox.
目前,我有以下代码:
TextRange range = new TextRange(MyTextInput.Document.ContentStart, MyTextInput.Document.ContentEnd);
range.Text = @"TOP a multiline text or file END";
Regex reg = new Regex("(top|file|end)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (Match match in reg.Matches(range.Text))
{
TextPointer start = range.Start.GetPositionAtOffset(match.Index, LogicalDirection.Forward);
TextPointer end = range.Start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward);
// text contains the exact match I want
string text = range.Text.Substring(match.Index, match.Length);
// here the highlighted text isn't the text I searched...
TextRange textrange = new TextRange(start, end);
textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
textrange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
}
TOP
是正确的突出但不文件
或结束
,但突出了我或
。
TOP
is correctly highlighted but not file
or end
but highlight me or
.
有什么建议?
推荐答案
你必须想象在RichTextBox引擎盖下不理解的行为。我并不确切地知道,但我想象中的以下内容:第1-2行集与的RichTextBox
A 段落
内容一个运行
。
you have to imagine what the RichTextBox does under the hood to understand the behavior. I don't know exactly but I imagine the following: Line 1-2 set as Content of RichTextBox
a Paragraph
with a Run
.
然后用 ApplyPropertyValue
在RichTextBox的内容得到改变!现在,它包含一个段落
与跨度
(用运行
中)和运行。
Then with the first iteration with ApplyPropertyValue
the Content of the RichTextBox gets changed! It now contains a Paragraph
with a Span
(with a Run
inside) and a Run.
然后你必须要考虑的正则表达式匹配之间的差异 GetPositionAtOffset
。正则表达式匹配返回字符串中的一个字符位置的索引。
And then you have to consider the discrepancy between the Regex match and GetPositionAtOffset
. The Regex match returns an index for a char position in a string.
GetPositionAtOffset 用途的 一个偏移量的符号,要计算并返回的位置的,其中一个标志是:
GetPositionAtOffset uses "An offset, in symbols, for which to calculate and return the position" where a symbol is:
- 为元素的TextElement打开或关闭标签。
- 包含在InlineUIContainer或BlockUIContainer一个的UIElement元素。需要注意的是这样的的UIElement总是算作正好一个符号;通过的UIElement包含的任何额外的内容或元素不作为符号。
- 一个16位的Unicode字符的文本运行元素中。
所以,你可能想要做的是这样的:
So what you might want to do is something like this:
TextRange range = new TextRange(MyTextInput.Document.ContentStart, MyTextInput.Document.ContentEnd);
range.Text = @"TOP a multiline text or file END";
Regex reg = new Regex("(top|file|end)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
var start = MyTextInput.Document.ContentStart;
while (start != null && start.CompareTo(MyTextInput.Document.ContentEnd) < 0)
{
if (start.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
var match=reg.Match(start.GetTextInRun(LogicalDirection.Forward));
var textrange = new TextRange(start.GetPositionAtOffset(match.Index, LogicalDirection.Forward), start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward));
textrange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
textrange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
start= textrange.End; // I'm not sure if this is correct or skips ahead too far, try it out!!!
}
start = start.GetNextContextPosition(LogicalDirection.Forward);
}
*免责声明:我没有尝试过这是现在我隔靴搔痒一个开发环境。我甚至不知道这是否编译,但我希望如此。
*Disclaimer: I have not tried this as right now I'm nowhere near a development environment. I don't even know if this compiles, but I hope so.
这篇关于突出显示文本的RichTextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!