1

我在格式化文本字段时遇到问题。我有按钮 + 和 - 来调整文本的大小。TextField 类具有用于新文本格式的属性 defaultTextField。当我更改 defaultTextFormat 大小属性时 - 整个文本的大小都会发生变化。我到处寻找解决方案,但还没有找到。文本编辑器 WISWYG(我不确定名称是否正确)在我遇到问题时只需更改 defaultTextFormat 属性即可正常工作。可能是因为 flash 和 AIR 之间的差异(flash 上的编辑器和我的 AIR 上的应用程序)。请帮忙。

这里设置/获取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);
    }

以及更改格式的代码:

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

编辑:用于说明的 DROPBOX 上的图像: https ://dl.dropboxusercontent.com/u/237572639/Capture.PNG

编辑 2:我需要的图像(代码完全一样!)(所见即所得编辑器) https://dl.dropboxusercontent.com/u/237572639/WYSIWYG.PNG

4

1 回答 1

0

要更改所有未来键入的文本,您可以尝试以下代码(结果可见此处):

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);

})

当然,这只是一种做你想做的事的方式,你必须根据你的需要调整它并改进它。

于 2014-12-12T17:10:59.467 回答