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

前端 未结 12 615
离开以前
离开以前 2020-11-30 12:12

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

label.text = \"asd\" + string;

Would like the string

相关标签:
12条回答
  • 2020-11-30 12:50

    In ASP.NET you could do:

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

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

    0 讨论(0)
  • 2020-11-30 12:58

    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.

    0 讨论(0)
  • 2020-11-30 12:58

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

    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.Text you assign it to RichTextBox.Rtf, and apply some simple RTF formatting.

    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 ");
      }
    
    0 讨论(0)
  • 2020-11-30 12:58

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

    0 讨论(0)
  • 2020-11-30 12:59

    I've built a UserControl that holds a TransparentRichTextBox allowing you to format portions of text by using the RTF syntax.

    Nothing special and the syntax is quite easy to understand. Check it out here.

    0 讨论(0)
  • 2020-11-30 13:01

    WinForms doesn't allow you to do that.

    0 讨论(0)
提交回复
热议问题