dynamically determine the largest possible font size

A

ara.t.howard

hi all-

i'm a totally js hack so go easy on me...

i'd like to create a function that, given the size of a block of text
in

x = maximum number of chars
y = total number of lines (this is in chars too of course)

could determine and set the font size to be close, say 90 percent, to
the maximum that will display in the users browser without scrolling.
if you know what lightning slides are you'll understand what i'm
doing.

for example, given

foo
bar
foobar

x = 6
y = 3

i am trying to set the font size such that this text would __just__
fit onto the screen without scrolling in horizontal or vertical
directions.

i've got some code that's close, but i'm hoping someone here might
have a more elegant way.

kind regards

-a
 
M

marss

hi all-

i'm a totally js hack so go easy on me...

i'd like to create a function that, given the size of a block of text
in

x = maximum number of chars
y = total number of lines (this is in chars too of course)

could determine and set the font size to be close, say 90 percent, to
the maximum that will display in the users browser without scrolling.
if you know what lightning slides are you'll understand what i'm
doing.

for example, given

foo
bar
foobar

x = 6
y = 3

i am trying to set the font size such that this text would __just__
fit onto the screen without scrolling in horizontal or vertical
directions.

Hi,
You can try to define max font size for the container by augmentation
in series until the container size exceeds the document body size. For
example, code snippet(untested):

function getFontSize()
{
var s = document.getElementById("container");
var fontSize = 10;
while(true)
{
s.style.fontSize = fontSize;
if (s.offsetWidth > document.body.offsetWidth || s.offsetHeight
document.body.offsetHeight)
{
fontSize--;
alert("Max font size = "+fontSize);
break;
}
fontSize++;
}
}

It is simple an idea.
i've got some code that's close, but i'm hoping someone here might
have a more elegant way.

You didn't post the code you have, so I could not compare elegancy. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top