簡體   English   中英

對 SyndicationFeed 標題使用與 Richtextbox 中的摘要不同的字體 - C#

[英]Use different font for SyndicationFeed title than summary in richtextbox - C#

我正在用 C# 制作一個 RSS 閱讀器。 它獲取提要並將其添加到富文本框中。 有沒有辦法讓提要中的標題與富文本框中的摘要不同? 如果是這樣,如何? 這是我的代碼:

if (feedsList.SelectedItem != null)
{
     feedTxt.Text = "";
     string url = feedsList.GetItemText(feedsList.SelectedItem);
     Uri myUri = new Uri(url, UriKind.Absolute);
     XmlReader reader = XmlReader.Create(url);
     SyndicationFeed feed = SyndicationFeed.Load(reader);
     foreach (SyndicationItem item in feed.Items)
     {
          feedTxt.Text = feedTxt.Text + item.Title.Text;
          feedTxt.Text = feedTxt.Text + "\n\n" + item.Summary.Text + "\n\n";
     }
}

提前致謝!

您可以使用RichTextBox控件上的Selection屬性。 像這樣的東西:

if (feedsList.SelectedItem != null)
{
    feedTxt.Text = "";
    string url = feedsList.GetItemText(feedsList.SelectedItem);
    Uri myUri = new Uri(url, UriKind.Absolute);
    XmlReader reader = XmlReader.Create(url);
    SyndicationFeed feed = SyndicationFeed.Load(reader);

    foreach (SyndicationItem item in feed.Items)
    {
        // Store current content length of RichTextBox
        int currentLength = feedTxt.Length;

        // Append new content
        feedTxt.Text += item.Title.Text + "\n\n";
        feedTxt.Text += item.Summary.Text + "\n\n";

        // Set the font for Title using selection
        feedTxt.SelectionStart = currentLength.Length;
        feedTxt.SelectionLength = item.Title.Text.Length;
        feedTxt.SelectionFont = new System.Drawing.Font("Tahoma", 24);

        // Set the font for Summary using selection
        feedTxt.SelectionStart = currentLength + item.Title.Text.Length;
        feedTxt.SelectionLength = item.Summary.Text;
        feedTxt.SelectionFont = new System.Drawing.Font("Arial", 16);

        // Reset Selection
        txtTest.SelectionStart = 0;
        txtTest.SelectionLength = 0;
    }
}

更新

看起來你不能同時附加新的內容和樣式(Winforms 的可能限制?)。 我的簡單解決方案是在一次掃描中附加所有內容並計算您想要設置樣式的所有選擇並將它們推送到列表。

然后你再循環一遍你的所有選擇並做造型。

你首先需要這個結構:

  struct ArticleSelection
  {
      public int TitleStart { get; set; }
      public int TitleEnd { get; set; }
      public int SummaryStart { get; set; }
      public int SummaryEnd { get; set; }
  };

更新代碼

if (feedsList.SelectedItem != null)
{
    feedTxt.Text = "";
    string url = feedsList.GetItemText(feedsList.SelectedItem);
    Uri myUri = new Uri(url, UriKind.Absolute);
    XmlReader reader = XmlReader.Create(url);
    SyndicationFeed feed = SyndicationFeed.Load(reader);


    // Create List to store our selections
    List<ArticleSelection> articleSelections = new List<ArticleSelection>();

    // Loop all incoming content
    foreach (SyndicationItem item in feed.Items)
    {
        // Store current content length of RichTextBox
        int currentLength = feedTxt.Length;

        // Append new content
        feedTxt.Text += item.Title.Text + "\n\n";
        feedTxt.Text += item.Summary.Text + "\n\n";

        // Calculate selection
        articleSelections.Add(new ArticleSelection()
        {
            TitleStart = currentLength,
            TitleEnd = feed.Item1.Length,
            SummaryStart = currentLength + feed.Item1.Length,
            SummaryEnd = feedTxt.Text - currentLength // This accounts for new lines above
        }); ; 
    }


    // Loop through the content and style it
    foreach (ArticleSelection selection in articleSelections)
    { 
        // Set the selection for Title
        txtTest.SelectionStart = selection.TitleStart;
        txtTest.SelectionLength = selection.TitleEnd;
        txtTest.SelectionFont = new Font("Tahoma", 24);

        // Set the selection for Summary
        txtTest.SelectionStart = selection.SummaryStart;
        txtTest.SelectionLength = selection.SummaryEnd;
        txtTest.SelectionFont = new Font("Arial", 14);
    }

    // Remove Selection
    txtTest.DeselectAll();
}

你應該得到這樣的東西: 示例代碼輸出

暫無
暫無

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

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