在RichTextBox的文字格式 [英] Format words in RichTextBox

查看:144
本文介绍了在RichTextBox的文字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来找到每个以@开头的行,并通过使其大胆格式化:

  
{
变种文字=新的TextRange的foreach(在tweetText.Document.Blocks VAR线)(line.ContentStart,
line.ContentEnd)。文本;
line.FontWeight = text.StartsWith(@)?
FontWeights.Bold:FontWeights.Normal;
}



不过,我想用代码来找出每个字代替线开头的@这样我就可以像格式化一个段落:




等等等等等等的 @username 等等等等等等等等 @anotherusername



解决方案

这很可能使用一些优化作为我做到了快,但是这应该让你开始

 私人无效RichTextBox_TextChanged(对象发件人,TextChangedEventArgs E)
{
tweetText.TextChanged - = RichTextBox_TextChanged;
INT POS = tweetText.CaretPosition.GetOffsetToPosition(tweetText.Document.ContentEnd);

的foreach(在tweetText.Document.Blocks.ToList段线())
{
字符串文本=新的TextRange(line.ContentStart,line.ContentEnd)。文本;

line.Inlines.Clear();

的String [] = wordSplit text.Split(新的char [] {''});
诠释计数= 1;

的foreach(在wordSplit串字)
{
如果(word.StartsWith(@))
{
RUN RUN =新的运行(字);
run.FontWeight = FontWeights.Bold;
line.Inlines.Add(运行);
}
,否则
{
line.Inlines.Add(字);
}

如果(计数++ = wordSplit.Length!)
{
line.Inlines.Add();
}
}
}

tweetText.CaretPosition = tweetText.Document.ContentEnd.GetPositionAtOffset(-pos);
tweetText.TextChanged + = RichTextBox_TextChanged;
}


I am using the following code to find each line that starts with "@" and format it by making it bold:

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

However, I would like to use the code to find each word instead of line beginning with "@" so I could format a paragraph like:

Blah blah blah @username blah blah blah blah @anotherusername

解决方案

This could probably use some optimization as I did it quick, but this should get you started

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;
}

这篇关于在RichTextBox的文字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆