本文运用flash 组件编写一个简单的控制文字效果的程序,所使用到的组件分别为TextArea(输入文字)、Button(提交TextArea内输入的文字,让TextField显示)、Slider(控制字体TextFiled的大小,引入import fl.controls.ColorPicker;)、ComboBox(控制文字TextFiled的字体)、ColorPicker(控制文字TextFiled的颜色,引入import fl.events.ColorPickerEvent;);下面是效果。
( 动画演示结果 )
下面介绍具体编写的具体步骤
第一步,先写入场景中一个显示文字的TextFiled,addChild 将加入到场景,setChildIndex 值为 0,将置最底层,下面是详细代码。
//(C) Copyright weisim3.com 09.27.2010
var mytxt:TextField=new TextField();
mytxt.text="HI,This is my text!!";
mytxt.width=550;//文字显示宽度
mytxt.wordWrap=true;//换行
//mytxt.textColor=0x999999;//颜色字体
![]()
var newFormat:TextFormat = new TextFormat();
//newFormat.font="宋体";//字体
addChild(mytxt);
setChildIndex(mytxt,0);//置底层
mytxt.setTextFormat(newFormat);
mytxt.autoSize=TextFieldAutoSize.LEFT;
第二步,加入Slider 组建同时引入 import fl.events.SliderEvent;,让SIider 实现控制文字大小,代码如下
import fl.events.SliderEvent;
import fl.events.ColorPickerEvent;
![]()
//(C) Copyright weisim3.com 09.27.2010
![]()
var mytxt:TextField=new TextField();
mytxt.text="HI,This is my text!!";
mytxt.width=550;
mytxt.wordWrap=true;//换行
//mytxt.textColor=0x999999;
![]()
var newFormat:TextFormat = new TextFormat();
//newFormat.font="宋体";
addChild(mytxt);
setChildIndex(mytxt,0);//置底层
mytxt.setTextFormat(newFormat);
mytxt.autoSize=TextFieldAutoSize.LEFT;
![]()
//第二步
![]()
//handControl.value=20;
newFormat.size=20;//设置默认字体显示为20
handControl.value= (Number)(newFormat.size)-5;//下面以5进制为的单位减去5
handControl.setSize(200,50);//silder 大小宽度
handControl.snapInterval=5;//移动以5进制
handControl.tickInterval=5;//尺码显示和snapInterval对应
handControl.liveDragging=true;
![]()
![]()
![]()
sliderText.text=(String)(handControl.value);//sliderText显示slider滑动的值
![]()
handControl.addEventListener(SliderEvent.CHANGE,controlSize);
function controlSize(event) {
![]()
sliderText.text=(String)(handControl.value);
newFormat.size=handControl.value;
mytxt.setTextFormat(newFormat);
mytxt.autoSize=TextFieldAutoSize.LEFT;
}
下面依次实现ColorPicker(引入import fl.events.ColorPickerEvent;)颜色拾取控制文字颜色,ComboBox控制文字字体,以及提交更新文字,如下ComboBox图例,以及全部代码。
(图为 ComboBox 的值)
import fl.events.SliderEvent;
import fl.events.ColorPickerEvent;
![]()
//(C) Copyright weisim3.com 09.27.2010
![]()
var mytxt:TextField=new TextField();
mytxt.text="HI,This is my text!!";
mytxt.width=550;
mytxt.wordWrap=true;//换行
//mytxt.textColor=0x999999;
![]()
var newFormat:TextFormat = new TextFormat();
//newFormat.font="宋体";
addChild(mytxt);
setChildIndex(mytxt,0);//置底层
mytxt.setTextFormat(newFormat);
mytxt.autoSize=TextFieldAutoSize.LEFT;
![]()
//第二步
![]()
//handControl.value=20;
newFormat.size=20;//设置默认字体显示为20
handControl.value= (Number)(newFormat.size)-5;//下面以5进制为的单位减去5
handControl.setSize(200,50);//silder 大小宽度
handControl.snapInterval=5;//移动以5进制
handControl.tickInterval=5;//尺码显示和snapInterval对应
handControl.liveDragging=true;
![]()
![]()
![]()
sliderText.text=(String)(handControl.value);//sliderText显示slider滑动的值
![]()
handControl.addEventListener(SliderEvent.CHANGE,controlSize);//响应SliderEvent.CHANGE事件
function controlSize(event){
![]()
sliderText.text=(String)(handControl.value);
newFormat.size=handControl.value;// 获取的Slider的,文字的大小
mytxt.setTextFormat(newFormat);
mytxt.autoSize=TextFieldAutoSize.LEFT;
}
![]()
![]()
colorControl.addEventListener(ColorPickerEvent.CHANGE,colorset);//响应ColorPickerEvent.CHANGE 事件
function colorset(event):void {
colorText.text="#"+(String)(colorControl.selectedColor);
mytxt.textColor=colorControl.selectedColor;//文字色彩,获取colorPicker的值
}
![]()
PutIn_btn.addEventListener(MouseEvent.CLICK,txtUpdate);
function txtUpdate(e:Event):void {
//TextArea 为空时不进行处理,不提交文字
if (txtIN_txt.text!="") {
mytxt.text=txtIN_txt.text;
sliderText.text=(String)(handControl.value);
newFormat.size=handControl.value;
mytxt.setTextFormat(newFormat);
mytxt.autoSize=TextFieldAutoSize.LEFT;
} else {
txtIN_txt.setStyle("borderColor",0xFFFF00);
//txtIN_txt.borderColor
}
}
![]()
ComFont.addEventListener(Event.CHANGE,txtFont);
function txtFont(e:Event):void {
//文字的字体 ComboBox选取为 "字体" 则无效,不发生改变
fontText.text=ComFont.selectedItem.data;
if (ComFont.selectedItem.data!="字体") {
newFormat.font=ComFont.selectedItem.data;
mytxt.setTextFormat(newFormat);
mytxt.autoSize=TextFieldAutoSize.LEFT;
trace(ComFont.selectedItem.data);
}
}
( 程序展示 )