473,804 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "MeasureStr ing" 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 7660
is it ok with you to use fixed width fonts? at least you do not need any
calcualtions.

thank you.
"Robert W." <Ro*****@discus sions.microsoft .com> wrote in message
news:90******** *************** ***********@mic rosoft.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 "MeasureStr ing" 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 MeasureLabelHei ght(string text, Font font, int maxWid)
{
Form form1 = new Form(); // Necessary to access
'CreateGraphics ' below
Graphics g = form1.CreateGra phics(); // 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.SetMeasu rableCharacterR anges(ranges);

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

return (int)(rect.Bott om + 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*****@discus sions.microsoft .com> wrote in message
news:90******** *************** ***********@mic rosoft.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 "MeasureStr ing" 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 "MeasureStr ing" 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(s tring text, Font font, SizeF
layoutAread, StringFormat format)?

Try using the following code:

Label l = ...;
SizeF area = new SizeF(
l.ClientRectang le.Width, float.PositiveI nfinity);
/* l.Format, but Label does not expose Format */
StringFormat format = StringFormat.Ge nericTypographi c;

SizeF needed = graphics.Measur eString(
l.Text, l.font, area, format);
int borders = l.Height - l.ClientRectang le.Height;
l.Height = (int)(Math.Ceil ing(needed.Heig ht) + 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
2102
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 dimensions, and if I show the new image before setting its dimensions, the new image is stretched first to the old image dimension before it is adjusted to its own dimension. I would like to load a new image with its own dimension at the same time....
24
2793
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> But I am curious about what techniques those of you who have done higher volume access implementations use to ensure high performance of the database in a multi-user 100mbps LAN implementation??? Thanks
6
9899
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
6002
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 TIFF to several PNG files this causes a problem, becuase the resulting image is (the page to the far left and a lot of black space surrounding it and a filesize that is larger than needed. Any ideas?
0
1424
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 determing in the subreport the parameter name it is linked to in this case @customerId is linked to @idCustomer using code. I know there is a subreports link collection but unsure how to use it. Any ideas/code samples
2
2117
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
2515
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 thumbnails and the enlarged image are set via variables. To work properly, all the original images must be the same dimensions which means a great deal of editing and cropping before the images can be posted. And portrait images sometimes cannot be cropped...
1
2463
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 ways to "style" a DIV, it should be noted that a CSS file is used in this case. The same is required for an <IMG... element contained within the DIV.
2
1482
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
1249
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 memory allocator algorithm. You can get some more information on it by reading that particular thread. Anyway, here is a link to where I am going to stick all of the example code: http://appcore.home.comcast.net/vzoom/malloc/ (the allocators home...
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10311
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.