Skip to main content

SetStyle() Method Autoresizing Problem in Flex

setStyle() Method:

Flex setStyle() method is used to change an object's style properties at runtime, for example in the script section you can write

someObject.setStyle("fontSize", varSize);

to change someObject’s font size to the size stored in the
variable “varSize”.

Resize Problem with setStyle():

The problem I faced using setStyle() was that I wanted a textAarea to resize its width after the textArea’s fontSize is increased. For example if the font size is set to too large which cannot be accommodated in the current width then the text will not fit in the textArea nor the textArea will resize itself if the vertical and horizontal Scrolling policy is set to “off”. The straight forward approach which should (but didn’t) work is given below to achieve the resizing effect.

public function setFontSize():void{

myTextArea.width = textareaScrolling.textWidth;

However the textArea’s width is not adjusted by the last statement, somehow the .textWidth prpoperty is not updated and if the varSize(new font size) is big enough to make the text overflow the textArea, the textArea’s width is not adjusted.

One Simple solution is to use the.htmlText property of the textArea instead of using the setStyle() method. The example goes like this.

public function setFontSize ():void{
myTextArea.htmlText = "<TEXTFORMAT LEADING='2'><P ALIGN='LEFT'><FONT FACE='"Arial"'
SIZE='"+ varSize +"' LETTERSPACING='0' KERNING='0'>"+ myTextArea.text+" </FONT></P></TEXTFORMAT>";
myTextArea.width = textareaScrolling.textWidth;

validateProperties() is mandatory here, without calling this method the textWidth property wont be updated and your textArea wont resize.

Hope this helps!!!