How can I read a spark TextArea implicit line breaks? - apache-flex

I have a TextArea where the user can enter text and also change the width and height of the TextArea. When resizing, the text is breaking where expected. I need to find where the TextArea skin added the implicit line breaks after resizing.
ENVIRONMENT
FlexBuilder 4.6
Flex SDK 4.6.0
Flash Player 11.1
EXAMPLE
This is just plain text that
breaks after the word "that".
Any ideas on how to find the position of the line break when the TextArea lineBreak property is toFit and the text has no CR or LF characters? In the example above, it would be position 28.

You can split up the text into line components and return their individual text lengths pretty easily, but you'll have to use some 'text layout framework' magic.
First access your TextArea's textFlow property (not the text property), which will return a TextFlow instance. This is a model for the text inside your TextArea.
This object has a flowComposer which takes care of the layout of the text and carries a lot of information with it.
Its type is IFlowComposer, but you'll have to cast it to a StandardFlowComposer in order to access the individual lines.
Now you have access to the lines property, which is a collection of TextFlowLine
Each of these lines has a textLength property.
So the following
var composer:StandardFlowComposer =
myTextArea.textFlow.flowComposer as StandardFlowComposer;
for each (var line:TextFlowLine in composer.lines)
trace(line.textLength)
would yield (with your example)
29
30

Related

How do I truncate a Flex StyleableTextField after two lines?

In my Flex 4.5 mobile app, I have an actionScript item renderer (that derives from Flex's LabelItemRenderer). I want to fit in exactly 2 lines of text, and then truncate the rest. The width and height of the label are fixed and known statically.
How can I do this? The StyleableTextField.truncateToFit() method only works for one line of text.
I've set wordWrap = true, so the text now flows into the second line - but I need to truncate the text if it doesn't fit in two lines.
I need it to show all the text if there is only one line. (in both cases the label should be vertically middle-aligned in my renderer)
I know how to override layoutContents to do sizing and positioning etc of the StyleableTextField. So I'm looking specifically for ideas to implement custom text truncation with the StyleableTextField).
Any ideas?
Unless you're using something specific to the StyleableTextField try the s:Label. It has a property maxDisplayedLines which you could set to 2 and it will handle the truncation.

Changing Spark TextArea text color dynamically over a range of characters

Hoping to obtain the help of some Flex 3/4 gurus out there...
I am attempting to dynamically change the color of a subset/portion of text in a Spark TextArea control. Using the MX-based TextArea, I could leverage the mx.controls.textClasses.TextRange type and change the color directly as follows:
TextRange tr = new TextRange(theTextAreaControl, false, beginIndex, endIndex);
tr.color = somePredefindColor;
Input Parameters to TextRange constructor:
1st argument: The TextArea control that will provide access to the underlying textField property
2nd argument: Indicates the TextRange will not modify the content of the TextArea
3rd argument: The beginning index position in the TextArea text string
4th argument: The ending index position in the TextArea text string
How would I go about doing this for a Spark-based TextArea control? I am looking to dynamically change the font color for a range of text, not just the entire TextArea AND I cannot statically specify the font color. The problem I run into when trying to re-use the TextRange type is that the 1st argument is expected to provide the textField property, which is currently not on the Spark-based TextArea control. I thought about extending the Spark-based TextArea control to provide this accessor property but that seems like overkill and is probably not the best approach.
Any help would be greatly appreciated.
Thanks,
Michael
When manipulating Spark text components, you'll want to use the Text Layout Framework (TLF), which is built on top of the Flash Text Engine (FTE) that was introduced in Flash Player 10. It gives you major control over how the text is displayed (at the cost of being pretty complex.) Here's a good place to start: Using Text Layout Framework

wordWrap="true" does not work in Flex Datagrid

in Flex Datagrid, I need to support line wrap function in one column.
In theory I need to set wordWrap="true" and variableRowHeight="true". It works well.
But the problem is that if in the cloumn I set itemRenderer="lc.monitoring.logviewer.components.CustomColumnRenderer" properties, then the line wrap funciton does not take effect. It seems that the line wrap function can not work with itemRenderer in the datagrid column.
Any one can help me about this.
Don't use Label - it is for displaying a single line of text.
The Label control displays a single line of noneditable text. Use the Text control to create blocks of multiline noneditable text.
mx.controls.Text supports multiline text and wordwrap
If the control is not as wide as the text, the text will wordwrap. The text is always aligned top-left in the control.
You should try to override measure and updateDisplaylist inside your itemrenderer.
Your item renderer does not know how to resize the height in order to allow showing info inside the dg

Flex wordwrap issue with multiple text instances

I have a scenario where I want to dynamically add words of text to a container so that it forms a paragraph of text which is wrapped neatly according to the size of the parent container. Each text element will have differing formatting, and will have differing user interaction options. For example, imagine the text " has just spoken out about ". Each word will be added to the container one at a time, at run time. The username in this case would be bold, and if clicked on will trigger an event. Same with the news article. The rest of the text is just plain text which, when clicked on, would do nothing.
Now, I'm using Flex 3 so I don't have access to the fancy new text formatting tools. I've implemented a solution where the words are plotted onto a canvas, but this means that the words are wrapped at a particular y position (an arbitrary value I've chosen). When the container is resized, the words still wrap at that position which leaves lots of space.
I thought about adding each text element to an Array Collection and using this as a datasource for a Tile List, but Tile Lists don't support variable column widths (in my limited knowledge) so each word would use the same amount of space which isn't ideal.
Does anyone know how I can plot words onto a container so that I can retain formatting, events and word wrapping at paragraph level, even if the container is resized?
Why aren't you just using a mx:Text component and html text (you can call functions from htmlText), and apply different formatting using html tags.
For information on how to trigger a function from a htmlText field:
http://www.adobepress.com/articles/article.asp?p=1019620

Label word wrap in Flex 4

How can the text in a Label control (or a similar control) be wrapped in Flex 4 beta? In Flex 3 I could use the Text control but this is no longer available in Flex 4.
You can use maxDisplayedLines and lineBreak properties with spark Label component:
<s:Label maxDisplayedLines="{-1}" lineBreak="toFit" text="...." />
It works with Flex 4.5.
Try <s:SimpleText />. From the excellent Migration Guide (p. 75):
The lightest-weight component of the
text primitives. The SimpleText class
supports the least number of text
features. This class is similar to the
Label class, except that it supports
multiple lines. It does not support
user ineractivity such as selection,
editing, or scrolling.
Hope that helps!
Spark Label can display multiple lines, which MX Label cannot:
In Spark Label, three character sequences are recognized as explicit
line breaks: CR ("\r"), LF ("\n"), and CR+LF ("\r\n").
If you don't specify any kind of width for a Label, then the longest
line, as determined by these explicit line breaks, determines the
width of the Label.
If you do specify some kind of width, then the specified text is
word-wrapped at the right edge of the component's bounds, because the
default value of the lineBreak style is "toFit". If the text extends
below the bottom of the component, it is clipped.
To disable this automatic wrapping, set the lineBreak style to
"explicit". Then lines are broken only where the text contains an
explicit line break, and the ends of lines extending past the right
edge is clipped.

Resources