Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

winforms

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

label.text = "asd" + string;

Would like the string portion to be bold.

Is possible, how can this be done?

like image 381
Phil Avatar asked Jan 14 '10 09:01

Phil


People also ask

How do I make certain text bold?

text. style. StyleSpan(Typeface. BOLD), start, end, Spannable.

How do I bold text in a label in HTML?

To bold the text in HTML, use either the strong tag or the b (bold) tag. Browsers will bold the text inside both of these tags the same, but the strong tag indicates that the text is of particular importance or urgency. You can also bold text with the CSS font-weight property set to “bold.”

How do I bold text in a label in asp net?

Another option would be setting the Font. Bold property of the label to True. Assuming you can use several labels of course.

What is bold text formatting?

Formatting elements were designed to display special types of text: <b> - Bold text. <strong> - Important text.


3 Answers

The following class illustrates how to do it by overriding OnPaint() in the Label class 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.

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 bold and world in normal.

like image 116
particle Avatar answered Oct 14 '22 22:10

particle


WebForms

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

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

Winforms

Two options:

  1. Put two labels side by side (far easier)
  2. Subclass Label and do your own custom drawing in the OnPaint() method.

The second choice has been answered already.

like image 24
Chris S Avatar answered Oct 14 '22 23:10

Chris S


WinForms doesn't allow you to do that.

like image 22
iburlakov Avatar answered Oct 14 '22 21:10

iburlakov