469,301 Members | 2,278 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

C# Font Scaling in a fixed width???

Hi, I was wondering if anyone could help me. Iím trying to add text into a fixed width. Say label1.MaximumSize = new Size(0, 30);
label1.text=textbox1.text
When the label gets full I want the text to start squeezing together and not just cut off. I keep playing with string formats ECT. But just canít seem to figure it out. Is there a way to have a label or drawstring perform this kind of function???
Oct 30 '08 #1
11 9782
tlhintoq
3,525 Expert 2GB
Hi, I was wondering if anyone could help me. Iím trying to add text into a fixed width. Say label1.MaximumSize = new Size(0, 30);
label1.text=textbox1.text
When the label gets full I want the text to start squeezing together and not just cut off. I keep playing with string formats ECT. But just canít seem to figure it out. Is there a way to have a label or drawstring perform this kind of function???
Here's some code from one of my functions that puts a text tag onto a jpg graphic. It works a little different from your need, as I extend the label to space required by all the text at a fixed size. You want to reduce the font to match a fixed space.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Font myFont = new Font("Arial", LableSize); // LableSize is the font size                    
  3. Graphics LGraphic = Graphics.FromImage(myLable);//myLable is an Image obj                 
  4. PaintEventArgs e = new PaintEventArgs(LGraphic, FullSize);//Fullsize is the size of the total graphic so I know how much space I have to work with later on                 
  5. SizeF oSize1 = e.Graphics.MeasureString(szLable, myFont);
  6.  
I'm thinking that you should be able to reverse engineer a loop from this that takes your fixed space, and a starts with a font size of 1, and keeps increasing the size until your text no longer fits, then drop back down to the size that worked and use that to display your text. Maybe something involving a check like:
Expand|Select|Wrap|Line Numbers
  1. int FontSizeToUse = 1;
  2. for (int Loop = 1; Loop < 25; Loop++)
  3. {
  4.      if (oSize1.Width > myTextBox.Width) 
  5.      {
  6.           FontSizeToUse = Loop - 1;
  7.           break;
  8.      }
  9. }
  10. // Use the FontSizeToUse for your lable
  11.  
Oct 31 '08 #2
Hi, thanks for the reply. Im using somthing like that now.
Expand|Select|Wrap|Line Numbers
  1. int FontSizeCount = 0;
  2. private void textBox1_TextChanged(object sender, EventArgs e)
  3.         {
  4.  
  5.             label1.MaximumSize = new Size(0, 30);
  6.             if (label1.Width > 232)
  7.             {
  8.                 label1.Font = ChangeFontSize(label1.Font, label1.Font.Size - 2);
  9.                 label1.Location = new Point(28, FontSizeCount + 33);
  10.                 label1.Refresh();
  11.                 FontSizeCount++;
  12.             }
  13.             if ( label1.Width < 219 && FontSizeCount > 0)
  14.             {
  15.                label1.Font = ChangeFontSize(label1.Font, label1.Font.Size + 2);
  16.                label1.Location = new Point(28, 33 - FontSizeCount);
  17.                label1.Refresh();
  18.                FontSizeCount--;
  19.             }
  20.  
  21.             label1.Text = textbox1.Text;
  22.  
  23.  
  24.         }
  25.  
  26.         static public Font ChangeFontSize(Font font, float fontSize)
  27.         {
  28.             if (font != null)
  29.             {
  30.                 float currentSize = font.Size;
  31.                 if (currentSize != fontSize)
  32.                 {
  33.                     font = new Font(font.Name, fontSize, font.Style, font.Unit, font.GdiCharSet, font.GdiVerticalFont);
  34.                 }
  35.             }
  36.             return font;
  37.         }
  38.  
And it is working it decreasing and increasing the size of the fonts, but I need it to not make the fonts smaller, but to squeeze them together. I found something like what I need it to do, if you got http://www.yugiohcardmaker.net and add a text to the name it writes on the card, after it reaches its max point it starts to squeeze the text together. Iím not sure how I can do it, or if I need to think of another way...
Oct 31 '08 #3
tlhintoq
3,525 Expert 2GB
Hi, thanks for the reply. Im using somthing like that now.
Expand|Select|Wrap|Line Numbers
  1. int FontSizeCount = 0;
  2. private void textBox1_TextChanged(object sender, EventArgs e)
  3.         {
  4.  
  5.             label1.MaximumSize = new Size(0, 30);
  6.             if (label1.Width > 232)
  7.             {
  8.                 label1.Font = ChangeFontSize(label1.Font, label1.Font.Size - 2);
  9.                 label1.Location = new Point(28, FontSizeCount + 33);
  10.                 label1.Refresh();
  11.                 FontSizeCount++;
  12.             }
  13.             if ( label1.Width < 219 && FontSizeCount > 0)
  14.             {
  15.                label1.Font = ChangeFontSize(label1.Font, label1.Font.Size + 2);
  16.                label1.Location = new Point(28, 33 - FontSizeCount);
  17.                label1.Refresh();
  18.                FontSizeCount--;
  19.             }
  20.  
  21.             label1.Text = textbox1.Text;
  22.  
  23.  
  24.         }
  25.  
  26.         static public Font ChangeFontSize(Font font, float fontSize)
  27.         {
  28.             if (font != null)
  29.             {
  30.                 float currentSize = font.Size;
  31.                 if (currentSize != fontSize)
  32.                 {
  33.                     font = new Font(font.Name, fontSize, font.Style, font.Unit, font.GdiCharSet, font.GdiVerticalFont);
  34.                 }
  35.             }
  36.             return font;
  37.         }
  38.  
And it is working it decreasing and increasing the size of the fonts, but I need it to not make the fonts smaller, but to squeeze them together. I found something like what I need it to do, if you got http://www.yugiohcardmaker.net and add a text to the name it writes on the card, after it reaches its max point it starts to squeeze the text together. Iím not sure how I can do it, or if I need to think of another way...
I don't think there is a way of affecting spacing or leading of a label. The control simply doens't have that much power.
Had you considered making a graphic of your text and placing that on the form? Then you can easily change the width of the graphic to space you need, resulting in skinnier letters.
Oct 31 '08 #4
Thanx tlhintoq, that would be fine, im still not sure how to do it.. do you mean somthing like.
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.                         bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  4.                         using (Graphics g = Graphics.FromImage(bm))
  5. {
  6.  g.DrawString(textBox1.Text, new Font(CardDescFontName, CardDescFontSize), Brushes.Black, new RectangleF(new PointF(30, 380), new SizeF(288, 90)));
  7.  
  8. }
  9.  
do you have an example of how I can do it??? thanx again
Oct 31 '08 #5
Still No Luck... I have looked all over Google, just cannot seem to get it. Can anyone please show me an example of how to draw text and have the letters squeezed together instead of cutting off, if they wonít fit in a fixed width area???
Nov 1 '08 #6
tlhintoq
3,525 Expert 2GB
Thanx tlhintoq, that would be fine, im still not sure how to do it.. do you mean somthing like.
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.                         bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  4.                         using (Graphics g = Graphics.FromImage(bm))
  5. {
  6.  g.DrawString(textBox1.Text, new Font(CardDescFontName, CardDescFontSize), Brushes.Black, new RectangleF(new PointF(30, 380), new SizeF(288, 90)));
  7.  
  8. }
  9.  
do you have an example of how I can do it??? thanx again
You're definately on the the right track. Use the .DrawString to draw your text on to a graphic. From their scale the graphic to match your fixed-size space.

I was recently reminded of user controls from another post. This might be a good use for such a thing. A custom user control that takes a text string and displays it as a graphic. Maybe a couple properties to set for which font to use and so forth. That way once you work out all the behavior you can easily drop it on other forms.
Nov 1 '08 #7
Thank you, i will try playing around with that...
Nov 1 '08 #8
Hi again, can someone look at this and tell me what Iím doing wrong. Maybe Iím just brain fried from trying to figure this out for 4 days. Iím new to c# and learning as I go. I can now get the image to stretch but I canít get it to do the opposite and squeeze the text together...

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Drawing.Drawing2D;
  9. using System.Drawing.Text;
  10. using System.Windows.Forms;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         Bitmap bm;
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void button1_Click(object sender, EventArgs e)
  23.         {
  24.                 bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  25.                 using (Graphics g = Graphics.FromImage(bm))
  26.                 {
  27.                     pictureBox1.Refresh();
  28.                     g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  29.                     g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 36), Brushes.Black, new RectangleF(new PointF(0, 0), new SizeF(275, 70)));
  30.  
  31.                     g.ScaleTransform(2f,1f);
  32.                     g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 36), Brushes.Black, new RectangleF(new PointF(10, 0), new SizeF(275, 70)));
  33.                 }
  34.  
  35.  
  36.                 using (Graphics h = pictureBox1.CreateGraphics())
  37.                 {
  38.                     h.DrawImageUnscaled(bm, 0, 0);
  39.                 }
  40.         }
  41.         private void Form1_Load(object sender, EventArgs e)
  42.         {
  43.         }
  44.     }
  45. }
  46.  
Nov 2 '08 #9
tlhintoq
3,525 Expert 2GB
Hi again, can someone look at this and tell me what Iím doing wrong. Maybe Iím just brain fried from trying to figure this out for 4 days. Iím new to c# and learning as I go. I can now get the image to stretch but I canít get it to do the opposite and squeeze the text together...

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Drawing.Drawing2D;
  9. using System.Drawing.Text;
  10. using System.Windows.Forms;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         Bitmap bm;
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void button1_Click(object sender, EventArgs e)
  23.         {
  24.                 bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  25.                 using (Graphics g = Graphics.FromImage(bm))
  26.                 {
  27.                     pictureBox1.Refresh();
  28.                     g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  29.                     g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 36), Brushes.Black, new RectangleF(new PointF(0, 0), new SizeF(275, 70)));
  30.  
  31.                     g.ScaleTransform(2f,1f);
  32.                     g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 36), Brushes.Black, new RectangleF(new PointF(10, 0), new SizeF(275, 70)));
  33.                 }
  34.  
  35.  
  36.                 using (Graphics h = pictureBox1.CreateGraphics())
  37.                 {
  38.                     h.DrawImageUnscaled(bm, 0, 0);
  39.                 }
  40.         }
  41.         private void Form1_Load(object sender, EventArgs e)
  42.         {
  43.         }
  44.     }
  45. }
  46.  
I think you can just set the SizeMode property of the PictureBox at design time to either StretchImage or AutoSize (whichever gives you the results you like the look of), then change line 36. Notice you are specifically telling it to draw unscaled.
Instead just set the image property of the PictureBox to your newly created image.
Expand|Select|Wrap|Line Numbers
  1. PictureBox1.Image = MyNewlyCreatedImageObject;
Nov 2 '08 #10
Hi, again.. first let me say thanx to tlhintoq for the help I have got so far. well I can now get the image to stretch but I canít get it to do the opposite and squeeze the text together... anyone have any ideas or am I doing wrong.




Expand|Select|Wrap|Line Numbers
  1. Bitmap bm;
  2.  
  3. private void button1_Click(object sender, EventArgs e)
  4. {
  5. bm = new Bitmap(pictureBox1.Width , pictureBox1.Height);
  6. using (Graphics g = Graphics.FromImage(bm))
  7. pictureBox1.Refresh();
  8. g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  9. g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 18), Brushes.Black, new RectangleF(new PointF(0, 0), new SizeF(275, 70)));
  10. g.ScaleTransform(3f,1f);
  11. g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 36), Brushes.Black, new RectangleF(new PointF(10, 0), new SizeF(275, 70)));
  12. }
  13.  
  14. using (Graphics h = pictureBox1.CreateGraphics())
  15. {
  16. h.DrawImage(bm,0,0);
  17. }
  18. }
  19.  
  20.  
Nov 4 '08 #11
OK.. I Got It.. Some one over at msdn was able to figure it out.. Im going to Post it here incase anyone here needs somthing like this..

Expand|Select|Wrap|Line Numbers
  1. bm = new Bitmap(pictureBox1.Width, pictureBox1.Height);   
  2.             using (Graphics g = Graphics.FromImage(bm))   
  3.             {   
  4.                 pictureBox1.Refresh();   
  5.                 SizeF sz = g.MeasureString(textBox1.Text, new Font("Stone Serif ITC TT", 18));   
  6.                 float sf = (pictureBox1.Width / sz.Width < pictureBox1.Height / sz.Height) ? pictureBox1.Width / sz.Width : pictureBox1.Height / sz.Height;   
  7.                 g.ScaleTransform(sf,sf);   
  8.                 g.DrawString(textBox1.Text, new Font("Stone Serif ITC TT", 18), Brushes.Black, new PointF(0, 0), new StringFormat(StringFormatFlags.NoWrap | StringFormatFlags.NoClip));   
  9.             }   
  10.  
  11.             using (Graphics h = pictureBox1.CreateGraphics())   
  12.             {   
  13.                 h.DrawImage(bm, 0, 0);   
  14.             }  
  15.  
  16.  
Thanks for all the help..
Nov 5 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Csaba2000 | last post: by
4 posts views Thread by Michel Joly de Lotbiniere | last post: by
4 posts views Thread by Eric Eggermann | last post: by
9 posts views Thread by Adam | last post: by
4 posts views Thread by Paul Gorodyansky | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.