Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calculating string width in pixels

    How would you go about calculating the width (in pixels) of a string of text, when the text could be any mix of font families, sizes or styles? The TextRange object in IE exposes a boundingWidth property that could prove useful, but as far as I can tell the TextRange object is not supported in FF or Safari and I need a cross browser solution. Any insight would be greatly appreciated. Thanks in advance.

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if you could give a little more background into what you need this for maybe someone can suggest a method that works?

    *ie, if it is text within a span tag i can remember something about a element.scaleWidth ???
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #3
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Luckily, I discovered that I can simply copy a text string into a hidden span element and then use the span element's offsetWidth property to get the width of the string in pixles, which is exactly what I needed. Problem solved!

  • #4
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    offsetWidth........ dam, so close!
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<


  •  

    Posting Permissions

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