xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
import flashx.textLayout.elements.*;
private function countWords():void {
var leaf:SpanElement = new SpanElement();
leaf = SpanElement(richTxt1.textFlow.getFirstLeaf());
var p:ParagraphElement = new ParagraphElement();
p = leaf.getParagraph();
doSomething(p);
while (leaf = SpanElement(leaf.getNextLeaf())) {
p = leaf.getParagraph();
doSomething(p);
}
wcLabel.text += "# Words: " + wordCount;
lenLabel.text += "Avg length of each word: " + lenTotal/wordCount + " chars";
}
private var wordCount:int = 0;
private var lenTotal:int = 0;
private function doSomething(p:ParagraphElement):void {
var wordBoundary:int = 0;
var prevBoundary:int = 0;
// If these are equal, then there are no more words.
while (wordBoundary != p.findNextWordBoundary(wordBoundary)) {
if (p.findNextWordBoundary(wordBoundary) - wordBoundary > 1) {
wordCount += 1;
}
prevBoundary = wordBoundary;
wordBoundary = p.findNextWordBoundary(wordBoundary);
// If the value is greater than 1, then it's a word, otherwise it's a space.
if (wordBoundary - prevBoundary > 1) {
var s:String = p.getText().substring(prevBoundary, wordBoundary);
lenTotal += s.length;
}
}
}
textAlign="justify" percentWidth="100">
Lorem ipsum dolor sit amet, consectetur adipiscing
elit.
Cras posuere posuere sem, eu congue orci mattis
quis.
Curabitur pulvinar tellus venenatis ipsum tempus
lobortis.