开发者

Flash AS3: crop TextField content off at X lines, add '...' at the end

There's only room for three lines of text in the interface, but the content is external and variable, and if it ends up taking up more than three lines, there needs to be some sort of 'view all' button functionality. I can kind of think about what that function needs to look like, but I'm not quite sure what the best way to do it in AS3 would be. Something like (in pseudo code):

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 charact开发者_运维问答ers, with the append value tacked onto the end
    }
    return source;
}

... right? How would you fill in the missing bit?


Something like...

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜