根据标签宽度和字符串长度动态更新标签文本

时间:2014-06-04 10:05:41

标签: actionscript-3 flex flex4 flex4.5

我有一个标签,一个textInput和一个如下面的按钮

<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)"/>  

我的函数更新了btn点击的lbl.text及其工作情况 但我需要动态更新标签文本,我的功能看起来像这样

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);
}

您能否建议我如何计算displayFact,以便根据lbl.widthstr.length进行更改,str应符合标签的宽度。

1 个答案:

答案 0 :(得分:1)

没有简单的方法,因为char的宽度不是常数而且字体不一样。

查看herehere

相反,如果你想要实现的只是隐藏超出的文本,你可以使用

截断
<s:Label id="lbl" height="50" width="100" maxDisplayedLines=1 backgroundColor="blue"/>
相关问题