Reputation: 19
The Graphics.MeasureString(string, Font) method is not accurate on windows mobile. Is there a solution for the problem?
public static string GetSubString(Graphics graphic, Font font,
string text, int availableWidth)
{
string tempString = string.Empty;
Size stringSize = graphic.MeasureString(text, font).ToSize();
if (stringSize.Width < availableWidth)
{
return text;
}
for (int index = text.Length-1; index > 3; index--)
{
tempString = string.Format("{0}...", text.Substring(0, index));
Size tempStringSize = graphic.MeasureString(tempString, font).ToSize();
if (tempStringSize.Width < availableWidth)
{
return tempString;
}
}
}
The method return incorrect size, not matching the width of the label.
Upvotes: 1
Views: 1657
Reputation: 18013
Graphics.MeasureString
is accurate only if you use Graphics.DrawString
.
Is it a Windows.Forms.Label
? Then it uses Graphics.DrawString
only if UseCompatibleTextRendering
property is true. Otherwise, you should use TextRenderer.MeasureText
and TextRenderer.DrawText
methods instead.
Upvotes: 1