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

前端 未结 12 614
离开以前
离开以前 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:40

    VB.NET Solution

    I took @affan's answer answer of extending the Label Class and overriding the OnPaint method.

    I translated his solution into VB and made a few changes to overcome some issues I was having with padding. My version also makes the text to the right of the pipe character | bold instead of the left.

    Example:

    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
    
    0 讨论(0)
  • 2020-11-30 12:42

    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

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

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

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

    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.

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

    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.

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

    Does it need to be a Label control, 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 RichTextBox instead.

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