473,378 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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
3 7643
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John | last post by:
I am rotating images at one location of my web site. My problem is if I set the width and height of the new image before I show the new image, the old image is stretched first to the new image...
24
by: Bob Alston | last post by:
Most of my Access database implementations have been fairly small in terms of data volume and number of concurrent users. So far I haven't had performance issues to worry about. <knock on wood> ...
6
by: MurrayTh | last post by:
Is there any way to determine the dimensions of an image using the image's URL? ie load image based on URL, and then get dimensions? or perhaps better method?
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
0
by: Stu | last post by:
Hi, Im using VB dot net and crystal 11 I have a mainReport with a parameter @Idcustomer and in the mainReport I have a subreport @customerId They are linked using crystal. I would like to...
2
by: lgbjr | last post by:
Hi All, Is there a way to sort multiple dimensions of an array in VB.NET? Let's say I have the following in a multi-dimension array: 3081 100 2 3081 100 1 3081 20 1 3081 1 2 3021 100 2
1
by: johnrees | last post by:
I am building a photo gallery using code which creates thumbnails of a specific size from any image. When clicked the thumbnail expands dynamically to a pre-determined size. The sizes of the...
1
by: Mentat | last post by:
In the description below ??? marks specific problem areas. Any help is appreciated. Required ======== A method to detect the width and height values of a <DIV... element. Since there are two...
2
by: Miles | last post by:
Hi all, Wondering if anyone can help me. If i have an associative array: $arr = array( "one" =array(1, 2, 3), "two" =array(5, 6), "three" =array(7,8,9,10) .... "n" =array(p,q,r....)
0
by: Chris Thomasson | last post by:
If you have been following the "Is this standard c++..." thread in this group, then you may know that I am currently very busy developing a stable and robust implementation for my multi-thread...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.