In this explorer I’m going to highlight everything I can think of that can be done to a TextArea. Most of the solutions – such as changing the lineHeight – comes from other developers. I’m just collecting everything I’ve found here as a central resource.
| view source |
Here is the MXML code declaring the TextArea I’ll be talking about throughout the rest of this post – notice the ID=’ta’ for reference.
<mx:TextArea
id="ta"
fontSize="{ _fontSize }"
leading="{ _leading }" />
ValidateNow!
To access TextArea properties, such as lineHeight, textHeight, etc., you’ll want to call ta.validateNow(); before hand – thanks @brandon_ellis for the tip. That runs the Layout Manager which validates everything. Then the values should be correct. I’ve run into numerous problems trying to get the textHeight, especially when using htmlText. It always seems to be wrong unless calling validateNow();
Accessing the unaccessible
When creating a TextArea with MXML you can access the leading and fontSize properties. But accessing ta.leading or ta.fontSize in Actionscript won’t work. A simple work around – hack – is to set the values to Bindable variables and then change those varables as needed.
Addendum: Nick Kwiatkowski points out in the comments that fontSize and leading are styles – which is pretty obvious when you stop and think about it for a second – and that’s why they aren’t accessible to Actionscript unless you use ta.setStyle( ‘fontSize’, value ); Thanks Nick.
The term leading is so preMil
Leading is just the line height of the TextArea. Why they don’t just call it lineHeight I have no idea. So what if Photoshop calls it leading – which is probably a hold over from Gutenberg or something. I’m a programmer. Not a type setter!
Anyhow, if you want to get the current leading value of TextArea you’ll need to do a little dirty work. Turns out that you can’t just ask. But if you look at the ta.htmlText you’ll see the markup code in there complete with the leading value. It would look something like this:
<TEXTFORMAT LEADING="2"><P ALIGN="LEFT">
I wrote this simple getter to suck the value out of there:
private function getLeading( ta:TextArea ):int {
// can't directly access the value for leading
ta.validateNow();
var leadingPosition:int = ta.htmlText.indexOf( 'LEADING' );
var leadingEndPosition:int = ta.htmlText.indexOf( '"', leadingPosition + 9 );
var defaultLeading:int = int( ta.htmlText.substring( leadingPosition + 9, leadingEndPosition ) );
return defaultLeading;
}
I could probably just assume that the position of LEADING in the htmlText will never change but what the hell. It easy enough to get the indexOf so that I know for sure I’m looking in the correct place. And besides, if the value goes into double-digits then I’ll have to look at a different substring.
All the above does is look for where the letters ‘leading‘ start. Then add 9 – number of characters in leading=”, find where the next indexOf a double-quote starts from that point which will probably be either one or two characters away. ( If you have a value of leading with triple digits then you’re doing something wrong or trying to get into the Guinness book.
Addendum in the comments mrm suggests using regular expressions to find the leading value. Something like:
var match:Array = ta.htmlText.match(/<TEXTFORMAT[^><]+LEADING="(\d+)"[^><]*>/i);
var defaultLeading:int = match[1];
Number of lines
Why, why, why would the number of lines be internal? I’m sure it’s Deepas fault. :-) So to get the number of lines in your TextArea try this:
private function getNumberLines( ta:TextArea ):int {
ta.validateNow();
var numLines:uint = ta.mx_internal::getTextField().numLines;
return numLines;
}
To be fair the Flex Dev team is on average about 50 times smarter than me so maybe there is a good reason for numLines being internal.
What is like leading but different?
Try lineHeight. Leading is . . . um . . . you know, I’m not exactly sure what leading measures. But lineHeight is the height of a line in pixels. And of course you can’t just get it directly from the TextArea. You have to jump through yet another hoop – abet a small one.
private function getLineHeight( ta:TextArea ):int {
ta.validateNow();
var textLineMetrics:TextLineMetrics = ta.getLineMetrics( 0 );
var lineHeight:int = textLineMetrics.height;
return lineHeight;
}
Just how big is this thing?
Finally, if you need to know the pixel size of all of the text in the TextArea then just ask for ta.textHeight. Of course don’t forget to call ta.validateNow(); first or you’ll get a number that only resembles the textHeight.
Selecting Text
Programatically selecting text in a TextArea is simple. The problem is that unless the TextArea has focus you won’t be able to see what is selected. The trick is to give the TextArea focus after every change of the selection you make. That’s what I’m doing with the NumericSteppers that change the selectionBeginIndex and selectionEndIndex values.
Credits
I pulled all these examples from previous projects that I’ve worked on. But most of this I originally got from scowering other blogs – mostly blog.FlexExamples.com – so thanks everyone.





Dan,
The reason why your leading and fontSize are not accessable via ActionScript is because they are actually styles, not properties. To set them via code (and avoid all that gobby-gook that comes from binding), you can use the ta.setStyle("fontSize","17"); command. If you wish to get the current style for some odd reason, you can to the inverse: ta.getStyle("fontSize);
Try this regular expression instead of your indexOf + substring method:
var match:Array = ta.htmlText.match(/<TEXTFORMAT[^><]+LEADING="(d+)"[^><]*>/i);
var defaultLeading:int = match[1];
It's much more reliable and, of course, cleaner.
There's a backslash before the "d" in the capturing parentheses (to match one or more digits). Your script must've removed it.
@Nick K, thanks for pointing that out. I've used setStyle many times. I don't know why it didn't occure to me that fontSize was a style – duhhh!
@mrm, damn I wish I knew regular expresions that well. Thanks.
Thanks for compiling all of this! It is very instructive.
I hate to nitpick, but you should correct the regular expression you wrote in the article.
- you should escape the < and > characters in the article or the <TEXTFORMAT[^> part won't be displayed (the browser interprets it as a html tag)
- change the forward slash in the paranthesis with a backslash. \d (I hope the backslash character was printed out ok) is the correct way to match digits. Using a forward slash you get a compiler error.
Here are a couple of regular expression examples
@mrm, thanks for nitpicking. This time I updated the code in the example to make sure it works – it does. Plus I gave you credit in the source-code comment with a link to your site. Thanks again.
lineHeight = fontSize + leading
In other words, leading is the height of the white space between lines.
Also, there's a problem with your explorer: the value of the TextArea's leading and the value in the NumericStepper are not in sync. Try clicking several times on the up arrow to see what I mean. If you then click the down arrow and the counter reaches 0 and keep clicking the NumericStepper shows 3, then 1, then 2 then you get an error.
@mrm, Are you sure that lineHeight = fontSize + leading? It seems that they are related but that there are rounding issues and such going on in the TextArea.
I do get some funky behavior with the NumericStepper and the leading. Like you have to click twice to increase by one even though the step is 1. I worked for a while to get that to work but fixing one thing broke another. That's why I think that the TextArea has some funky measurements going on.
I'm pretty sure.
The problem might be that font size is measured in pt and line height in px. So yeah, there can be some rounding issues.
See also:
http://en.wikipedia.org/wiki/Typeface#Font_metrics
http://livedocs.adobe.com/flex/3/langref/flash/text/TextLineMetrics.html
Hi,
Very useful example for me. But i want line number when clicked and up or down key press. Any idea for this.
Thanks for sharing code.
@shivang, That's a tough one. I think you could estimate the line number based on the lineHeight.
Hi, is it possible to create table in any flex text component? I need table like in Microsoft Word…
Can i view in any flex text component Excel document with it's table and another date?
Thanks for any suggestions!
@Vasil No, you can’t create an HTML table in any TextArea. However you can create something like a table layout very easily using HGroup and VGroup.
Thanks) i create table( when i print its seems like a table) with:
1) HRule, VRule,Label
2) TextArea (grupping together)
3) Resizible TextArea
And one more probem…
How set Text_Input_ID.text as CURRENT changing string in textarea?
@Vasil If I understand the question you want to change the text that is displayed in your TextArea, correct? If that’s the case then assume that your TextArea has an id=”myTextArea” then in Actionscript all you need to do is say: myTextArea.text = “hello world”.
lets see simple exampe)
we have some text:
string1: "Hello world from srt1! "
string2: "Hello world from srt2! "
string3: "Hello world from srt3! "
string4: "Hello world from srt4! "
also we have textinput component, i need set textinput.text = string_â„–_n.text
where n=string where cursor being…
if string2: "Hello world CURSOR_SET_HERE from srt2! "
then textinput.text = "Hello world from srt2! "
if string4: "Hello world CURSOR_SET_HERE from srt4! "
then textinput.text = "Hello world from srt4! "
@Vasil I can't remember the specifics off the top of my head but there's a method on the TextArea for setting the cursor position. It sounds like that's all you need to make your example work.
thanks) but i cant find example where:
1. i can find cursor position
2. analizing curor position, and then find string where cursor set (probably i must work with geometry?)
sorry for my eng)
@Vasil Keep an eye out. I’ll write a blog post soon about how to place the cursor in a specific location in a TextArea. As for finding the text at the cursor location that’s nothing more than looking at the content of the TA – as a string – and using the String utilities. That’s basic Actionscript.