tags:

views:

26

answers:

2

So I've searched a ton of sites, and what I am looking for is, as the user types, the text automatically resizezs smaller (or larger, if they delete something).

For an example, as well as the other code I'm using, you can visit http://twmorton.com/magicpreviewjquery/

Thanks!

A: 

Just put an event on .keydown() which will check the length of .text() inside the element and resize it accordingly

cheshire
+1  A: 

You could try some simple algorithm like this:

$(function() {
    var $in = $("#in"),                                     // <== could be a div
        w = $in.width();
    $in.css("font-size", 20*30/$in.text().length);
    $in.click(function() {
        this.contentEditable='true';
    });
    $(document).keyup(function() {

        $in.css("font-size", 20*30/$in.text().length);       // <== Resizing algo

    });
});​

Try it out with this jsFiddle

Peter Ajtai
This works exactly the way I need it to, but it is not contained in my DIV. Is there any special CSS I would have to use? You can see my demo here: http://twmorton.com/magicpreviewjquery/
Tom
The problem is that when you hit the first letter the letter is gigantic. I would make an upper limit using Math.min(). I'd also change the CSS a little, but it looks like it's basically working. Take a look at this ==> http://jsfiddle.net/bwTVR/ ----------- Vertical align is tricky. Read this ==> http://phrogz.net/CSS/vertical-align/index.html --------- You might also have to play around with the resizing algorithm.
Peter Ajtai
Your JSFiddle link works perfectly, until I try to apply my Cufon font to it. Then things get out of hand. http://twmorton.com/magicpreviewjquery/ and ideas on that? I want the user to be able to enter their name and it would type out in the cufon font
Tom
@Tom - Looks like it works. I see the font size change. So what you have to do is adjust the initial font size and rate of font size change so that it fits into the box. Just use a smaller initial font.
Peter Ajtai