Algorithm for estimating text width based on contents

前端 未结 8 2160
傲寒
傲寒 2021-02-08 09:11

This is a long shot, but does anyone know of an algorithm for estimating and categorising text width (for a variable width font) based on its contents?

For example, I\'d

8条回答
  •  一个人的身影
    2021-02-08 09:36

    You really have no way of knowing what browser, font settings, screen size etc the client is using. (OK, sometimes the request headers provide an indication, but really nothing consistent or reliable.)

    So, I would:

    • display some sample text in Internet Explorer with default settings on a screen/window size of 1024x768 (this is generally the most common size)
    • take an average characters/line with this configuration and use that for your estimate.

    I've generally found this "good enough", for example, for estimating the number of lines that some text will take up on a browser, in order to estimate how many adverts to show next to the text.

    If it was really really crucial to you, then I can imagine a convoluted scheme whereby you initially send some Javascript to the client, which then takes a measurement and sends it back to your server, and you then use this information for future pages for that client. I can't imagine it's usually worth the effort, though.

提交回复
热议问题