LinkLabel 需要比 TextRenderer.MeasureText 更多的空间
如果我给 TextRenderer.MeasureText 一些要测量的文本和要使用的宽度,它将返回显示该文本所需的高度。
private static int CalculateHeight(string text, Font font, int width)
{
Size size = TextRenderer.MeasureText(text, font, new Size(width, Int32.MaxValue), TextFormatFlags.NoClipping | TextFormatFlags.WordBreak);
return size.Height;
}
如果我将文本、宽度和高度赋予 LinkLabel,它将按照提供的宽度和高度显示文本,而不会剪切任何内容。
但是,如果我将 Link 放入 LinkLabel.Links 集合中,LinkLabel 将绘制文本,字符之间的间距似乎稍大一些,有时这会导致文本末尾被剪裁。 有什么办法可以防止这种情况发生吗? 我尝试在有链接时添加填充,但没有可靠的方法来准确知道还需要多少空间。 还有其他方法可以做到这一点吗?
If I give TextRenderer.MeasureText some text to measure and width to use it will return the height needed to display that text.
private static int CalculateHeight(string text, Font font, int width)
{
Size size = TextRenderer.MeasureText(text, font, new Size(width, Int32.MaxValue), TextFormatFlags.NoClipping | TextFormatFlags.WordBreak);
return size.Height;
}
If I give that text, width and height to a LinkLabel it would display the text in the width and height provided with nothing clipped off.
However, if I put a Link into the LinkLabel.Links collection, the LinkLabel will draw the text with what appears to be a little more spacing between the characters and at times this will cause the end of the text to be clipped. Is there anyway to prevent this? I've tried adding padding when there is a link, but there's no reliable way to know exactly how much more space will be needed. Are there any other ways to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用 Control.GetPreferredSize方法来计算控制所需的宽度或高度(在您的情况下为 LinkLabel )。 您不应将 MeasureText 用于此类目的,您可以在此处找到更详细的说明(准确度TextRenderer.MeasureText 结果。)
You should use Control.GetPreferredSize method to calculate width or height needed for control (LinkLabel in your case). You should not use MeasureText for such purposes, more detailed explanation you can find here (Accuracy of TextRenderer.MeasureText results.)
如果
LinkLabel
包含多个链接,或者某些文本部分不在链接中,则控件使用Graphics.DrawString/MeasureString
而不是TextRenderer.DrawText/MeasureText
。 你可以很容易地看到它的实际效果,渲染中最大的区别是小L字母:If a
LinkLabel
contains more than one link, or there are parts of text which are nor in a link, then the control usesGraphics.DrawString/MeasureString
instead ofTextRenderer.DrawText/MeasureText
. You can easily see it in action, the biggest difference in rendering is with the small L letter:TextRenderer.MeasureText 是 DrawTextEx API。 返回的值来自 lprc 结构。 您可能想查看该 API 以了解更多详细信息。
TextRenderer.MeasureText is a managed wrapper for the DrawTextEx API. The value returned comes from the lprc struct. You might want to look at that API for more details.
我想你可以删除使其下划线的样式。
linkLabel.Styles.Add("text-decoration", "none");
但当然它看起来不像一个链接。 :-/我猜另一个解决方案是自己添加填充。
这不是最漂亮的解决方案,但我猜它是有效的。
I guess you could remove the style that makes it underline.
linkLabel.Styles.Add("text-decoration", "none");
but then of course it wouldn't look like a link. :-/Another solution would be to add the padding yourself I guess.
Not the prettiest of solutions, but I would guess it works.