如何将字符串长度转换为像素单位?

发布于 2024-07-11 06:12:47 字数 275 浏览 17 评论 0原文

我有一个像这样的字符串:

string s = "This is my string";

我正在创建一个 Telerik 报告,我需要定义一个文本框,它是我的字符串的宽度。 但是,大小属性需要设置为单位(像素、点、英寸等)。 如何将字符串长度转换为像素,以便设置宽度?

编辑:我尝试获取对图形对象的引用,但这是在继承自Telerik.Reporting.Report的类中完成的。

I have a string like this:

string s = "This is my string";

I am creating a Telerik report and I need to define a textbox that is the width of my string. However the size property needs to be set to a Unit (Pixel, Point, Inch, etc). How can I convert my string length into, say a Pixel so I can set the width?

EDIT: I have tried getting a reference to the graphics object, but this is done in a class that inherits from Telerik.Reporting.Report.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心凉怎暖 2024-07-18 06:12:49

也取决于字体。 字符串长度不够。

Depends on the font, too. String length isn't sufficient.

最佳男配角 2024-07-18 06:12:49

您可以使用 MeasureString() 方法创建图形对象的实例。 但您需要向其传递字体名称、字体大小和其他信息。

You can create an instance of a graphics object an use the MeasureString() method. But you will need to pass it the font name, font size, and other info.

别想她 2024-07-18 06:12:49

我编写了三个相同的函数,如下所示。 仅更改签名即可使所有功能以相同的名称和谐相处。 我将它们放在一个名为“StringExtensions”的类中,并将每个函数声明为“共享”,以便外部编码可以访问它们,而无需创建该类的实例变量。

  Public Shared Function TextLengthInPixels(oTextBox As TextBox, bTrim As Boolean) As Integer
      Return TextRenderer.MeasureText(IIf(bTrim, oTextBox.Text.Trim, oTextBox.Text), oTextBox.Font).Width
  End Function

  Public Shared Function TextLengthInPixels(oLabel As Label, bTrim As Boolean) As Integer
      Return TextRenderer.MeasureText(IIf(bTrim, oLabel.Text.Trim, oLabel.Text), oLabel.Font).Width
  End Function

  Public Shared Function TextLengthInPixels(oRTB As RichTextBox, bTrim As Boolean) As Integer
      Return TextRenderer.MeasureText(IIf(bTrim, oRTB.Text.Trim, oRTB.Text), oRTB.Font).Width
  End Function

只需使用您想要的任何一个(或为具有“文本”属性的其他控件编写更多内容)。

I wrote three identical functions as shown below. Only the signatures are changed to enable all functions to live in harmony with the same name. I placed them in a Class called "StringExtensions" and declared each function as "Shared" so external coding could access them without creating an instance variable of the class.

  Public Shared Function TextLengthInPixels(oTextBox As TextBox, bTrim As Boolean) As Integer
      Return TextRenderer.MeasureText(IIf(bTrim, oTextBox.Text.Trim, oTextBox.Text), oTextBox.Font).Width
  End Function

  Public Shared Function TextLengthInPixels(oLabel As Label, bTrim As Boolean) As Integer
      Return TextRenderer.MeasureText(IIf(bTrim, oLabel.Text.Trim, oLabel.Text), oLabel.Font).Width
  End Function

  Public Shared Function TextLengthInPixels(oRTB As RichTextBox, bTrim As Boolean) As Integer
      Return TextRenderer.MeasureText(IIf(bTrim, oRTB.Text.Trim, oRTB.Text), oRTB.Font).Width
  End Function

Just use whichever one you want (or write more for other controls which have a "text" property).

红衣飘飘貌似仙 2024-07-18 06:12:48

不使用控件或表单:

using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
{
    SizeF size = graphics.MeasureString("Hello there", new Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Point));
}

或者在 VB.Net 中:

Using graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(New Bitmap(1, 1))
    Dim size As SizeF = graphics.MeasureString("Hello there", New Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Point))
End Using

Without using of a control or form:

using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
{
    SizeF size = graphics.MeasureString("Hello there", new Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Point));
}

Or in VB.Net:

Using graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(New Bitmap(1, 1))
    Dim size As SizeF = graphics.MeasureString("Hello there", New Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Point))
End Using
白芷 2024-07-18 06:12:48
Size textSize = TextRenderer.MeasureText("How long am I?", font);
Size textSize = TextRenderer.MeasureText("How long am I?", font);
八巷 2024-07-18 06:12:48

在这种情况下,我通常使用一种肮脏但简单的方法:

  • 我添加一个不可见的 Label ,其 AutoSize 属性为 true -肮脏的工作-。
  • 当我想要特定字符串的宽度时,我将其设置为Label.Text。
  • LabelWidth 将为我提供正确的值。

In this case, I usually use a dirty, but simple way:

  • I add an invisible Label that its AutoSize property is true -dirty work-.
  • When I want to have the Width for a specific string, I set it to the Label.Text.
  • The Width of the Label will give me the correct value.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文