使用Actionscript 3在TextField中突出显示文本

时间:2012-08-23 20:19:10

标签: actionscript-3 flash actionscript

我试图弄清楚如何创建一个能够“突出显示”给定字符串的子字符串的函数。理想情况下,该函数将采用3个参数:Haystack String,Search / Needle String和New Format。

这是我到目前为止所拥有的:

/* 
 * Replaces all occurances of one series of characters with another series of characters in correct TextFormat
 *
 * @param The haystack: TextField you are invoking the replace on.
 * @param The needle: Series of characters which you plan on replacing
 * @param The new format of the sub-string.
 * @return null
 * 
 * @usage <pre>StringUtility.highlight("Hello World", "World", "Michael");</pre>
 */ 
public static function highlight( block:TextField, search:String, appliedFormat:TextFormat ) {
    var original:String = block.text;
    var positions:Object = StringUtility.getPostitions( original, search );
    block.setTextFormat( value.appliedFormat, positions.posStart, positions.posEnd );
}

/* 
 * Replaces all occurances of one series of characters with another series of characters
 *
 * @param The haystack: string you are invoking the getPostitions on.
 * @param The needle: series of characters which you plan on measuring
 * @return The object with position values
 * 
 * @usage <pre>StringUtility.getPostitions("Hello World", "World", "Michael");</pre>
 */
public static function getPostitions(original:String, search:String) : String {
    var return_string  : String = "";
    var start_position : Number = 0;
    var end_position   : Number = 0;

    while (true) {
        start_position = original.indexOf( search, end_position );

        if ( -1 == start_position ) break;

        return_string += original.substring( end_position, start_position );
        return_string += search;

        end_position = start_position + search.length;
    }

    return var positions:Object = {posStart:start_position, posEnd:end_position};
}

1 个答案:

答案 0 :(得分:3)

有趣的是,我正要深入研究这个,因为我需要它来进行即将开展的项目。感谢分享。我对它进行了一些修改,以便它可以检测所有事件。

StringUtils.as

package  {

    import flash.text.*;

    public class StringUtils {

        public function StringUtils() {}

        public static function highlight(block:TextField, search:String, appliedFormat:TextFormat) {

            var positions:Array = getPositions(block.text, search);
            var len:uint = positions.length;

            for(var i:int = 0; i<len; i++){
                block.setTextFormat(appliedFormat, positions[i].posStart, positions[i].posEnd);
            }
        }

        public static function getPositions(original:String, search:String):Array {

            var positions:Array = [];
            var startPosition:Number;
            var endPosition:Number;

            while (startPosition != -1) {
                startPosition = original.indexOf(search, endPosition);
                endPosition = startPosition + search.length;
                if(startPosition > -1) positions.push({posStart:startPosition, posEnd:endPosition});
            }

            return positions;
        }
    }
}

StringUtils.fla 有一个名为'txt'的文本域和文本'哟检查这个dude,checkcheck !!'在舞台上

import StringUtils;
import flash.text.TextFormat;

var tf:TextFormat = new TextFormat();
tf.color = 0xFF0000;

StringUtils.highlight(txt, "check", tf);