Mohammad
Mohammad

Reputation: 1252

Calculate text width based on available height?

Is there any way to calculate text width based on available height in c# on windows forms?

Edit: I have the font size. I want to calculate the minimum width required for drawing the text considering the line could be wrapped.

Upvotes: 1

Views: 1387

Answers (1)

fixagon
fixagon

Reputation: 5566

what you can do is to measure a string in a default size with this method: (g is a Graphics Object)

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

you scale the fontsize depending on the proportion measuredHeight to availableHeight. After you can remeasure the string with the height of the available area

or you just measure it to get the proportions and calculated the expected width like that:

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

double expectedWidth = measuredProportion * wishedHeight;

Upvotes: 1

Related Questions