简体   繁体   中英

Highlight Text in TextField with Actionscript 3

I am trying to figure out how to create a function that will "highlight" a sub-string given a string. Ideally, the function would take either 3 parameters: Haystack String, Search/Needle String, and New Format.

Here's what I have so far:

/* 
 * 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};
}

Funny thing is that I was about to dig into this as I need it for an upcoming project. Thanks for sharing. I modified it a bit so it picks up all occurrences.

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 Having a textfield named 'txt' and text 'Yo check this out dude, checkcheck!!' on its stage

import StringUtils;
import flash.text.TextFormat;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM