C# 将标签文本的一部分设置为粗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2063263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:09:53  来源:igfitidea点击:

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

c#.netwinforms

提问by Phil

Is there any way to make a part of a label.textto be bold?

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

label.text = "asd" + string;

Would like the stringportion to be bold.

希望该string部分加粗。

Is possible, how can this be done?

有可能,这怎么办?

采纳答案by iburlakov

WinForms doesn't allow you to do that.

WinForms 不允许您这样做。

回答by Tadas ?ukys

In WinForms override Label.OnPaint() method and draw the text your self.

在 WinForms 中覆盖 Label.OnPaint() 方法并绘制自己的文本。

回答by Town

In ASP.NET you could do:

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

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

But like everyone else says, depends on what you're using.

但就像其他人说的,这取决于你使用的是什么。

回答by Chris S

WebForms

网页表格

Use Literalcontrol, and add a <b>tag around the part of the text you want:

使用Literal控件,并<b>在您想要的文本部分周围添加一个标签:

_myLiteral.Text = "Hello <b>big</b>world";

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

Winforms

Winforms

Two options:

两种选择:

  1. Put two labels side by side (far easier)
  2. Subclass Labeland do your own custom drawing in the OnPaint()method.
  1. 并排放置两个标签(更容易)
  2. 子类化Label并在该OnPaint()方法中进行您自己的自定义绘图。

The second choice has been answeredalready.

已经回答了第二个选择。

回答by Adrian Faciu

The easy way to do what you want is just to add two labels. In this way you could make one bold, and it will look ok with a proper positioning.

做你想做的最简单的方法就是添加两个标签。通过这种方式,您可以使一个粗体,并且在适当的位置下看起来不错。

The normal way would be to create a control that has two or more labels and you could set the properties on each one of them. Also this has the advantage that is reusable.

通常的方法是创建一个具有两个或多个标签的控件,您可以为每个标签设置属性。这也具有可重复使用的优点。

回答by Simon

Does it need to be a Labelcontrol, or do you just need to put text in a particular place? If the former, you'll need to do custom painting as other people have noted. If not, you could use a readonly RichTextBoxinstead.

它需要是一个Label控件,还是只需要将文本放在特定的位置?如果是前者,您将需要像其他人所指出的那样进行自定义绘画。如果没有,您可以使用 readonlyRichTextBox代替。

回答by particle

The following class illustrates how to do it by overriding OnPaint()in the Labelclass of WinForms. You can refine it. But what I did was to use the pipe character (|) in a string to tell the OnPaint()method to print text before the |as bold and after it as normal text.

下面的类说明了如何通过OnPaint()LabelWinForms 类中进行覆盖来实现。你可以细化它。但是我所做的是|在字符串中使用竖线字符 ( ) 来告诉该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);                
        }
    }
}

Here's how to use it:

以下是如何使用它:

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

Hello will be printed in boldand world in normal.

Hello 将以粗体打印,而 world 将以普通打印。

回答by RenniePet

This is an elaboration on Simon's suggestion of replacing the Label control with a readonly RichTextBoxcontrol.

这是对 Simon 将 Label 控件替换为只读RichTextBox控件的建议的详细说明。

  1. Replace the Label control with a RichTextBox control, same location and size. In following notes the name of the control is rtbResults.

  2. Make it readonly: rtbResults.ReadOnly = True;

  3. No borders: rtbResults.BorderStyle = BorderStyle.None;

  4. Instead of assigning the string to be displayed to Label.Textyou assign it to RichTextBox.Rtf, and apply some simple RTF formatting.

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

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

  3. 无边界: rtbResults.BorderStyle = BorderStyle.None;

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

The following code is a sample - it displays words generated by a Scrabble cheater program where the high-value letters are in bold.

以下代码是一个示例 - 它显示由拼字游戏作弊程序生成的单词,其中高值字母以粗体显示。

  /// <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 ");
  }

回答by H. Abraham Chavez

It depends on how pragmatic you are. Something that sounds overkill, but could work, is to use a web browser control in your form, and feed into it HTML markup. As I said overkill for a label, but if you have more than one line of text you need to format, it might be an option. – H. Abraham Chavez just now edit

这取决于你有多务实。听起来有些矫枉过正但可行的方法是在表单中使用 Web 浏览器控件,并将 HTML 标记输入其中。正如我所说的标签过度,但如果您需要格式化多行文本,它可能是一种选择。– H. Abraham Chavez 刚刚编辑

回答by vezenkov

Use Infragistics' UltraLabelcontrol - it supports html formatting. There are probably other third party solutions too.

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