Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. MeasureString and HTML

MeasureString and HTML

Scheduled Pinned Locked Moved C#
graphicshtmlquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mr Rogers
    wrote on last edited by
    #1

    I'm trying to determine the dimensions of a string as it will appear in HTML. The closest way I've been able to do this is to use a graphics object and use MeasureString. However, there seems to be a slight difference between GDI and HTML with the dimension of strings. Does anybody know how I can reliably measure the string? Thanks in advance.

    D 1 Reply Last reply
    0
    • M Mr Rogers

      I'm trying to determine the dimensions of a string as it will appear in HTML. The closest way I've been able to do this is to use a graphics object and use MeasureString. However, there seems to be a slight difference between GDI and HTML with the dimension of strings. Does anybody know how I can reliably measure the string? Thanks in advance.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You can't. The browser will display the string in whatever font was assigned to it client-side. There is no way for the server to know in what font, pitch, and browser Text Size the string will be rendered in. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      M 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You can't. The browser will display the string in whatever font was assigned to it client-side. There is no way for the server to know in what font, pitch, and browser Text Size the string will be rendered in. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        M Offline
        M Offline
        Mr Rogers
        wrote on last edited by
        #3

        Maybe I should clarify a bit more then. I want to be able to predict the flow of text that I'm placing into HTML. I'm applying styles to the text using CSS. For example, I know that it's going to be Arial 12 px. So why shouldn't I be able to determine the way that say IE or Firefox are going to render it?

        D S 2 Replies Last reply
        0
        • M Mr Rogers

          Maybe I should clarify a bit more then. I want to be able to predict the flow of text that I'm placing into HTML. I'm applying styles to the text using CSS. For example, I know that it's going to be Arial 12 px. So why shouldn't I be able to determine the way that say IE or Firefox are going to render it?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Because there is no way to garantee that the workstation has Arial 12 pt on it. Also, the users can just click the View menu and change the Text Size, regardless of the size you think they're going to see. Also, last I checked (admittedly, a long time ago), Mac's don't have an Arial font, but they do have web browsers. Also, the size of the browser window will change the rendered flow of the document. Not everybody runs their browsers maximized at 1280x1024. At 1920x1440, you can fit a lot of text on a single line using Arial 12pt. You cannot determine this information server-side. If your rendering is that picky, you'll have to handle rendering in Java client-side. At least there, you've got more information about the environment you're working in. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          M 1 Reply Last reply
          0
          • M Mr Rogers

            Maybe I should clarify a bit more then. I want to be able to predict the flow of text that I'm placing into HTML. I'm applying styles to the text using CSS. For example, I know that it's going to be Arial 12 px. So why shouldn't I be able to determine the way that say IE or Firefox are going to render it?

            S Offline
            S Offline
            Scott Serl
            wrote on last edited by
            #5

            You might get slightly better results if you specify your font size in points; when you use pixels, you don't know how the browser is going to interpret that. Also, you have the problem of how the ie box model differs from every other browser, so the actual size of the area where text is rendered might be different in each browser depending on your settings for margin, padding, and borders.

            M 1 Reply Last reply
            0
            • D Dave Kreskowiak

              Because there is no way to garantee that the workstation has Arial 12 pt on it. Also, the users can just click the View menu and change the Text Size, regardless of the size you think they're going to see. Also, last I checked (admittedly, a long time ago), Mac's don't have an Arial font, but they do have web browsers. Also, the size of the browser window will change the rendered flow of the document. Not everybody runs their browsers maximized at 1280x1024. At 1920x1440, you can fit a lot of text on a single line using Arial 12pt. You cannot determine this information server-side. If your rendering is that picky, you'll have to handle rendering in Java client-side. At least there, you've got more information about the environment you're working in. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              M Offline
              M Offline
              Mr Rogers
              wrote on last edited by
              #6

              If I specify the font size using css the user is unable to change the font size and I'm not so sure about Macs not having Arial but that's besides the point. Also, I don't care how big the user has their browser because I just want to know the dimentions of a given set of text. I will be the one locking in the size of the text area using HTML. I simply can't believe that there is no way of determining that out without using the HTML DOM. Still, thanks for your input.

              1 Reply Last reply
              0
              • S Scott Serl

                You might get slightly better results if you specify your font size in points; when you use pixels, you don't know how the browser is going to interpret that. Also, you have the problem of how the ie box model differs from every other browser, so the actual size of the area where text is rendered might be different in each browser depending on your settings for margin, padding, and borders.

                M Offline
                M Offline
                Mr Rogers
                wrote on last edited by
                #7

                I've found so far that Firefox and IE are fairly similar when using gdi to determine the size of fonts. I'll have to check out my margin and whatnot to see if that's where I'm getting my problem from.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups