Results 1 to 3 of 3

Thread: [DHTML] TextFormatter

  1. #1
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default [DHTML] TextFormatter

    1) CODE TITLE: TextFormatter

    2) AUTHOR NAME/NOTES: Thomas Eding

    3) DESCRIPTION: A simple class to help format strings within an element

    4) URL TO CODE: http://trinithis.awardspace.com/Text...xtFormatter.js

    Tested in IE6 and Fx2

    Demo: http://trinithis.awardspace.com/Text...Formatter.html

    How to use:
    Code:
    var tf = new TextFormatter(document.getElementById("theElement"));
    tf.print("This");
    tf.println(" is");
    tf.setFontFamily("monospace");
    tf.println("a test!");
    tf.setWidth(15);
    tf.setWidthAlign("left");
    tf.print("x").print("y").println("f(x,y)");    //you can chain any of TextFormatter's methods like this
    tf.print(3.6).print(4.9).println(4.51);
    tf.setWidth();    //sets the width to normal if argument is <1 or absent
    There are more methods than shown. Just look at the source code and look at the method names. Should be fairly obvious.

    NOTE: The methods format(), formatNumber(), and newWrapper() are intended to be used indirectly through other methods.
    Last edited by Trinithis; 09-14-2007 at 10:45 PM.
    Trinithis

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ahh, that's pretty handy. I like it -- good work I would suggest some way of avoiding doing newWrapper() after every modification, though -- it really only needs to be done if text has been output since the last newWrapper().
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    EDIT: Changed newWrapper() to check for a wrapperUsed flag, and I added a setNumberFormat(f, n) method.

    setNumberFormat(f, n) only applies to print(num) or println(num) iff num is a number primitive or a Number object. Aka: print(""+num) won't work.

    It accepts for f "none", "exponential", "fixed", "precision", "radix", or nothing at all, which defaults to "none". The n argument is an optional number that relates to f.

    See http://developer.mozilla.org/en/docs...Number#Methods for details. ("radix" corresponds to the toString() method.)

    If anyone would like to submit any features I could add, I'll do it. (Unless of course it's something like a full-fledged version of http://java.sun.com/javase/6/docs/ap...er.html#syntax )
    Last edited by Trinithis; 09-14-2007 at 10:49 PM.
    Trinithis

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •