我正在尝试基于模式创建一个彩色RichTextBox。
正文是:
Hey, This{Red} IS A {Cyan}sample. The {Green}color is green color
每个{ }
包含一种颜色,它是下一个单词的样式:
嘿,这是一个颜色是绿色的颜色Hey, This
默认颜色。IS A
应为红色。sample. The
应为青色。color is green color
应为绿色。
下面是我的代码:
// Hey, This{Red} IS A {Cyan}sample. The {Green}color is green color
// shown text should be:
// Hey, This IS A sample. The color is green
const string OriginalText = "Hey, This{Red} IS A {Cyan}sample. The {Green}color is green color";
const string ShownText = "Hey, This IS A sample. The color is green color";
const string Pattern = "(?<=\\{)(.*?)(?=\\})";
rtbMain.Text = ShownText;
rtbMain.SelectAll();
rtbMain.SelectionColor = Color.Black;
rtbMain.SelectionBackColor = Color.White;
Regex regex = new(Pattern, RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(OriginalText);
if (matches.Count > 0)
{
var rtbText = rtbMain.Text;
var length = ShownText.Length;
var allMatches = new List<Match>();
for (int i = 0; i < matches.Count; i++)
{
var m = matches[i];
allMatches.Add(m);
Match nextMatch = null;
if (matches.Count > i + 1)
{
nextMatch = matches[i + 1];
}
var sum = GetSum();
var start = m.Index;
var currentLength = m.Length;
if (nextMatch != null)
{
var end = nextMatch.Index - start- sum;
rtbMain.Select(start- 1, end);
}
else
{
var currentIndex = OriginalText.IndexOf(m.Value);
rtbMain.Select(length - currentIndex, (length - currentIndex) - sum);
}
rtbMain.SelectionColor = GetColor(m.Value);
}
int GetSum()
{
return allMatches!.Select(m => m.Value.Length - 1).Sum();
}
Color GetColor(string color)
{
return Color.FromName(color);
}
}
else
{
Debug.WriteLine("No matches found");
}
由于RichTextBox没有颜色标记,我不知道如何计算索引/长度的正确位置。
屏幕截图:
1条答案
按热度按时间gg58donl1#
您也可以比对要剖析之字串的结束位置,然后在循环Matches集合时,您只需要计算字串内的目前位置,并考虑每个相符项目的长度。
使用稍微修改的正则表达式,每个Match的
Index
和Length
引用匹配的标签(例如{green}
),并且Group 1
中的每个值是Color的名称。大概是这样的:
(note这里只使用了
SelectionColor
,因为我在每次迭代时都会向Control追加一个新字符串。添加的新字符串实际上已经是一个Selection,所以不需要显式设置选择的长度)导致: