467,923 Members | 1,250 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Clever Techniques for Determing Dimensions of Multi-Line Label?

Long ago I developed a simple algorithm for calculating how much space is
required to display a multi-line label in a limited width. It seemed to be
working okay but then my testing revealed a flaw when trying to display the
following string with an MS Sans Serif 8 point font in a 208 pixel width
space:

"A $50-million investment in cycling infrastructure - the largest history of
the province"

Using the Graphics "MeasureString" function it determined that 2 lines were
necessary to display this text. But unfortunately the longer words forced a
word-wrap sooner than usual. So the string ended up being displayed like
this:

A $50-million investment in cycling
infrastructure - the largest history of the
province
As you can see, this is 3 lines, not 2. I realize I could overcompensate
the height required but then this will add an excessive vertical margin in
many cases. So I'm wondering if anyone has come up with a superior, accurate
approach to this problem?

--
Robert W.
Vancouver, BC
www.mwtech.com

Mar 22 '06 #1
  • viewed: 7387
Share:
3 Replies
is it ok with you to use fixed width fonts? at least you do not need any
calcualtions.

thank you.
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:90**********************************@microsof t.com...
Long ago I developed a simple algorithm for calculating how much space is
required to display a multi-line label in a limited width. It seemed to
be
working okay but then my testing revealed a flaw when trying to display
the
following string with an MS Sans Serif 8 point font in a 208 pixel width
space:

"A $50-million investment in cycling infrastructure - the largest history
of
the province"

Using the Graphics "MeasureString" function it determined that 2 lines
were
necessary to display this text. But unfortunately the longer words forced
a
word-wrap sooner than usual. So the string ended up being displayed like
this:

A $50-million investment in cycling
infrastructure - the largest history of the
province
As you can see, this is 3 lines, not 2. I realize I could overcompensate
the height required but then this will add an excessive vertical margin in
many cases. So I'm wondering if anyone has come up with a superior,
accurate
approach to this problem?

--
Robert W.
Vancouver, BC
www.mwtech.com

Mar 22 '06 #2
Mohammad,

Actually, using fixed-width fonts would NOT solve the problem because the
words will typically wrap sooner than one expects.

In any case, I've done more research and found some code which I modified
for my own purposes. I'll post it here, as it may help others in the future:

public static int MeasureLabelHeight(string text, Font font, int maxWid)
{
Form form1 = new Form(); // Necessary to access
'CreateGraphics' below
Graphics g = form1.CreateGraphics(); // Create a Graphics object
for the Control

System.Drawing.StringFormat format = new System.Drawing.StringFormat ();
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 0,
maxWid, 10000);
System.Drawing.CharacterRange[] ranges = {new
System.Drawing.CharacterRange(0, text.Length)};
System.Drawing.Region[] regions = new System.Drawing.Region[1];

format.SetMeasurableCharacterRanges(ranges);

regions = g.MeasureCharacterRanges (text, font, rect, format);
rect = regions[0].GetBounds(g);

return (int)(rect.Bottom + 1.0F);
}


--
Robert W.
Vancouver, BC
www.mwtech.com

"Mohammad Shalabi" wrote:
is it ok with you to use fixed width fonts? at least you do not need any
calcualtions.

thank you.
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:90**********************************@microsof t.com...
Long ago I developed a simple algorithm for calculating how much space is
required to display a multi-line label in a limited width. It seemed to
be
working okay but then my testing revealed a flaw when trying to display
the
following string with an MS Sans Serif 8 point font in a 208 pixel width
space:

"A $50-million investment in cycling infrastructure - the largest history
of
the province"

Using the Graphics "MeasureString" function it determined that 2 lines
were
necessary to display this text. But unfortunately the longer words forced
a
word-wrap sooner than usual. So the string ended up being displayed like
this:

A $50-million investment in cycling
infrastructure - the largest history of the
province
As you can see, this is 3 lines, not 2. I realize I could overcompensate
the height required but then this will add an excessive vertical margin in
many cases. So I'm wondering if anyone has come up with a superior,
accurate
approach to this problem?

--
Robert W.
Vancouver, BC
www.mwtech.com


Mar 22 '06 #3


Robert W. wrote:
Using the Graphics "MeasureString" function it determined that 2 lines were
necessary to display this text. But unfortunately the longer words forced a
word-wrap sooner than usual. So the string ended up being displayed like
this:


Why are you determining the number of lines? MeasureString returns the
area needed.

Is this using SizeF MeasureString(string text, Font font, SizeF
layoutAread, StringFormat format)?

Try using the following code:

Label l = ...;
SizeF area = new SizeF(
l.ClientRectangle.Width, float.PositiveInfinity);
/* l.Format, but Label does not expose Format */
StringFormat format = StringFormat.GenericTypographic;

SizeF needed = graphics.MeasureString(
l.Text, l.font, area, format);
int borders = l.Height - l.ClientRectangle.Height;
l.Height = (int)(Math.Ceiling(needed.Height) + borders);

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Mar 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by MurrayTh | last post: by
1 post views Thread by johnrees | last post: by
2 posts views Thread by Miles | last post: by
reply views Thread by Chris Thomasson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.