【问题标题】:Format words in RichTextBox在 RichTextBox 中格式化单词
【发布时间】:2013-08-15 03:09:41
【问题描述】:

我正在使用以下代码查找以“@”开头的每一行并将其设置为粗体:

foreach (var line in tweetText.Document.Blocks)
        {
            var text = new TextRange(line.ContentStart,
                           line.ContentEnd).Text;
            line.FontWeight = text.StartsWith("@") ?
                           FontWeights.Bold : FontWeights.Normal;
        }

但是,我想使用代码来查找每个单词而不是以“@”开头的行,这样我就可以格式化如下段落:

废话废话 @username废话废话 @anotherusername

【问题讨论】:

    标签: c# wpf formatting richtextbox


    【解决方案1】:

    这可能需要一些优化,因为我做得很快,但这应该可以帮助您开始

    private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {    
         tweetText.TextChanged -= RichTextBox_TextChanged;
         int pos = tweetText.CaretPosition.GetOffsetToPosition(tweetText.Document.ContentEnd);
    
         foreach (Paragraph line in tweetText.Document.Blocks.ToList())
         {
            string text = new TextRange(line.ContentStart,line.ContentEnd).Text;
    
            line.Inlines.Clear();
    
            string[] wordSplit = text.Split(new char[] { ' ' });
            int count = 1;
    
            foreach (string word in wordSplit)
            {
                if (word.StartsWith("@"))
                {
                    Run run = new Run(word);
                    run.FontWeight = FontWeights.Bold;
                    line.Inlines.Add(run);
                }
                else
                {
                    line.Inlines.Add(word);
                }
    
                if (count++ != wordSplit.Length)
                {
                     line.Inlines.Add(" ");
                }
            }
         }
    
         tweetText.CaretPosition = tweetText.Document.ContentEnd.GetPositionAtOffset(-pos);
         tweetText.TextChanged += RichTextBox_TextChanged;
    }
    

    【讨论】:

    • 这导致 TextRange 部分出现未处理的异常。请注意,这是在 richtextbox_textchanged 事件中使用的,并且是用 WPF 编写的
    • RichTextBox 不支持“.Document.Blocks.ToList()”
    • It has to... BlockCollection 继承自 TextElementCollection,后者继承了 IEnumerable。你确定你在 WPF 中并且使用 System.Windows.Controls.RichTextBox 而不是 System.Windows.Forms.RichTextBox
    • ToList 是 LINQ 的扩展方法。确保您拥有using System.Linq;
    • 谢谢安倍。但是,当您运行代码时,文本会混杂在一起(忽略空格键),并且文本光标始终位于文本的开头。有什么想法吗?
    【解决方案2】:

    我不知道您的确切要求,但我建议您不要将 RichtextBox 用于语法高亮目的。 有一个名为 AvalonEdit 的优秀组件可以轻松用于此目的。你可以在这篇文章中阅读更多关于 AvalonEdit 的信息:http://www.codeproject.com/Articles/42490/Using-AvalonEdit-WPF-Text-Editor

    满足您要求的语法定义:

    <SyntaxDefinition name="customSyntax"
            xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
        <Color name="User" foreground="Blue" fontWeight="bold" />
    
        <RuleSet>
            <Span color="User" begin="@" end =" "/>
        </RuleSet>
    </SyntaxDefinition>
    

    完整的演示项目可以在这里下载:http://oberaffig.ch/stackoverflow/avalonEdit.zip

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 2011-05-14
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      相关资源
      最近更新 更多