簡體   English   中英

在richtextbox wpf中更改特定文本的顏色

[英]Change back color of specific text in richtextbox wpf

我正在研究函數,它將改變RichTextBox文本中標簽的顏色(等等等等)。 我嘗試編寫一些代碼,但僅第一個標記正確突出顯示。 第二個標簽將被高亮幾個字符。 拜托,我該怎么做呢?

實際狀態-函數僅第一個標簽正確着色,其他字符之前為彩色字符。

我想要的狀態-僅對標記着色,並且其內容在RichTextBox中。

問題部分:

        TextPointer text = txbPlainText.Document.ContentStart;
        while (text.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
        {
          text = text.GetNextContextPosition(LogicalDirection.Forward);
        }
        TextPointer start = text.GetPositionAtOffset(tagStartIndex);
        TextPointer end = text.GetPositionAtOffset(i + tagBuilder.Length);

        TextRange textRange = new TextRange(start, end);
        textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Color.FromRgb(220, 204, 163)));

外觀: 有問題的圖片

這是整個功能:

private void ColorizeTags()
{
  string tagString = string.Empty;
  int tagStartIndex = 0;
  char[] txbChars = GetTxbText().ToCharArray();

  for (int i = 0; i < txbChars.Count(); i++)
  {
    char actualChar = txbChars[i];

    if (actualChar == '<' && txbChars[i + 1] != '/')
    {
      StringBuilder tagBuilder = new StringBuilder();
      foreach (string tag in TagList)
      {
        for (int x = i; x < (i + tag.Length); x++)
        {
          if (x > txbChars.Count())
          {
            break;
          }

          tagBuilder.Append(txbChars[x]);
        }

        if (tagBuilder.ToString() == tag)
        {
          tagString = tagBuilder.ToString();
          tagStartIndex = i;
          break;
        }
      }
    }
    else if (actualChar == '<' && txbChars[i + 1] == '/')
    {
      if (string.IsNullOrWhiteSpace(tagString))
      {
        continue;
      }

      string endTag = tagString.Insert(tagString.IndexOf('<') + 1, "/");
      StringBuilder tagBuilder = new StringBuilder();

      for (int c = i; c < (i + endTag.Length); c++)
      {
        tagBuilder.Append(txbChars[c]);
      }

      if (tagBuilder.ToString() == endTag)
      {
        TextPointer text = txbPlainText.Document.ContentStart;
        while (text.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
        {
          text = text.GetNextContextPosition(LogicalDirection.Forward);
        }
        TextPointer start = text.GetPositionAtOffset(tagStartIndex);
        TextPointer end = text.GetPositionAtOffset(i + tagBuilder.Length);

        TextRange textRange = new TextRange(start, end);
        textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Color.FromRgb(220, 204, 163)));
        tagString = string.Empty;
        continue;
      }
    }
  }
}

private string GetTxbText()
{
  return new TextRange(txbPlainText.Document.ContentStart, txbPlainText.Document.ContentEnd).Text;
}

我搜索並發現了問題。 問題是當我獲得起點和終點的位置偏移時,當前文本指針的值已更改。

第二個問題是,我的TextPointer text忽略了空格,以下為解決方案。

解決方案:不要使位置直線偏移。

之前:

...
TextPointer text = txbPlainText.Document.ContentStart;
while (text.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
{
  text = text.GetNextContextPosition(LogicalDirection.Forward);
}
TextPointer start = text.GetPositionAtOffset(tagStartIndex);
TextPointer end = text.GetPositionAtOffset(i + tagBuilder.Length);
...

后:

...
TextPointer text = txbPlainText.Document.ContentStart;
TextPointer start = GetTextPointAt(text, tagStartIndex);
TextPointer end = GetTextPointAt(text, endIndex);
...

private static TextPointer GetTextPointAt(TextPointer from, int pos)
{
  TextPointer ret = from;
  int i = 0;

  while ((i < pos) && (ret != null))
  {
    if ((ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text) || (ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.None))
      i++;

    if (ret.GetPositionAtOffset(1, LogicalDirection.Forward) == null)
      return ret;

    ret = ret.GetPositionAtOffset(1, LogicalDirection.Forward);
  }

  return ret;
}

答案來源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM