/ LJCJSCodeDoc / LJCCommonLib / LJC / AverageCharWidth


Parameters
font - A CSS font object.
text - A string value.

Returns

The average character width.

Syntax

JavaScript
static AverageCharWidth(font, text)

Gets the average character width for the supplied font and text.

Example

JavaScript
<body>
  <!-- The dialog for debug or other display text. -->
  <dialog id="textDialog"
    style="border: none; padding: 0px">
    <textarea id="textArea" rows="25" cols="80" autocorrect="off"
      autocapitalize="off" spellcheck="false" readonly
      style="border: none; padding: 10px">
    </textarea>
  </dialog>
</body>

// Gets the average character width using the first selector element font.
AverageCharWidth()
{
  const selector = "div";
  let font = LJC.SelectorStyle(selector, "font");
  const text = "This is sample text.";
  let result = LJC.AverageCharWidth(font, text);

  // result;
  // With font = "16px Times New Roman"
  // 6.22;

  const textAreaID = "textArea";
  const eTextArea = LJC.Element(textAreaID);
  font = LJC.ElementStyle(eTextArea, "font");
  result = LJC.AverageCharWidth(font, text);

  // result:
  // With font = "13.3333px monospace"
  // 7.33;
}

Copyright © Lester J. Clark and Contributors.
Licensed under the MIT License.