为什么TextRenderer.MeasureText无法正常工作? [英] Why doesn't TextRenderer.MeasureText work properly?

查看:119
本文介绍了为什么TextRenderer.MeasureText无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在给定可用画布一定宽度的情况下,我想测量文本的高度。我传递的文字非常长,我知道会自动换行。为此,我将其称为:

I want to measure the height of the text given a certain width of available canvas. The text that I pass in is really long and I know will wrap. To that end, I call the following:

using System.Windows.Forms;
...
string text = "Really really long text that is sure to wrap...";
Font font = new Font("Arial", 14);
Size canvas = new Size(1100, 850);
Size size = TextRenderer.MeasureText(text, font, canvas);

无论我为画布传递什么,它总是为 size返回14高度

No matter what I pass in for canvas, it always returns 14 for size.Height.

我错过了一些简单的东西吗?

Am I missing something simple?

推荐答案

请使用 TextFormatFlags 测量参数,如下所示:

Please, use the TextFormatFlags measure parameter as shown below:

Size size = TextRenderer.MeasureText(text, font, canvas, TextFormatFlags.WordBreak);

这篇关于为什么TextRenderer.MeasureText无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆