标签具有不同的字体大小

时间:2014-05-27 17:00:44

标签: winforms label

基本上我想达到这样的目的:

enter image description here

但我不知道该怎么做,我尝试用2个标签来组合它们,但结果并不是很好......

4 个答案:

答案 0 :(得分:0)

创建一个继承自Label的新类,并覆盖 void OnPaint(PaintEventArgs e)方法以更改默认呈现行为:

public class MyLabel : Label
{
    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.DrawString("A", Font, new SolidBrush(ForeColor), 10, 10);
        e.Graphics.DrawString("B", new Font(Font.FontFamily, 20), new SolidBrush(ForeColor), 50, 10);
    }
}

结果," B"将是" A"的两倍。您可以以相同的方式实现目标,但是您必须计算子字符串的位置(" 145","。"," 54"并绘制它们。

答案 1 :(得分:0)

您需要从不同的角度绘制文本,即基线:

Public Class MyLabel
  Inherits Label

  <Browsable(False)> _
  Public Overrides Property AutoSize As Boolean
    Get
      Return False
    End Get
    Set(value As Boolean)
      'MyBase.AutoSize = value
    End Set
  End Property

  Protected Overrides Sub OnPaint(e As PaintEventArgs)
    'MyBase.OnPaint(e)
    Dim fromLine As Integer = Me.ClientSize.Height * 0.75
    Dim g As Graphics = e.Graphics
    Dim fontParts() As String = Me.Text.Split(".")
    Using bigFont As New Font(Me.Font.FontFamily, 20)
      TextRenderer.DrawText(g, fontParts(0), bigFont, _
                            New Point(0, fromLine - GetBaseLine(bigFont, g)), _
                            Me.ForeColor, Color.Empty)
      If fontParts.Length > 1 Then
        Dim bigWidth As Integer = TextRenderer.MeasureText(g, fontParts(0), bigFont, _
                                                           Point.Empty, TextFormatFlags.NoPadding).Width
        Using smallFont As New Font(Me.Font.FontFamily, 8)
          TextRenderer.DrawText(g, "." & fontParts(1), smallFont, _
                                New Point(bigWidth + 3, fromLine - GetBaseLine(smallFont, g)), _
                                Me.ForeColor, Color.Empty)
        End Using
      End If
    End Using
  End Sub

  Private Function GetBaseLine(fromFont As Font, g As Graphics) As Single
    Dim fontHeight As Single = fromFont.GetHeight(g)
    Dim lineSpacing As Single = fromFont.FontFamily.GetLineSpacing(fromFont.Style)
    Dim cellAscent As Single = fromFont.FontFamily.GetCellAscent(fromFont.Style)
    Return fontHeight * cellAscent / lineSpacing
  End Function

End Class

代码基本上测量一行中字体的高度。在我的示例中,我使用了Label的客户端空间的底部25%来表示,从这一行开始绘制:Me.ClientSize.Height * 0.75

对于您使用的每种字体,您必须测量该字体的基线并从绘图线中减去该字体,以便抵消文本的绘图位置。

由于锯齿和字形悬垂,测量单个角色的尺寸并不容易。我在大文本和小文本之间添加了一个小填充:bigWidth + 3以使其看起来很好。如果大数以7结尾,那么距离看起来有点偏,因为7的茎是倾斜的。

结果:

enter image description here

答案 2 :(得分:0)

将devexpress LabelControl.AllowHtmlString属性设置为true,并使用<size>的Text属性中支持的LabelControl标记,详见HTML文本格式文档。

答案 3 :(得分:0)

您可以在Windows窗体中使用用户控件WPF。要做到这一步。 1.将用户控件添加到Windows窗体 2.来自xml的usercontrol名称网格,如t1 3.将此函数添加到usercontrol.wpf.cs

public void Actor(string text)
{
    StringBuilder sb = new StringBuilder();
    sb.Append(@"<TextBlock   xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
                    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> ");
    sb.Append(text);
    sb.Append(@"</TextBlock>");
    TextBlock myButton = (TextBlock)XamlReader.Parse(sb.ToString());
    this.t1.Children.Clear();
    t1.Children.Add(myButton);
}

4。之后,从form1.css中添加此函数。

              userControl11.Actor("<Run Text='Hi ' FontWeight='Bold'/><Run Text='Hitler ' FontWeight='Bold'/>");
    userControl11.Actor(" < Run FontWeight = 'Bold' FontSize = '14' Text = 'This is WPF TextBlock Example. ' />");

你可以使用xml wpf管理Actor函数的写代码“”。