Search code examples
actionscript-3textairtextformat

as3 Text Field formatting issue


I have a problem with formatting the text field. I have buttons + and - to size of text. TextField class has property defaultTextField for new text formatting. And when I change defaultTextFormat size property - whole text's size changes. I have searched for solution everywhere and I haven't found it yet. Text editor WISWYG (I am not sure if name is right) is working well with just changing defaultTextFormat property while I have issue. Maybe it happens because of difference between flash and AIR (editor on flash and my app on AIR). Please help.

Here code to set/get TextFormat:

    public function set selectionTextFormat(value:TextFormat):void {
        var begin:int = _textField.selectionBeginIndex;
        var end:int = _textField.selectionEndIndex;
        if (begin == end)
        {
            _textField.defaultTextFormat = value;
        }
        else
        {
            _textField.setTextFormat(value, begin, end);
        }
    }

    public function get selectionTextFormat():TextFormat 
    {
        var begin:int = _textField.selectionBeginIndex;
        var end:int = _textField.selectionEndIndex;
        if (begin == end)
        {
            return _textField.defaultTextFormat;
        }
        return  _textField.getTextFormat(begin, end);
    }

And code to change format:

    private function setFormat(property:String, value:*):void
    {
        var tf:TextFormat = TextFormatter.TF.selectionTextFormat;
        tf[property] = value;
        TextFormatter.TF.selectionTextFormat = tf;
    }

EDIT : IMAGE ON DROPBOX FOR EXPLANATION: https://dl.dropboxusercontent.com/u/237572639/Capture.PNG

EDIT 2: IMAGE OF WHAT I NEED (CODE IS ABSOLUTELY SAME!) (WYSIWYG editor) https://dl.dropboxusercontent.com/u/237572639/WYSIWYG.PNG


Solution

  • To change all future typed text, you can try this code ( the result is visible here ):

    var text_input:TextField = new TextField();
        text_input.border = true;
        text_input.type = 'input';
        text_input.text = 'hello world!';
    addChild(text_input);
    
    var new_fmt:TextFormat = new TextFormat();
    
    btn_color.addEventListener(
        MouseEvent.CLICK, 
        function(e:MouseEvent):void {
            // set the new text color
            new_fmt.color = 0xFF0000;   
        }
    )
    btn_size.addEventListener(
        MouseEvent.CLICK, 
        function(e:MouseEvent):void {
            // set the new text size
            new_fmt.size = int(txt_size.text)       
        }
    )
    
    text_input.addEventListener(Event.CHANGE, function(e:Event):void {
    
        text_input.setTextFormat(new_fmt, text_input.caretIndex-1);
    
    })
    

    Of course, this is just a manner to do what you want, you have to adapt it to your need and improve it.