How to truncate text with ellipsis (…) in flex

actionscript-3airapache-flex

In my flex app, I have a <mx:Text> control with a fixed height and width enough to show two lines. Now if the text is too long to be shown in two lines, I would like to have it truncated with showing ellipsis (…). The default truncation with ellipsis seems to be present with label, but label cannot show text in two lines.

How do I mimic this behavior in <mx:Text> control in flex? Thanks in advance!!!

Best Answer

The spark.components.Label component inherits the maxDisplayedLines property from spark.components.supportClasses.TextBase. Here is the help for that particular property:

An integer which determines whether, and where, the text gets truncated.

Truncating text means replacing excess text with a truncation indicator such as "...". The truncation indicator is locale-dependent; it is specified by the "truncationIndicator" resource in the "core" resource bundle.

If the value is 0, no truncation occurs. Instead, the text will simply be clipped if it doesn't fit within the component's bounds.

If the value is is a positive integer, the text will be truncated if necessary to reduce the number of lines to this integer.

If the value is -1, the text will be truncated to display as many lines as will completely fit within the height of the component.

Truncation is only performed if the lineBreak style is "toFit"; the value of this property is ignored if lineBreak is "explicit".

The default value is 0.

From this we can see that if you set the maxDisplayedLines property to -1, the component will display as much text as it can, and append the "..." if it had to truncate the text.