键入时拼写检查器出现问题。如何仅更改部分文本的字体?
Issue with Spell Checker as you type. How to Change font for only part of text?
我尝试实施 Hunspell 拼写检查程序库来检查我使用 C# 在我的记事本应用程序中键入时的拼写。它似乎工作正常,但是当出现拼写错误的单词时,整个 RichTextBox 都会带有下划线。
public void spellchecker()
{
Invoke(new MethodInvoker(delegate ()
{
using (Hunspell hunspell = new Hunspell("en_us.aff", "en_US.dic"))
{
String sentence = GetRichTextBox().Text;
foreach (string item in sentence.Split(' '))
{
bool correct = hunspell.Spell(item);
if (correct == false)
{
GetRichTextBox().Font = new Font(GetRichTextBox().Font, FontStyle.Underline);
}
else {
GetRichTextBox().Font = new Font(GetRichTextBox().Font, FontStyle.Regular);
}
}
}
}));
}
错误似乎在行中:
GetRichTextBox().Font = new Font(GetRichTextBox().Font, FontStyle.Underline);
所以当我将它替换为:
item.Font = new Font(item.Font, FontStyle.Underline);
..出现错误 "String Does not contain definition for font"。我无法让拼错的单词单独加下划线。
首先,不要用 ' '
拆分字符串,因为这会将 "Hello;world" 视为一个单词。您应该使用正则表达式在字符串中查找单词。使用此模式 \w+
.
二、如图this answer to the linked question, you can use the SelectionColor
and SelectionFont
属性更改文字样式选中目标文字后.
这应该有效:
Font fnt = richTextBox1.Font;
Color color;
foreach (Match match in Regex.Matches(richTextBox1.Text, @"\w+"))
{
string word = match.Value;
if (!hunspell.Spell(word))
{
fnt = new Font(fnt.FontFamily, fnt.Size, FontStyle.Underline);
color = Color.Red;
}
else
{
fnt = new Font(fnt.FontFamily, fnt.Size, FontStyle.Regular);
color = Color.Black;
}
richTextBox1.Select(match.Index, match.Length); // Selecting the matching word.
richTextBox1.SelectionFont = fnt; // Changing its font and color
richTextBox1.SelectionColor = color;
richTextBox1.SelectionStart = richTextBox1.TextLength; // Resetting the selection.
richTextBox1.SelectionLength = 0;
}
结果:
注意:我使用if (word.length < 5)
进行测试,您可以按照上面的代码所示应用您自己的条件。
希望对您有所帮助。
我尝试实施 Hunspell 拼写检查程序库来检查我使用 C# 在我的记事本应用程序中键入时的拼写。它似乎工作正常,但是当出现拼写错误的单词时,整个 RichTextBox 都会带有下划线。
public void spellchecker()
{
Invoke(new MethodInvoker(delegate ()
{
using (Hunspell hunspell = new Hunspell("en_us.aff", "en_US.dic"))
{
String sentence = GetRichTextBox().Text;
foreach (string item in sentence.Split(' '))
{
bool correct = hunspell.Spell(item);
if (correct == false)
{
GetRichTextBox().Font = new Font(GetRichTextBox().Font, FontStyle.Underline);
}
else {
GetRichTextBox().Font = new Font(GetRichTextBox().Font, FontStyle.Regular);
}
}
}
}));
}
错误似乎在行中:
GetRichTextBox().Font = new Font(GetRichTextBox().Font, FontStyle.Underline);
所以当我将它替换为:
item.Font = new Font(item.Font, FontStyle.Underline);
..出现错误 "String Does not contain definition for font"。我无法让拼错的单词单独加下划线。
首先,不要用 ' '
拆分字符串,因为这会将 "Hello;world" 视为一个单词。您应该使用正则表达式在字符串中查找单词。使用此模式 \w+
.
二、如图this answer to the linked question, you can use the SelectionColor
and SelectionFont
属性更改文字样式选中目标文字后.
这应该有效:
Font fnt = richTextBox1.Font;
Color color;
foreach (Match match in Regex.Matches(richTextBox1.Text, @"\w+"))
{
string word = match.Value;
if (!hunspell.Spell(word))
{
fnt = new Font(fnt.FontFamily, fnt.Size, FontStyle.Underline);
color = Color.Red;
}
else
{
fnt = new Font(fnt.FontFamily, fnt.Size, FontStyle.Regular);
color = Color.Black;
}
richTextBox1.Select(match.Index, match.Length); // Selecting the matching word.
richTextBox1.SelectionFont = fnt; // Changing its font and color
richTextBox1.SelectionColor = color;
richTextBox1.SelectionStart = richTextBox1.TextLength; // Resetting the selection.
richTextBox1.SelectionLength = 0;
}
结果:
注意:我使用if (word.length < 5)
进行测试,您可以按照上面的代码所示应用您自己的条件。
希望对您有所帮助。