Class CmsTextMetrics

java.lang.Object
org.opencms.gwt.client.util.CmsTextMetrics

public final class CmsTextMetrics extends Object
Provides precise pixel measurements for blocks of text so that you can determine exactly how high and wide, in pixels, a given block of text will be.

Normal usage would be:

 for(Element e: elements) {
   CmsTextMetrics tm = CmsTextMetrics.get(e, "TextMetricsKey");

   // measure text
   if (r.getWidth(text) > 500) {
      // do something
   }
   // release
   tm.release();
 }
 
Based on my-gwt TextMetrics.

Since:
8.0.0
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    bind(com.google.gwt.dom.client.Element element)
    Binds this text metrics instance to an element from which to copy existing CSS styles that can affect the size of the rendered text.
    protected void
    bind(com.google.gwt.dom.client.Element element, CmsDomUtil.Style... attributes)
    Binds this text metrics instance to an element from which to copy existing CSS styles that can affect the size of the rendered text.
    get(com.google.gwt.dom.client.Element element, String key)
    Gets the text metrics object for a given DOM element and key.
    int
    Returns the measured height of the specified text.
    int
    Returns the measured width of the specified text.
    void
    Should be called, when finished measuring, to release the playground.
    void
    setFixedWidth(int width)
    Sets a fixed width on the internal measurement element.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • get

      public static CmsTextMetrics get(com.google.gwt.dom.client.Element element, String key)
      Gets the text metrics object for a given DOM element and key.

      If the key is null, or the method has been never called with the same key before, a new text metrics object will be created, with its style taken from the element parameter. Otherwise, the text metrics object for the given key will be returned, and the element parameter will be ignored.

      Parameters:
      element - the element from which to take the style
      key - the text metrics key
      Returns:
      a text metrics object
    • getHeight

      public int getHeight(String text)
      Returns the measured height of the specified text. For multiline text, be sure to call setFixedWidth(int) if necessary.

      Parameters:
      text - the text to be measured
      Returns:
      the height in pixels
    • getWidth

      public int getWidth(String text)
      Returns the measured width of the specified text.

      Parameters:
      text - the text to measure
      Returns:
      the width in pixels
    • release

      public void release()
      Should be called, when finished measuring, to release the playground.

    • setFixedWidth

      public void setFixedWidth(int width)
      Sets a fixed width on the internal measurement element. If the text will be multiline, you have to set a fixed width in order to accurately measure the text height.

      Parameters:
      width - the width to set on the element
    • bind

      protected void bind(com.google.gwt.dom.client.Element element)
      Binds this text metrics instance to an element from which to copy existing CSS styles that can affect the size of the rendered text.

      Parameters:
      element - the element
    • bind

      protected void bind(com.google.gwt.dom.client.Element element, CmsDomUtil.Style... attributes)
      Binds this text metrics instance to an element from which to copy existing CSS styles that can affect the size of the rendered text.

      Parameters:
      element - the element
      attributes - the attributes to bind