Package org.opencms.gwt.client.util
Class CmsTextMetrics
java.lang.Object
org.opencms.gwt.client.util.CmsTextMetrics
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 TypeMethodDescriptionprotected 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.static CmsTextMetrics
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
release()
Should be called, when finished measuring, to release the playground.void
setFixedWidth
(int width) Sets a fixed width on the internal measurement element.
-
Method Details
-
get
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 stylekey
- the text metrics key- Returns:
- a text metrics object
-
getHeight
Returns the measured height of the specified text. For multiline text, be sure to callsetFixedWidth(int)
if necessary.- Parameters:
text
- the text to be measured- Returns:
- the height in pixels
-
getWidth
Returns the measured width of the specified text.- Parameters:
text
- the text to measure- Returns:
- the width in pixels
-
release
Should be called, when finished measuring, to release the playground. -
setFixedWidth
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
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
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 elementattributes
- the attributes to bind
-