dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: text editor

  1. #1
    I Just Started Flash Today johnglynn's Avatar
    Join Date
    Mar 2001
    Posts
    180

    text editor

    I am trying to make a nifty text editor that sports common, expected features like bold, italics, underline, bullets, links etc...

    The problem I am having is getting text states (setting is no problem). There is no inherent "getBold" or "getItalics" to know what state the text is in.

    Try this out (version 7 as 2):

    Code:
    this.createTextField("output_txt", this.getNextHighestDepth(), 0, 0, 300, 200);
    output_txt.html = true;
    output_txt.multiline = true;
    output_txt.wordWrap = true;
    output_txt.border = true;
    output_txt.type = "input";
    output_txt.htmlText = "Some very happy text for you to change.  Select a portion of this text and right click and select \"Bold\"";
    var my_cm:ContextMenu = new ContextMenu();
    my_cm.customItems.push(new ContextMenuItem("Bold...", doBold));
    my_cm.customItems.push(new ContextMenuItem("Uppercase...", doUpperCase));
    function doUpperCase():Void {
    	var startIndex:Number = Selection.getBeginIndex();
    	var endIndex:Number = Selection.getEndIndex();
    	var stringToBold:String = output_txt.text.substring(startIndex, endIndex);
    	output_txt.replaceText(startIndex, endIndex, stringToBold.toUpperCase());
    }
    function doBold():Void {
    	var startIndex:Number = Selection.getBeginIndex();
    	var endIndex:Number = Selection.getEndIndex();
    	var stringToBold:String = output_txt.text.substring(startIndex, endIndex);
    	stringToReplace = setText(stringToBold);
    	output_txt.replaceText(startIndex, endIndex, stringToReplace);
    	var totalString:String = new String(output_txt.text);
    	output_txt.htmlText = totalString;
    	var totalString2:String = output_txt.htmlText;
    	debug(totalString);
    }
    output_txt.menu = my_cm;
    //
    function myOnKeyDown() {
    	if (Key.isDown(Key.CONTROL) && Key.isDown(Key.SHIFT) && Key.getCode() == 38) {
    		doBold();
    	}
    }
    var myListener:Object = new Object();
    myListener.onKeyDown = myOnKeyDown;
    Key.addListener(myListener);
    //titleStyle = new TextFormat();
    function setText(_s) {
    	__s = "<b>" + _s + "</b>";
    	return __s;
    }
    //
    //
    this.createTextField("debug_txt", this.getNextHighestDepth(), 0, 205, 300, 600);
    debug_txt.multiline = true;
    debug_txt.wordWrap = true;
    function debug(_t) {
    	debug_txt.text = _t;
    }
    I apologize for the lack of comments in the code. Let me know if it needs more explanation.

    Thanks,
    John

  2. #2
    I Just Started Flash Today johnglynn's Avatar
    Join Date
    Mar 2001
    Posts
    180
    bump?

  3. #3
    Senior Member inder_s2010's Avatar
    Join Date
    Apr 2004
    Location
    don't no
    Posts
    198
    have you embedded fonts you are using?
    inder

  4. #4
    Member
    Join Date
    Oct 2003
    Posts
    46

    Try This

    this.createTextField("output_txt", this.getNextHighestDepth(), 0, 0, 300, 200);
    output_txt.html = true;
    output_txt.multiline = true;
    output_txt.wordWrap = true;
    output_txt.border = true;
    output_txt.type = "input";
    output_txt.htmlText = "Some very happy text for you to change. Select a portion of this text and right click and select \"Bold\"";
    var my_cm:ContextMenu = new ContextMenu();
    my_cm.customItems.push(new ContextMenuItem("Bold...", doBold));
    my_cm.customItems.push(new ContextMenuItem("Uppercase...", doUpperCase));
    my_cm.customItems.push(new ContextMenuItem("ChkBoldState...", getBoldState));
    function doUpperCase():Void {
    var startIndex:Number = Selection.getBeginIndex();
    var endIndex:Number = Selection.getEndIndex();
    var stringToBold:String = output_txt.text.substring(startIndex, endIndex);
    output_txt.replaceText(startIndex, endIndex, stringToBold.toUpperCase());
    }
    function doBold():Void {
    var startIndex:Number = Selection.getBeginIndex();
    var endIndex:Number = Selection.getEndIndex();
    var stringToBold:String = output_txt.text.substring(startIndex, endIndex);
    stringToReplace = setText(stringToBold);
    output_txt.replaceText(startIndex, endIndex, stringToReplace);
    var totalString:String = new String(output_txt.text);
    output_txt.htmlText = totalString;
    var totalString2:String = output_txt.htmlText;
    debug(totalString);
    //debug(stringToReplace);
    }
    function getBoldState(){
    var startIndex:Number = Selection.getBeginIndex();
    var endIndex:Number = Selection.getEndIndex();
    var resultString:String = debug_txt.text.substring(startIndex, endIndex+7);
    startString=resultString.substring(0, 3)
    endString=resultString.substring(resultString.leng th-4, resultString.length)
    if(startString.toLowerCase()=="<b>" && endString.toLowerCase()=="</b>"){
    trace("True")
    }
    else{
    trace("False")
    }
    }
    output_txt.menu = my_cm;
    //
    function myOnKeyDown() {
    if (Key.isDown(Key.CONTROL) && Key.isDown(Key.SHIFT) && Key.getCode() == 38) {
    doBold();
    }
    }
    var myListener:Object = new Object();
    myListener.onKeyDown = myOnKeyDown;
    Key.addListener(myListener);
    //titleStyle = new TextFormat();
    function setText(_s) {
    __s = "<b>" + _s + "</b>";
    return __s;
    }
    //
    //
    this.createTextField("debug_txt", this.getNextHighestDepth(), 0, 205, 300, 600);
    debug_txt.multiline = true;
    debug_txt.wordWrap = true;
    function debug(_t) {
    debug_txt.text = _t;
    }

  5. #5
    I Just Started Flash Today johnglynn's Avatar
    Join Date
    Mar 2001
    Posts
    180

    Closer:)

    Hey creativesaby,

    Thanks for that. That allows me to check if the selected text is bold. I need to know the states independant of whether I have selected it or not.

    If I select two words, make them both bold then select only one of the bolded words and check their state the getBoldState() method returns false.

    hmmm....

    Do I need to make an array of all the characters in the text?

    (Uh oh)

    Thanks,
    John
    Last edited by johnglynn; 03-16-2006 at 09:36 AM. Reason: uh...

  6. #6
    I Just Started Flash Today johnglynn's Avatar
    Join Date
    Mar 2001
    Posts
    180

    I don't understand?

    Quote Originally Posted by inder_s2010
    have you embedded fonts you are using?
    I am not sure where you were going with that question but no doubt somewhere brilliant. I don't want to use embedded fonts if I don't have to because of fle size. I would rather use device fonts.

    I am not sure where you were going but I am guessing you were suggesting embedding all the fonts I want to use along with all the states I want to use? Uh oh ...

    Thanks for the reply,
    John

  7. #7
    I Just Started Flash Today johnglynn's Avatar
    Join Date
    Mar 2001
    Posts
    180
    this works, but not that well and I am thinking it uses embedded fonts because of all the limitations.

    http://www.flashloaded.com/flashcomp...xteditor/?id2=

  8. #8
    Grandmaster Flash
    Join Date
    Apr 2004
    Location
    Edinburgh, Scotland
    Posts
    139
    Hi John

    Try swapping the getBoldState method as suggested by creativesaby for the one below. It ALMOST works. The only problem is getting the caret position in the actual HTML source of the textfield, as opposed from the offset of the displayed text. I'm not sure how you do it, but you might have a way. I think the basic algorithm should work fairly well though...

    Code:
    function getBoldState() {
    	var bStartTag:String = "<B>";
    	var bCloseTag:String = "</B>";
    	var inBold:Boolean = false;
    	var caretIndex:Number = Selection.getCaretIndex();
    	trace(caratIndex);
    	for(var i:Number=0; i<output_txt.htmlText.length; i++)
    	{
    		// read next chars
    		var snippetStart:String = output_txt.htmlText.substring(i, i+bStartTag.length);
    		var snippetClose:String = output_txt.htmlText.substring(i, i+bCloseTag.length);
    		if(snippetStart==bStartTag)
    		{
    			trace("in bold" + i );
    			inBold = true;
    		}
    		if(snippetClose==bCloseTag)
    		{
    			trace("out of bold" + i);
    			inBold = false;
    		}
    		if(i==caretIndex)
    		{
    			trace(inBold);
    		}
    	}
    }
    If it ain't broke, don't fix it.

  9. #9
    I Just Started Flash Today johnglynn's Avatar
    Join Date
    Mar 2001
    Posts
    180
    I see where you are going. Sweet! So close...

    I have a bad feeling baout this though. I am thinking that it will wind up being something ugly like this:
    (rough code, needs textField_txt.text, but you get the idea)

    Code:
    var myString:String = myTextField.text;
    var numChars:Number = myString.length;
    var char_array:Array = new Array();
    //
    for (n = 0; n < numChars; n++) {
    	//[bold, italics, font, size, color, underline]
    	b = false;
    	i = false;
    	f = "_sans";
    	s = 12;
    	c = 0x000000;
    	u = false;
    	char_array.push([b, i, f, s, c, u]);
    }
    //then each character has an array of all attributes associated with it
    trace(char_array[1][2]);
    Closer?
    Last edited by johnglynn; 03-16-2006 at 11:53 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center