Why doesn't TextRenderer.MeasureText work properly?

懵懂的女人 提交于 2019-12-07 05:37:16

问题


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);

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

Am I missing something simple?


回答1:


Please, use the TextFormatFlags measure parameter as shown below:

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


来源:https://stackoverflow.com/questions/8361555/why-doesnt-textrenderer-measuretext-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!