截断文本字段中的溢出文本并添加省略号(…)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了截断文本字段中的溢出文本并添加省略号(…)相关的知识,希望对你有一定的参考价值。
import flash.text.TextField; var t : TextField = new TextField(); t.text = "Now is the time for all good men to come to the aid of their party." t.width = 100; t.height = 40; t.border = true; //t.multiline = true; //t.wordWrap = true; truncate( t ); addChild( t ); function truncate( textField : TextField, addElipsis : Boolean = true, ellipsis : String = "u2026" ) : void { var tempTextField : TextField; if ( ! textOverflowing( textField ) ) return; tempTextField = copyTextField( textField ); while( textOverflowing( tempTextField, ellipsis ) ) tempTextField.text = tempTextField.text.substr( 0, tempTextField.text.length - 1 ); tempTextField.appendText( ellipsis ); textField.text = tempTextField.text; } function textOverflowing( textField : TextField, suffix : String = null ) : Boolean { var margin : Number = 4; //Flash adds this to all textfields; var tempTextField : TextField = copyTextField( textField ); if ( suffix ) tempTextField.appendText( suffix ); if ( tempTextField.textWidth > tempTextField.width - margin || tempTextField.textHeight > tempTextField.height - margin ) return true; return false; } function copyTextField( original : TextField ) : TextField { var copy : TextField = new TextField(); copy.width = original.width; copy.height = original.height; copy.multiline = original.multiline; copy.wordWrap = original.wordWrap; copy.embedFonts = original.embedFonts; copy.antiAliasType = original.antiAliasType; copy.autoSize = original.autoSize; copy.defaultTextFormat = original.getTextFormat(); copy.text = original.text; return copy; }
以上是关于截断文本字段中的溢出文本并添加省略号(…)的主要内容,如果未能解决你的问题,请参考以下文章