AS3 FLASH文本编辑器
重要代码:
package {
import fl.controls.ColorPicker;
import fl.controls.ComboBox;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.EventDispatcher;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.Font;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.ui.Mouse;
import flash.utils.setTimeout;
/**
* ...
* @author 沙子 QQ:8286659 http://www.taoshaw.com
*/
public class main extends MovieClip {
//输入文本框
private var my_text:TextField;
//加粗按钮
private var my_btn_bold:mc_bold;
//倾斜按钮
private var my_btn_itlic:mc_itlic;
//居左
private var my_btn_left:mc_auto_left;
//居中
private var my_btn_middle:mc_auto_middle;
//居右
private var my_btn_right:mc_auto_right;
//下划线
private var my_btn_xiahuaxian:mc_xiahuaxian;
//超链接
private var my_btn_url:mc_url;
//显示超链接输入对话框
private var my_btn_url_shuru:mc_url_shuru;
//图片
private var my_btn_pic:mc_pic;
//图片输入框
private var my_btn_pic_shuru:mc_pic_shuru;
//字体大小
private var my_btn_size:ComboBox;
//字体颜色
private var my_Col:ColorPicker;
//字号
private var font_Arr:Array;
//字体
private var my_btn_font:ComboBox;
//获取字体数组
private var my_Arr_font:Array;
//设置字体下拉框的样式
private var linkTextFormat:TextFormat;
//初始化开始选择索引跟结束选择索引
private var my_Beginfocus:int=-1;
private var my_Endfocus:int=-1;
private var my_TextFormat:TextFormat;
//显示所有对象
private var my_Sprite:Sprite;
public function main() {
my_text = new TextField();
my_btn_bold = new mc_bold();
my_btn_itlic = new mc_itlic();
my_btn_left = new mc_auto_left();
my_btn_middle = new mc_auto_middle();
my_btn_right = new mc_auto_right();
my_btn_xiahuaxian = new mc_xiahuaxian();
my_btn_url = new mc_url();
my_btn_url_shuru = new mc_url_shuru();
my_btn_pic = new mc_pic();
my_btn_pic_shuru = new mc_pic_shuru();
my_btn_size = new ComboBox();
my_Col = new ColorPicker();
my_TextFormat = new TextFormat();
my_Sprite = new Sprite();
font_Arr = new Array();
//获取系统可用字体
my_Arr_font = Font.enumerateFonts(true);
linkTextFormat = new TextFormat();
my_btn_font = new ComboBox();
my_Sprite.addChildAt(my_text, 0);
my_Sprite.addChildAt(my_btn_bold, 1);
my_Sprite.addChildAt(my_btn_itlic, 2);
my_Sprite.addChildAt(my_btn_size, 3);
my_Sprite.addChildAt(my_Col, 4);
my_Sprite.addChildAt(my_btn_font, 5);
my_Sprite.addChildAt(my_btn_left, 6);
my_Sprite.addChildAt(my_btn_middle, 7);
my_Sprite.addChildAt(my_btn_right, 8);
my_Sprite.addChildAt(my_btn_xiahuaxian, 9);
my_Sprite.addChildAt(my_btn_url, 10);
my_Sprite.addChildAt(my_btn_pic, 11);
addChild(my_Sprite);
//显示文本框
_showTextField();
//显示加粗按钮
_showBtn_Bold();
//显示倾斜按钮
_showBtn_itlic();
//显示居左
_showBtn_left();
//显示居中
_showBtn_middle();
//显示居右
_showBtn_right();
//显示下划线按钮
_showBtn_xiahuaxian();
//显示超链接按钮
_showBtn_url();
//显示插入图片按钮
_showBtn_pic();
//显示字号
_showBtn_size();
//显示颜色组件
_showBtn_Col();
//显示字体下拉框
_showBtn_font();
//字体下拉框样式
_styleCombobox();
}
//字体下拉框样式
private function _styleCombobox():void {
linkTextFormat.font = "Arial";
linkTextFormat.size = 12;
//linkTextFormat.bold = true;
linkTextFormat.color = 0x171717;
}
//加粗
private function _TextFormat_bold():void {
my_TextFormat.bold=! my_TextFormat.bold;
}
//倾斜
private function _TextFormat_italic():void {
my_TextFormat.italic = !my_TextFormat.italic;
}
//居左
private function _TextFormat_auto_left():void {
my_TextFormat.align = "left";
}
//居中
private function _TextFormat_auto_middle():void {
my_TextFormat.align="center";
}
//居右
private function _TextFormat_auto_right():void {
my_TextFormat.align="right";
}
//下划线
private function _TextFormat_xiahuaxian():void {
my_TextFormat.underline = !my_TextFormat.underline;
}
//超链接
private function _TextFormat_url(e:MouseEvent):void {
//my_TextFormat.url="http://www.taoshaw.com"
my_TextFormat.url = my_btn_url_shuru.url_txt.text;
my_Sprite.removeChildAt(11);
}
//取消超链接
private function _TextFormat_Clearurl(e:MouseEvent):void {
my_Sprite.removeChildAt(11);
my_btn_url_shuru.url_txt.text = "";
}
//插入图片
private function _TextFormat_pic(e:MouseEvent):void {
my_text.htmlText+="<img src='/UploadTeach/200812/20081205174844162.jpg'><br>"
//my_TextFormat.url = my_btn_url_shuru.url_txt.text;
//trace("请插入图片")
my_Sprite.removeChildAt(12);
}
//取消插入图片对话框
private function _TextFormat_Clearpic(e:MouseEvent):void {
my_Sprite.removeChildAt(12);
my_btn_pic_shuru.url_txt.text = "";
}
//设置拖曳
private function stopDrags(e:MouseEvent):void {
e.currentTarget.stopDrag();
}
private function startDrags(e:MouseEvent):void {
e.currentTarget.startDrag();
}
//显示加粗按钮
private function _showBtn_Bold():void {
my_btn_bold.x=35;
my_btn_bold.y=25;
my_btn_bold.addEventListener(MouseEvent.ROLL_OVER, changeNext);
my_btn_bold.addEventListener(MouseEvent.ROLL_OUT, changeCurrent);
my_btn_bold.addEventListener(MouseEvent.CLICK, _setTextFormat);
}
//显示字号按钮
private function _showBtn_size():void {
my_btn_size.x = 90;
my_btn_size.y = 23;
my_btn_size.width = 70;
font_Arr = [12, 14, 16, 18, 20, 22, 24, 32, 36, 36,40,48, 60,64,68,74,80,84,96, 120];
for (var i:uint = 0; i < font_Arr.length; i++) {
my_btn_size.addItem({label:font_Arr,data:font_Arr});
}
my_btn_size.addEventListener(Event.CHANGE,_setTextFormatNext);
}
//显示倾斜按钮
private function _showBtn_itlic():void {
my_btn_itlic.x = 60;
my_btn_itlic.y = 25;
my_btn_itlic.addEventListener(MouseEvent.ROLL_OVER, changeNext);
my_btn_itlic.addEventListener(MouseEvent.ROLL_OUT, changeCurrent);
my_btn_itlic.addEventListener(MouseEvent.CLICK, _setTextFormat);
}
//显示颜色按钮
private function _showBtn_Col():void {
my_Col.x = 197;
my_Col.y = 22;
my_Col.addEventListener(Event.CLOSE,_setTextFormatNext);
}
//显示字体下拉框
private function _showBtn_font():void {
my_btn_font.x = 230;
my_btn_font.y = 22;
for (var i:uint = 0; i < my_Arr_font.length; i++) {
//trace(my_Arr_font.fontName)
my_btn_font.addItem({label:my_Arr_font.fontName});
}
my_btn_font.textField.setStyle("textFormat", linkTextFormat);
my_btn_font.dropdown.setRendererStyle("textFormat", linkTextFormat);
my_btn_font.width = 115;
my_btn_font.addEventListener(Event.CHANGE,_setTextFormatNext);
}
//显示居左按钮
private function _showBtn_left():void {
my_btn_left.x = 355;
my_btn_left.y = 25;
my_btn_left.addEventListener(MouseEvent.ROLL_OVER, changeNext);
my_btn_left.addEventListener(MouseEvent.ROLL_OUT, changeCurrent);
my_btn_left.addEventListener(MouseEvent.CLICK, _setTextFormat);
}
//显示居中按钮
private function _showBtn_middle():void {
my_btn_middle.x = 380;
my_btn_middle.y = 25;
my_btn_middle.addEventListener(MouseEvent.ROLL_OVER, changeNext);
my_btn_middle.addEventListener(MouseEvent.ROLL_OUT, changeCurrent);
my_btn_middle.addEventListener(MouseEvent.CLICK, _setTextFormat);
}
//显示居右按钮
private function _showBtn_right():void {
my_btn_right.x = 405;
my_btn_right.y = 25;
my_btn_right.addEventListener(MouseEvent.ROLL_OVER, changeNext);
my_btn_right.addEventListener(MouseEvent.ROLL_OUT, changeCurrent);
my_btn_right.addEventListener(MouseEvent.CLICK, _setTextFormat);
}
//显示下划线按钮
private function _showBtn_xiahuaxian():void {
my_btn_xiahuaxian.x = 430;
my_btn_xiahuaxian.y = 25;
my_btn_xiahuaxian.addEventListener(MouseEvent.ROLL_OVER, changeNext);
my_btn_xiahuaxian.addEventListener(MouseEvent.ROLL_OUT, changeCurrent);
my_btn_xiahuaxian.addEventListener(MouseEvent.CLICK, _setTextFormat);
}
//显示超链接按钮
private function _showBtn_url():void {
my_btn_url.x = 455;
my_btn_url.y = 25;
my_btn_url.addEventListener(MouseEvent.ROLL_OVER, changeNext);
my_btn_url.addEventListener(MouseEvent.ROLL_OUT, changeCurrent);
my_btn_url.addEventListener(MouseEvent.CLICK, _setTextFormat);
}
//显示插入图片按钮
private function _showBtn_pic():void {
my_btn_pic.x = 170;
my_btn_pic.y = 25;
my_btn_pic.addEventListener(MouseEvent.ROLL_OVER, changeNext);
my_btn_pic.addEventListener(MouseEvent.ROLL_OUT, changeCurrent);
my_btn_pic.addEventListener(MouseEvent.CLICK, _setTextFormat);
}
//设置样式(A)
private function _setTextFormat(e:MouseEvent):void {
switch (e.currentTarget) {
case my_btn_bold :
_TextFormat_bold();
break;
case my_btn_itlic :
_TextFormat_italic();
break;
case my_btn_left :
_TextFormat_auto_left();
break;
case my_btn_middle :
_TextFormat_auto_middle();
break;
case my_btn_right :
_TextFormat_auto_right();
break;
case my_btn_xiahuaxian :
_TextFormat_xiahuaxian();
break;
case my_btn_url :
my_btn_url_shuru.x = 200;
my_btn_url_shuru.y = 130;
my_btn_url_shuru.ok.addEventListener(MouseEvent.CLICK, _TextFormat_url);
my_btn_url_shuru.cancel.addEventListener(MouseEvent.CLICK, _TextFormat_Clearurl);
my_btn_url_shuru.addEventListener(MouseEvent.MOUSE_DOWN, startDrags);
my_btn_url_shuru.addEventListener(MouseEvent.MOUSE_UP, stopDrags);
my_Sprite.addChildAt(my_btn_url_shuru,11);
break;
case my_btn_pic :
my_btn_pic_shuru.x = 200;
my_btn_pic_shuru.y = 130;
my_btn_pic_shuru.ok.addEventListener(MouseEvent.CLICK, _TextFormat_pic);
my_btn_pic_shuru.cancel.addEventListener(MouseEvent.CLICK, _TextFormat_Clearpic);
my_btn_pic_shuru.addEventListener(MouseEvent.MOUSE_DOWN, startDrags);
my_btn_pic_shuru.addEventListener(MouseEvent.MOUSE_UP, stopDrags);
my_Sprite.addChildAt(my_btn_pic_shuru,12);
break;
}
my_text.setTextFormat(my_TextFormat, my_Beginfocus, my_Endfocus);
}
//设置样式(B)
private function _setTextFormatNext(e:Event):void {
switch (e.currentTarget) {
case my_btn_size :
my_TextFormat.size = my_btn_size.selectedItem.data;
break;
case my_Col :
my_TextFormat.color = my_Col.selectedColor;
break;
case my_btn_font :
my_TextFormat.font = my_btn_font.selectedLabel;
break;
}
my_text.setTextFormat(my_TextFormat, my_Beginfocus, my_Endfocus);
}
//加粗按钮跟倾斜按钮的元件,有一个鼠标滑过效果
private function changeCurrent(e:MouseEvent):void {
MovieClip(e.currentTarget).gotoAndStop(1);
}
private function changeNext(e:MouseEvent):void {
MovieClip(e.currentTarget).gotoAndStop(2);
}
//显示输入文本框
private function _showTextField():void {
my_text.x=35;
my_text.y=50;
my_text.width=470;
my_text.height=330;
//输入文本类型
my_text.type=TextFieldType.INPUT;
my_text.text="欢迎光临淘沙网 http://www.taoshaw.com ";
my_text.border=true;
//自动换行
my_text.wordWrap=true;
//多行文本
my_text.multiline=true;
my_text.borderColor = 0xcccccc;
my_text.addEventListener(MouseEvent.CLICK,_getjiaodian);
}
//获取选择文本
private function _getjiaodian(e:MouseEvent):void {
my_Beginfocus=my_text.selectionBeginIndex;
my_Endfocus=my_text.selectionEndIndex;
}
}
}
补充:flash教程,动画技术