Wednesday, June 18, 2008

dynamic text field part I - letter spacing

This blog has moved.
Just discovered that flash resets the author-time letter spacing of a dynamic textfield when the textfield's text is set in ActionScript.

To maintain the authortime
letter spacing settings of the textfield, you need to record the textfield's textformat, set the text, and then reset the textfield's textformat to what it was. For example, if your textfield's instance name is test:

var fmt:TextFormat = test.getTextFormat();
test.text="New text";
test.setTextFormat(fmt);

something to check is that if the textfield does not contain any text, the author-time TextFormat
letter spacing property is not recorded. So if you don't want your textfield to contain any text, make sure the textfield contains at least one space to ensure it maintains its author-time TextFormat letter spacing property.

9 comments:

webdesignclasses said...

I cant get this (letter spacing) to work with a dynamic text field from an XML file -

webdesignclasses said...

here is my code:
// item properties assigned from XML
var fmt:TextFormat = cur_item.menu_txt.getTextFormat();
curr_node = node_xml.childNodes[i];
curr_item.action = curr_node.attributes.action;
curr_item.variables = curr_node.attributes.variables;
curr_item.menu_txt.setTextFormat(fmt);
curr_item.menu_txt.text = curr_node.attributes.name;

Craig Grummitt said...

yeah i don't think it's related to the source of the text. You need to set the TextFormat of your textfield AFTER setting the text of the textfield.

So swap the last two lines of your code and it should work.

Trys said...

well spotted - you've just saved me an hour of annoyance. have you logged a bug with Adobe for this?

Craig Grummitt said...

yeah thanks that hour of annoyance was pretty annoying too! it hadn't occurred to me to log the letter spacing bug, but i have now, cheers.

123 said...

is there a solution for htmlText with links?

This solutions works great if you don't use html

Craig Grummitt said...

Yeah 123, i can see your problem.

Using setTextFormat to set the textFormat of a textfield will replace any textformatting you applied to the textfield previously. this includes any textformatting created via htmltext, such as bold, italic or even anchor tags.

However, you can apply the textFormat object as a default to the textField, before setting the htmlText. You can do this like so:

var fmt:TextFormat = test.getTextFormat();
test.defaultTextFormat=fmt;
test.htmlText="Click <a href='http://www.craiggrummitt.com'>here</a> for more information.";

This way, the formatting applied by the htmlText property doesn't get overridden.

123 said...

hi craig,

you're right, if you would do this in as3!!!

the declaration defaultTextFormat doesn't exists in as2.

the only solution - if you would call it as a solution - i found is to set the test.url = '' to the hole textfield!

So, is there a real usable solution for html in as2?

Craig Grummitt said...

ah. the plot thickens. I think you're right, to retain the styling in AS2 you have to set the TextFormat of a textfield after setting the text or htmlText property, which doesn't work with styling in htmlText.

I think what you might want to have a look at is the StyleSheet class and the TextField.styleSheet property. The StyleSheet class has a letterSpacing property that you could use to retain the textField's letter-spacing.