[英]Make portion of a Label's Text to be styled bold
有没有办法让label.text
的一部分label.text
?
label.text = "asd" + string;
希望string
部分为粗体。
有可能,这怎么办?
下面的类说明了如何通过覆盖 WinForms 的Label
类中的OnPaint()
来实现。 你可以细化它。 但是我所做的是在字符串中使用管道字符 ( |
) 来告诉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);
}
}
}
以下是如何使用它:
LabelX x = new LabelX();
Controls.Add(x);
x.Dock = DockStyle.Top;
x.Text = "Hello | World";
Hello 将以粗体打印,而 world 将以普通打印。
使用Literal
控件,并在您想要的文本部分周围添加一个<b>
标签:
_myLiteral.Text = "你好<b>
大</b>
世界";
两种选择:
Label
并在OnPaint()
方法中执行您自己的自定义绘图。已经回答了第二个选择。
WinForms 不允许您这样做。
这是对 Simon 将 Label 控件替换为只读RichTextBox
控件的建议的详细说明。
将 Label 控件替换为 RichTextBox 控件,位置和大小相同。 在下面的注释中,控件的名称是 rtbResults。
使其只读: rtbResults.ReadOnly = True;
无边框: rtbResults.BorderStyle = BorderStyle.None;
不是将要显示的字符串分配给Label.Text
而是将其分配给RichTextBox.Rtf
,并应用一些简单的 RTF 格式。
以下代码是一个示例 - 它显示由拼字游戏作弊程序生成的单词,其中高值字母以粗体显示。
/// <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 ");
}
做你想做的最简单的方法就是添加两个标签。 通过这种方式,您可以使一个粗体,并且在适当的位置下看起来不错。
通常的方法是创建一个具有两个或多个标签的控件,您可以为每个标签设置属性。 这也具有可重复使用的优点。
它需要是一个Label
控件,还是只需要将文本放在特定的位置? 如果是前者,您将需要像其他人所指出的那样进行自定义绘画。 如果没有,您可以改用只读RichTextBox
。
在 WinForms 中覆盖 Label.OnPaint() 方法并绘制自己的文本。
在 ASP.NET 中,您可以执行以下操作:
label.Text = string.Format("asd <span style='font-weight: bold;'>{0}</span>", string);
但就像其他人说的,这取决于你使用的是什么。
这取决于你有多务实。 听起来有些矫枉过正但可行的方法是在表单中使用 Web 浏览器控件,并将 HTML 标记输入其中。 正如我所说的对标签来说太过分了,但是如果您需要格式化的文本不止一行,那么它可能是一种选择。 – H. Abraham Chavez 刚刚编辑
使用 Infragistics 的UltraLabel控件 - 它支持 html 格式。 可能还有其他第三方解决方案。
我已经构建了一个UserControl
,它包含一个TransparentRichTextBox
允许您使用 RTF 语法来设置文本部分的格式。
没什么特别的,语法也很容易理解。 在这里查看。
VB.NET解决方案
我接受了@affan 的答案,即扩展Label
类并覆盖OnPaint
方法。
我将他的解决方案翻译成 VB 并进行了一些更改以克服我在填充方面遇到的一些问题。 我的版本也使管道字符右侧的文本|
粗体而不是左边。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.