开发者

Dynamically resize/space text to fit div

开发者 https://www.devze.com 2023-03-08 15:21 出处:网络
I have a div with a fixed width, but the text inside (someones name) will vary. Is there a way of dynamically resizing/letter-spac开发者_JAVA技巧ing the text to fit perfectly in the div?

I have a div with a fixed width, but the text inside (someones name) will vary. Is there a way of dynamically resizing/letter-spac开发者_JAVA技巧ing the text to fit perfectly in the div?

However, I cannot use javascript as this script will be used in a HTML-PDF converter, which does not read javascript

text-align:justify won't work as if the text is too long for the div, it won't resize it. I find text-align:justify only works for paragraphs etc.

The name cannot go onto two lines


You need to use either:

 Graphics graphics = this.CreateGraphics();
 SizeF textSize = graphics.MeasureString("How long am I?", this.Font);

or

 Size textSize = TextRenderer.MeasureText("How long am I?", font);

TextRenderer is less accurate, but Graphics requires you to use a windows form - in your case you could have a form with a single textbox into which you place the text to be measured and then read it back, but TextRenderer is simpler.

Using the above you could write a function that adjusted the font-size until the desired length was reached.

It would then be a case of setting the style on the text sent to the browser to reflect this font-size.

0

精彩评论

暂无评论...
验证码 换一张
取 消