问题
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.
回答1:
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 thetext
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
来源:https://stackoverflow.com/questions/10705762/how-can-i-read-a-spark-textarea-implicit-line-breaks