Skip to main content
Inspiring
February 23, 2009
Answered

dynamic resizing of text line

  • February 23, 2009
  • 5 replies
  • 1034 views
hi,
i want to create my own button with a label, so the button's width changes according to the label's size, up to a certain size (beyond it the text is cropped). since i'd like to support multilingual optionality at the future i've decided to use the textLayout framework instead of the simple textField class.
anyhow, i'm creating a textline like this (where 'cf' is a character format object):
TextLineFactory.createTextLinesFromString(callBack,"abcdefghijkl",new Rectangle(0,0,NaN,NaN),cf);

and then on the callback function -
private function callBack(textLine:TextLine):void
{
var w:Number = Math.min(textLine.width, 60); // let's say 60 is the button's maximum width
var h:Number = textLine.height;
var shape:Shape = ShapeDraw.drawSimpleRect(w + 4,h + 4); // my own method of creating simple black shapes
var sprite:Sprite = new Sprite();
sprite.addChild(shape);
shape.x = shape.y = -2;
sprite.addChild(textLine);
addChild(sprite);
}
how do i get to change the textLine's width after creation and have it cropped? or is there a way to pre-calculate the textline's width and have it set to that size?
cheers,
eRez
This topic has been closed for replies.
Correct answer Abhi.G.
theErez,

You are better off setting the scrollRect on the parent of the text line (sprite). Sorry for not having clarified that.

- Abhishek
(Adobe Systems Inc.)

5 replies

Participating Frequently
February 25, 2009
Typically, in the callback you'd add the textLine to the intended container (the button in your case). Then outside the callback you can look at the children of the button and get to the textLines that way. You can see this pattern in the TextLineFactory example from the sdk.

-Chris
Adobe Employee
February 24, 2009
And don't set scrollRect in the callback. Set it after the return from createTextLines.
theErezAuthor
Inspiring
February 25, 2009
but unless in the callback i put the textLine into a class property - how can i access it from outside the callback?
this brings me to the problem i'm facing now:
let's say i have a function which creates textLine objects: it receives a string and put it into a textLine object (using the TextLineFactory). how can i return that textLine object to the caller of that function? can the callback function return a value? and if so - to who? an option i have in mind is to pass also a unique callback function, but that makes things too much complicated.
i'd appreciate any more enlightenment.
cheers,
eRez
Abhi.G.Correct answer
Participating Frequently
February 24, 2009
theErez,

You are better off setting the scrollRect on the parent of the text line (sprite). Sorry for not having clarified that.

- Abhishek
(Adobe Systems Inc.)
Participating Frequently
February 24, 2009
theErez,

1. lineBreak is applicable even if you are using TextLineFactory. You can set it on textFlowAttrs (the last parameter to createTextLinesFromString)

2. Use scrollRect (inherited from DisplayObject) to crop the TextLine based on the returned bounds.

- Abhishek
Adobe Systems
theErezAuthor
Inspiring
February 24, 2009
Abhishek,
thanks for your reply and the information you supplied me.
however, adding the following line to the 'callback' function in my original code -
textLine.scrollRect = new Rectangle(0,0,w,h);
causes some weird effect, and the only piece of text i'm able to see is the bottom part of the 'g' in my string (as if the rest of the text is masked).
it also happens if i set the lineBreak to 'explicit'
any help from here?
Adobe Employee
February 23, 2009
1) You are correctly using NaN in the bounds. After createTextLines the bounds will be filled in.
2) You also want to set lineBreak = "explicit" in the supplied TextFlow llevel TextLayoutFormat.

You will have to do the cropping yourself based on the returned bounds. As above the bounds is not filled in during the callback It is filled in on return from createTextLinesFromString.

Richard
theErezAuthor
Inspiring
February 24, 2009
thanks for your reply.
first, at what point do i set the 'lineBreak' property of the 'textFlow' object if i'm using the TextLineFactory? correct me if i'm wrong, but this is relevant only if i'm creating the textLine with the flowComposer, isn't it?
second - how do i crop the textline based on its returned bounds? if i set the textLine's width property it simply shrinks the text inside it. had i used a TextField class, i just had to set its width and height, not set the autoSize property, and that way any text beyond those dimensions isn't visible. is there any way to achieve it with a TextLine?
cheers,
eRez