subrat71
subrat71

Reputation: 348

Update label text according to label width and string length dynamically

I have a label a textInput and a button like below

<s:TextInput id="inputTxt"/>
<s:Label id="lbl" height="50" width="100" backgroundColor="blue"/>
<s:Button id="btn" label="Marquee" height="40" width="80" click="btn_clickHandler(event)"/>  

and my function updates the lbl.text on btn click and its working.
But i need to update the label text dynamically and my function looks like this

protected function btn_clickHandler(event:MouseEvent):void
{
  str = inputTxt.text;
  strLen = str.length;
  lbl.text = updateLabel(inputTxt.text);
}


private function updateLabel(str:String):String
{
  var displayFact:int;// code should be updated here

  if(strLen > 14)
      displayFact = 12;
  else
      displayFact = strLen;

  return new String(str).substr(0,displayFact);
}

can you suggest how can i calculate displayFact so that according to lbl.width and str.length it will changes and the str should fit the width of the label.

Upvotes: 0

Views: 224

Answers (1)

SharpEdge
SharpEdge

Reputation: 1762

There is no easy way because the width of a char is not constant and the font is not the same.

Look here and here

Instead if what you are trying to achieve is just hide the exceeding text you can truncate using

<s:Label id="lbl" height="50" width="100" maxDisplayedLines=1 backgroundColor="blue"/>

Upvotes: 1

Related Questions