【问题标题】:Calculate text width based on available height?根据可用高度计算文本宽度?
【发布时间】:2011-06-06 07:20:07
【问题描述】:

有没有办法根据 c# 在 windows 窗体上的可用高度计算文本宽度?

编辑:我有字体大小。考虑到可以换行,我想计算绘制文本所需的最小宽度。

【问题讨论】:

  • 您必须使用在给定文本、字体和宽度的情况下获取所需高度的能力进行迭代

标签: c# winforms


【解决方案1】:

你可以做的是用这个方法测量一个默认大小的字符串: (g 是一个图形对象)

g.MeasureString("area", Font, maxWidth)

您根据measuredHeight 与availableHeight 的比例来缩放字体大小。之后可以用可用区域的高度重新测量字符串

或者你只是测量它以获得比例并计算出预期的宽度:

float measureFontSize = 5;
SizeF measuredBox = g.MeasureString("my string", new Font("Arial", measureFontSize));
double measuredProportion = measuredBox.Width / measuredBox.Height;

double expectedWidth = measuredProportion * wishedHeight;

【讨论】:

    猜你喜欢
    • 2010-10-28
    • 2017-02-23
    • 2013-08-26
    • 1970-01-01
    • 2011-04-01
    • 2014-02-16
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多