2010-04-08 59 views
1

界面中只有三行文本的空間,但內容是外部和可變的,並且如果它最終佔用超過三行,需要某種「查看全部」按鈕功能。我可以考慮一下這個函數需要的樣子,但我不確定在AS3中做什麼的最好方法是什麼。類似於(僞代碼):Flash AS3:在X行裁剪TextField內容,在末尾添加'...'

function cropText(source:TextField, length:int, append:String):TextField{ 
    if(source.lineCount > length){ 
     source.text = // magic function that retuns the first length lines, 
     // minus append.length characters, with the append value tacked onto the end 
    } 
    return source; 
} 

......對不對?你將如何填補缺失的位?

回答

2

喜歡的東西...

private function cropText(source:TextField, length:int, append:String):TextField { 
    if (source.numLines > length) { 
     source.text = source.text.substr(0, source.getLineOffset(length) - append.length) + append; 
    } 

    return source; 
} 
+0

YESSS,getLineOffset是缺少一塊拼圖。謝謝! – 2010-04-08 21:30:58