Showing posts with label TextLineMetrics. Show all posts
Showing posts with label TextLineMetrics. Show all posts

Monday, 31 May 2010

TextLineMetrics...continued

This code works:

test_txt.addEventListener(Event.CHANGE,getLonger);
function getLonger(evnt:Event):void {
var met:TextLineMetrics = test_txt.getLineMetrics(0);
trace(met.height);
trace(met.ascent);
trace(met.descent);
trace(met.leading);
trace(' ');
if(test_txt.textHeight > test_txt.height){
test_txt.height = test_txt.textHeight + 6;
}
}

So, why would 6 be the magic number? I tried this in font size 18, 24, and 96, and different font faces as well. (By the way, in my previous efforts, the first line wasn't scrolling up because of using '+=', actually, it was because there wasn't enough added. So this can happen with '=' as well, if the textfield height isn't large enough.)

Anyway, if you have a look at this diagram:


(from http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/text/TextLineMetrics.html)

You can see a '2-pixel gutter' at the top and bottom of the text field, that gives 4 extra pixels, and there is the 'leading' value as well. I have traced out the 'leading' value in different faces and sizes, it is always 2.

So my theory is, that you need to take account of the gutters, and the leading for the next line. So for safety's sake, I will use '4 + met.leading';

UPDATE: You can set 'leading' in the properties panel, if you mouse over the second option in 'Spacing', it says 'Line spacing' - Mine was set to 2.0, but if I change it to, for example 5, the met.leading traces out 5.

Sunday, 30 May 2010

TextLineMetrics...to be continued.

OK, I am currently trying to make a text box that grows in length when the typer makes extra lines.
I tried:

if(test_txt.textHeight > test_txt.height) {
test_txt.height = (test_txt.height /(test_txt.numLines - 1)) * test_txt.numLines;
}

but text fields have some extras around the edges and stuff, so the text box got too big as time went by.

As a sidenote, I would like to mention that, if you use += and just try to add to the textfield height instead of changing the textfield height, that the first line scrolls up the text box.