0

I'm trying to fit some dynamic text into a defined box on my Wordpress site. I've investigated using the following two scripts:

With both I'm getting the same results which is the text coming into the boxes (a good start) but the text does not wrap nor does it seem to scale according to the available size as indicated in the stylesheet.

The text is a link inside a div. Here is the code for the div in the index.php file of Wordpress:

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div id="id<?php echo get_post_meta($post->ID, "blipclass", true); ?>" class="<?php echo get_post_meta($post->ID, "blipclass", true); echo " "?>blip storycontent frontp news-post">
        <a href="<?php echo the_permalink(); ?>" class="bliptext"><?php echo get_post_meta($post->ID, "blip", true); ?></a>
        <script type="text/javascript">fitTextInBox('id<?php echo get_post_meta($post->ID, "blipclass", true); ?>',136);</script>
    </div>
</div>

and relevant parts of my stylesheet

#idblip-c, #idblip-m, #idblip-y, #idblip-k, {
        width: 218px !important;
        height: 136px !important;
        overflow: auto !important;
}

.bliptext {
        padding:10px;
        padding-top:15px;
        width:100%;
        height:100%;
        overflow: auto !important;
}

Basically, I would like to see the front size change based on the available space in the box. The fewer words the bigger the font and vice versa.

Here is where I'm at right now:

as you can see, the font size is maxing out and is not wrapping. I get this far using either script.

Any tips on the matter is greatly appreciated.

some more of the CSS

#idblip-c, #idblip-m, #idblip-y, #idblip-k, {
        width: 218px !important;
        height: 136px !important;
        overflow: auto !important;

HTML output

        <div class="frontpics">
                                                <div id="post-1831" class="post-1831 post type-post status-publish format-standard hentry category-news">
                            <div id="idblip-c" class="blip-c blip storycontent frontp news-post">
                                <a href="story1/" class="bliptext">HE NEVER STOPS, EVER!</a>
                                <script type="text/javascript">fitTextInBox('idblip-c',136);</script>
                            </div>
                        </div>
                                                <div id="post-1649" class="post-1649 post type-post status-publish format-standard hentry category-news">

                            <div id="idblip-m" class="blip-m blip storycontent frontp news-post">
                                <a href="story2/" class="bliptext">FIRST PLACE</a>
                                <script type="text/javascript">fitTextInBox('idblip-m',136);</script>
                            </div>
                        </div>
                                                <div id="post-1826" class="post-1826 post type-post status-publish format-standard hentry category-news tag-caen tag-france tag-klar tag-masterplan">
                            <div id="idblip-y" class="blip-y blip storycontent frontp news-post">
                                <a href="story3/" class="bliptext">MASTER OF THE UNIVERSE</a>

                                <script type="text/javascript">fitTextInBox('idblip-y',136);</script>
                            </div>
                        </div>
                                                <div id="post-1820" class="post-1820 post type-post status-publish format-standard hentry category-news">
                            <div id="idblip-k" class="blip-k blip storycontent frontp news-post">
                                <a href="story4/" class="bliptext">CONTEMPORARY SHT</a>
                                <script type="text/javascript">fitTextInBox('idblip-k',136);</script>

                            </div>
                        </div>
                                        </div>
            </div>
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
CKK2011
  • 1
  • 2
  • The PHP and JavaScript is likely irrelevant. Could you paste a sample of the HTML that is outputted instead? And also some more of the CSS. – melkamo Mar 30 '11 at 16:40

2 Answers2

0

I made a plugin to address this issue:

https://github.com/jeremiahrose/perfectFit.js

It will resize each line separately after wrapping, which seems to be what you are looking for.

Jeremiah Rose
  • 3,865
  • 4
  • 27
  • 31
0

EDIT Looks like someone already made a jQuery plugin for this exact thing: http://plugins.jquery.com/project/TextFill

Not a specific answer, but a general strategy:

Take the id of the box and some default text size. Call a function that detects whether a scrollbar exists on a div. If so, decrease the font size slightly, and repeat. If not, increase the font size slightly, and repeat. At any point you detect that the previous action was to increase in size, and the current action was to decrease in size, you've found the point where the text fits the box. This could easily be done with a simple loop.

If reach some minimum text size and the scroll bar still exists, you can gracefully fail by setting the value to the default text size you originally sent.

You can reduce the number of tests by binary searching in either direction, each time you make a step that changes the outcome (has or doesn't have a scrollbar), you half the distance you change the font-size by in the next step.

How to find out whether a div has a scrollbar: possible solution (haven't tested), solutions on Stack Overflow using Jquery 1, 2, 3

Community
  • 1
  • 1
Dwight
  • 1,537
  • 2
  • 14
  • 17
  • TextFill is what I started out with and struggled to get working, so I tried TextFitinBox javascript from dhtmlgoodies.com to see if I could get any further. With both scripts, however, I came to the same dead end. This is depicted in the imgur screenshot at the bottom of the post. Scratching my head as to why the jQuery plugin TextFill and TextFitinBox end up with the same result of maxed out text and mashing all together on one line. – CKK2011 Mar 30 '11 at 17:18
  • I'm sorry. I didn't realize the library you tried was the same. Without the full code, I can't be sure, but here's what I'm thinking: some of those styles (such as post type-post status-publish format-standard hentry category-news) are interfering with the the styles in question. Further, since you know the jQuery textfill works on their demo, you should take their example, and slowly modify it until you get to the desired output on your site. The only way I know how text gets scrunched like that is an incorrect usage of letter-spacing and or line-height. – Dwight Mar 30 '11 at 18:08
  • Ok, I'm going to focus on the jQuery textfill script from now on. Can you think of any reasons why the text would not drop down to multiple lines, in other words, possible reasons the text refuses to wrap inside the box and span multiple lines if necessary? In my case, everything is pushed up to the top line and scrunched together. I'm going to look at the other styles as well to see if there is something there. Thanks. – CKK2011 Mar 30 '11 at 18:26
  • Something that prevents wrapping? A pre tag, code tag, or anything with `whitespace:nowrap;` Also, a bad line-height could jumble characters into a single line too. – Dwight Mar 30 '11 at 18:30
  • Took your advice and focused on the CSS. Created a separate style for the boxes in question and discovered it was a bad line-height from another style. Thanks for your help! – CKK2011 Apr 01 '11 at 12:15