簡體   English   中英

將標簽文本的一部分設置為粗體

[英]Make portion of a Label's Text to be styled bold

有沒有辦法讓label.text的一部分label.text

label.text = "asd" + string;

希望string部分為粗體。

有可能,這怎么辦?

下面的類說明了如何通過覆蓋 WinForms 的Label類中的OnPaint()來實現。 你可以細化它。 但是我所做的是在字符串中使用管道字符 ( | ) 來告訴OnPaint()方法在|之前打印文本| 和普通文本一樣加粗。

class LabelX : Label
{
    protected override void OnPaint(PaintEventArgs e) {
        Point drawPoint = new Point(0, 0);

        string[] ary = Text.Split(new char[] { '|' });
        if (ary.Length == 2) {
            Font normalFont = this.Font;

            Font boldFont = new Font(normalFont, FontStyle.Bold);

            Size boldSize = TextRenderer.MeasureText(ary[0], boldFont);
            Size normalSize = TextRenderer.MeasureText(ary[1], normalFont);

            Rectangle boldRect = new Rectangle(drawPoint, boldSize);
            Rectangle normalRect = new Rectangle(
                boldRect.Right, boldRect.Top, normalSize.Width, normalSize.Height);

            TextRenderer.DrawText(e.Graphics, ary[0], boldFont, boldRect, ForeColor);
            TextRenderer.DrawText(e.Graphics, ary[1], normalFont, normalRect, ForeColor);
        }
        else {

            TextRenderer.DrawText(e.Graphics, Text, Font, drawPoint, ForeColor);                
        }
    }
}

以下是如何使用它:

LabelX x = new LabelX();
Controls.Add(x);
x.Dock = DockStyle.Top;
x.Text = "Hello | World";       

Hello 將以粗體打印,而 world 將以普通打印。

網頁表格

使用Literal控件,並在您想要的文本部分周圍添加一個<b>標簽:

_myLiteral.Text = "你好<b></b>世界";

Winforms

兩種選擇:

  1. 並排放置兩個標簽(更容易)
  2. 子類Label並在OnPaint()方法中執行您自己的自定義繪圖。

已經回答了第二個選擇。

WinForms 不允許您這樣做。

這是對 Simon 將 Label 控件替換為只讀RichTextBox控件的建議的詳細說明。

  1. 將 Label 控件替換為 RichTextBox 控件,位置和大小相同。 在下面的注釋中,控件的名稱是 rtbResults。

  2. 使其只讀: rtbResults.ReadOnly = True;

  3. 無邊框: rtbResults.BorderStyle = BorderStyle.None;

  4. 不是將要顯示的字符串分配給Label.Text而是將其分配給RichTextBox.Rtf ,並應用一些簡單的 RTF 格式。

以下代碼是一個示例 - 它顯示由拼字游戲作弊程序生成的單詞,其中高值字母以粗體顯示。

  /// <summary>
  /// Method to display the results in the RichTextBox, prefixed with "Results: " and with the 
  /// letters J, Q, X and Z in bold type.
  /// </summary>
  private void DisplayResults(string resultString)
  {
     resultString = MakeSubStringBold(resultString, "J");
     resultString = MakeSubStringBold(resultString, "Q");
     resultString = MakeSubStringBold(resultString, "X");
     resultString = MakeSubStringBold(resultString, "Z");

     rtbResults.Rtf = @"{\rtf1\ansi " + "Results: " + resultString + "}";
  }


  /// <summary>
  /// Method to apply RTF-style formatting to make all occurrences of a substring in a string 
  /// bold. 
  /// </summary>
  private static string MakeSubStringBold(string theString, string subString)
  {
     return theString.Replace(subString, @"\b " + subString + @"\b0 ");
  }

做你想做的最簡單的方法就是添加兩個標簽。 通過這種方式,您可以使一個粗體,並且在適當的位置下看起來不錯。

通常的方法是創建一個具有兩個或多個標簽的控件,您可以為每個標簽設置屬性。 這也具有可重復使用的優點。

它需要是一個Label控件,還是只需要將文本放在特定的位置? 如果是前者,您將需要像其他人所指出的那樣進行自定義繪畫。 如果沒有,您可以改用只讀RichTextBox

在 WinForms 中覆蓋 Label.OnPaint() 方法並繪制自己的文本。

在 ASP.NET 中,您可以執行以下操作:

label.Text = string.Format("asd <span style='font-weight: bold;'>{0}</span>", string);

但就像其他人說的,這取決於你使用的是什么。

這取決於你有多務實。 聽起來有些矯枉過正但可行的方法是在表單中使用 Web 瀏覽器控件,並將 HTML 標記輸入其中。 正如我所說的對標簽來說太過分了,但是如果您需要格式化的文本不止一行,那么它可能是一種選擇。 – H. Abraham Chavez 剛剛編輯

使用 Infragistics 的UltraLabel控件 - 它支持 html 格式。 可能還有其他第三方解決方案。

我已經構建了一個UserControl ,它包含一個TransparentRichTextBox允許您使用 RTF 語法來設置文本部分的格式。

沒什么特別的,語法也很容易理解。 在這里查看

VB.NET解決方案

我接受了@affan 的答案,即擴展Label類並覆蓋OnPaint方法。

我將他的解決方案翻譯成 VB 並進行了一些更改以克服我在填充方面遇到的一些問題。 我的版本也使管道字符右側的文本| 粗體而不是左邊。

示例: 示例

Imports System.Windows.Forms
Imports System.Drawing

' Add some custom functionality to the standard Label Class
Public Class CustomLabel
    Inherits Label

    ' Allow bold font for right half of a label 
    ' indicated by the placement of a pipe char '|' in the string (ex. "Hello | World" will make bold 'World'
    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Dim drawPoint As Point = New Point(0, 0)
        Dim boldDelimiter As Char = "|"c

        Dim ary() As String = Me.Text.Split(boldDelimiter)

        If ary.Length = 2 Then
            Dim normalFont As Font = Me.Font
            Dim boldFont As Font = New Font(normalFont, FontStyle.Bold)

            ' Set TextFormatFlags to no padding so strings are drawn together.
            Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

            ' Declare a proposed size with dimensions set to the maximum integer value. https://msdn.microsoft.com/en-us/library/8wafk2kt(v=vs.110).aspx
            Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

            Dim normalSize As Size = TextRenderer.MeasureText(e.Graphics, ary(0), normalFont, proposedSize, flags)
            Dim boldSize As Size = TextRenderer.MeasureText(e.Graphics, ary(1), boldFont, proposedSize, flags)

            Dim normalRect As Rectangle = New Rectangle(drawPoint, normalSize)
            Dim boldRect As Rectangle = New Rectangle(normalRect.Right, normalRect.Top, boldSize.Width, boldSize.Height)

            

            TextRenderer.DrawText(e.Graphics, ary(0), normalFont, normalRect, Me.ForeColor, flags)
            TextRenderer.DrawText(e.Graphics, ary(1), boldFont, boldRect, Me.ForeColor, flags)

        Else
            ' Default to base class method
            MyBase.OnPaint(e)
        End If

    End Sub


End Class

暫無
暫無

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

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