about__ Flex 里 TextArea 字符换行问题。
最近在做一个字体编辑的软件,其中需要用 TextArea 接受用户输入,并转换成 Text 保留在 图片上,碰到了字符自动换行的问题,开始是在 TextArea 的text 里 追加 "\n" , 但后来找到了一种更简单的办法,就是动态控制 Text 的 Width ,代码如下:
private function setCssStyle():void{
fontSty=new FontStyleManager(parseInt(hs_lblWidth.value.toString()));
}
private static var area:TextArea=new TextArea();;
private function downHander():void{
vs_toolbarFoot.selectedChild= can_vs0;
can_img.addEventListener(MouseEvent.DOUBLE_CLICK,editText);
}
private function editText(e:MouseEvent):void{
if(vs_toolbarFoot.selectedChild!=can_vs0){
return;
}
//area=new TextArea();
area.text="";
area.setStyle("fontSize",fontSty.getSize());
area.maxChars = null;
area.alpha=0.5;
area.x=can_img.mouseX;
area.y=can_img.mouseY;
area.width=img.width-area.x - 4;
area.height = fontSty.getSize() * 2 ;
area.addEventListener(Event.CHANGE,changeManor);
area.horizontalScrollPolicy = "off";
area.verticalScrollPolicy = "off";
can_img.addChild(area);
area.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE,write);
area.addEventListener(FocusEvent.FOCUS_OUT,write);
}
private function write(e:FocusEvent):void{
if(area.text==""){
can_img.removeChild(area);
return;
}
var txt:Text=new Text();
txt.setStyle("fontSize",fontSty.getSize());
txt.x=area.x;
txt.y=area.y;
txt.htmlText=area.text;
txt.width=area.textWidth + fontSty.getSize();
// if(txt.width > area.width ){
// txt.htmlText +="\n" ;
// Alert.show("IS ME !!!!");
// }
// Alert.show(txt.width.toString());
// Alert.show(area.width.toString()+"___area");
can_img.removeChild(area);
can_img.addChild(txt);
}
private function changeManor(e:Event):void{
var manor :int = area.mx_internal::getTextField().numLines;
// if(manor < 2){
//
// }else{
// area.height = manor * fontSty.getSize();
// }
area.height = manor * fontSty.getSize() * 2;
if(( area.height + area.y ) > can_img .height){
area.maxChars = area.length ;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package
{
import flash.core.controls.manager.FontStyleManager;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.MouseEvent;
import mx.containers.Canvas;
import mx.controls.Text;
import mx.controls.TextArea;
import mx.core.mx_internal;
public class BWriteAble
{
private var _area:TextArea = new TextArea();
private var _styManager:FontStyleManager;
private var _dis:Canvas;
public function BWriteAble()
{
}
public function setArea(area:TextArea):void
{
this._area = area;
}
public function getArea():TextArea
{
return this._area;
}
public function setStyManager(size:int):void
{
_styManager = new FontStyleManager(size);
}
public function getStyManager():FontStyleManager
{
return this._styManager;
}
public function setDis(dis:DisplayObject):void
{
this._dis=dis;
}
public function getDis():DisplayObject
{
return this._dis;
}
private function downHander():void
{
_dis.addEventListener(MouseEvent.DOUBLE_CLICK,editText);
}
private function editText(e:MouseEvent):void
{
_area.text="";
_area.setStyle("fontSize",_styManager.getSize());
_area.maxChars = null as int;
_area.alpha=0.5;
if(_dis.mouseX > _dis.width - _styManager.getSize()-5 )
{
_area.x=_dis.width - _styManager.getSize()-5;
}
else
{
_area.x=_dis.mouseX;
}
if(_dis.mouseY > _dis.height - _styManager.getSize() * 2 )
{
_area.y=_dis.height - _styManager.getSize() * 2 ;
}else{
_area.y=_dis.mouseY;
}
_area.width=_dis.width-_area.x - 5;
_area.height = _styManager.getSize() * 2 ;
_area.addEventListener(Event.CHANGE,changeManor);
_area.horizontalScrollPolicy = "off";
_area.verticalScrollPolicy = "off";
_dis.addChild(_area);
_area.setFocus();
_area.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE,write);
_area.addEventListener(FocusEvent.FOCUS_OUT,write);
}
private function write(e:FocusEvent):void
{
if(_area.text=="")
{
_dis.removeChild(_area);
return;
}
var txt:Text = new Text();
txt.setStyle("fontSize",_styManager.getSize());
txt.x=_area.x;
txt.y=_area.y;
txt.htmlText=_area.text;
txt.width=_area.textWidth +5;
_dis.removeChild(_area);
_dis.addChild(txt);
}
private function changeManor(e:Event):void
{
var manor :int = _area.mx_internal::getTextField().numLines;
_area.height = manor * _styManager.getSize() * 2;
if(( _area.height + _area.y - _styManager.getSize() * manor + 8) > _dis .height)
{
_area.maxChars = _area.length ;
}
}
}
}
posted on 2011-07-26 11:32 破阵子 . 如是我闻 阅读(1752) 评论(0) 编辑 收藏 举报