How to count number of characters in a line using JavaScript

前端 未结 3 1428
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 13:14

Suppose I have a div with width 200px and font-size 16. Now I want to calculate number of characters that can be fit into a line of div. How can I calculate number of charac

3条回答
  •  醉梦人生
    2020-12-06 13:56

    You can only calculate the numbers of characters per line if the width of each character is the same. This is true for all fixed-width fonts like the one in the question editor of stackoverflow.

    If you ensured using a fixed-width font, you can calculate the width of a single character, e.g. using the function posted in this answer. Then, simply divide the width of the

    through the width of a character.

提交回复
热议问题