将Label的文本部分设置为粗体样式

Phi*_*hil 30 .net c# winforms

有没有办法让a的一部分label.text变得大胆?

label.text = "asd" + string;
Run Code Online (Sandbox Code Playgroud)

希望该string部分是大胆的.

有可能,这怎么办?

par*_*cle 27

下面的类演示了如何通过重写做OnPaint()Label类的WinForms的.你可以改进它.但我所做的是|在字符串中使用管道字符()来告诉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);                
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是如何使用它:

LabelX x = new LabelX();
Controls.Add(x);
x.Dock = DockStyle.Top;
x.Text = "Hello | World";       
Run Code Online (Sandbox Code Playgroud)

您好将以粗体和世界正常打印.

  • 这不适用于.NET Window Forms. (3认同)

ibu*_*kov 20

WinForms不允许这样做.

  • 这是什么样的答案? (4认同)
  • 这不是特别容易,但可能. (3认同)
  • 不是一个好答案 (2认同)

Chr*_*s S 19

的WebForms

使用Literal控件,并<b>在所需文本的部分周围添加标记:

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

的WinForms

两种选择:

  1. 并排放置两个标签(更容易)
  2. 子类Label并在OnPaint()方法中执行您自己的自定义绘图.

第二种选择已经得到了回答.


Ren*_*Pet 5

这是对Simon建议用readonly RichTextBox控件替换Label控件的一个详细说明.

  1. 将Label控件替换为RichTextBox控件,位置和大小相同.在以下注释中,控件的名称是rtbResults.

  2. 让它只读: rtbResults.ReadOnly = True;

  3. 没有国界: rtbResults.BorderStyle = BorderStyle.None;

  4. 而不是分配要显示的字符串,Label.Text您将其分配给RichTextBox.Rtf,并应用一些简单的RTF格式.

以下代码是一个示例 - 它显示由Scrabble骗子程序生成的单词,其中高值字母以粗体显示.

  /// <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 ");
  }
Run Code Online (Sandbox Code Playgroud)