Class TextMetrics


  • public class TextMetrics
    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.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void bind​(com.google.gwt.dom.client.Element el)
      Binds this TextMetrics instance to an element from which to copy existing CSS styles that can affect the size of the rendered text.
      void bind​(XElement el)
      Binds this TextMetrics instance to an element from which to copy existing CSS styles that can affect the size of the rendered text.
      void bind​(String className)
      Binds the TextMetrics instance using the styles from the given class name.
      static TextMetrics get()
      Returns the singleton instance.
      int getHeight​(com.google.gwt.safehtml.shared.SafeHtml html)
      Returns the measured height of the specified html.
      int getHeight​(String text)
      Returns the measured height of the specified text.
      Size getSize​(com.google.gwt.safehtml.shared.SafeHtml html)
      Returns the size of the specified text based on the internal element's style and width properties.
      Size getSize​(String text)
      Returns the size of the specified text based on the internal element's style and width properties.
      int getWidth​(com.google.gwt.safehtml.shared.SafeHtml html)
      Returns the measured width of the specified html.
      int getWidth​(String text)
      Returns the measured width of the specified text.
      void setFixedWidth​(int width)
      Sets a fixed width on the internal measurement element.
    • Method Detail

      • get

        public static TextMetrics get()
        Returns the singleton instance.
        Returns:
        the text metrics instance
      • bind

        public void bind​(com.google.gwt.dom.client.Element el)
        Binds this TextMetrics instance to an element from which to copy existing CSS styles that can affect the size of the rendered text.
        Parameters:
        el - the element
      • bind

        public void bind​(String className)
        Binds the TextMetrics instance using the styles from the given class name.
        Parameters:
        className - the class name
      • bind

        public void bind​(XElement el)
        Binds this TextMetrics instance to an element from which to copy existing CSS styles that can affect the size of the rendered text.
        Parameters:
        el - the element
      • 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
      • getHeight

        public int getHeight​(com.google.gwt.safehtml.shared.SafeHtml html)
        Returns the measured height of the specified html. For multiline text, be sure to call setFixedWidth(int) if necessary.
        Parameters:
        html - the text to be measured
        Returns:
        the height in pixels
      • getSize

        public Size getSize​(String text)
        Returns the size of the specified text based on the internal element's style and width properties.
        Parameters:
        text - the text to measure
        Returns:
        the size
      • getSize

        public Size getSize​(com.google.gwt.safehtml.shared.SafeHtml html)
        Returns the size of the specified text based on the internal element's style and width properties.
        Parameters:
        html - the html or text to measure
        Returns:
        the size
      • 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
      • getWidth

        public int getWidth​(com.google.gwt.safehtml.shared.SafeHtml html)
        Returns the measured width of the specified html.
        Parameters:
        html - the text to measure
        Returns:
        the width in pixels
      • 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