AS3 CookBook学习整理(七)
2009-03-23 17:56
363 查看
1.调整文本框大小以适应内容
解决方法:
设置autoSize属性可自动根据内容调整文本框大小。可用值为RIGHT, LEFT, CENTER,和NONE,都是flash.text.TextFieldAutoSize 类常量。
默认值为NONE,表示不自动调整大小。 另外,wordWrap属性设置为true,则当内容超出范围时自动换行。貌似必须先设置autoSize才生效。
Example:
2.滚动文本
解决方法:
水平滚动的单位为像素(最小值为0),垂直滚动的单位为行(最小值为1)。设置WheelEnabled=false可以禁用滚轮滚动行。 scrollH -- 水平滚动的像素
maxScrollH -- 文本的最大水平像素值,只读
scrollV -- 垂直滚动的行数
maxScrollV -- 文本的最大行数,只读
bottomScrollV -- 当前最后一行是第几行(在整个行数里),只读
Example:
3.响应滚动事件
解决方法:
当水平或垂直滚动产生时会触发scroll事件,flash.events.Event类的SCROLL常量即代表该事件。
Example:
4.格式化文本框文本
解决方法:
有三种方式对文本进行格式化:HTML标签格式化、使用TextFormat对象、CSS样式
* HTML标签格式化示例: textBox.htmlText = "<b>粗体文本</b><u>下划线文本</u>";
* TextFormat对象示例:(注意:只对setTextFormat之前的代码有效)
var textFormat:TextFormat = new TextFormat();
textFormat.color = 0xFF0000;
textFormat.htmlText = "有效";
//也可以对部分文字进行格式化,例如:textBox.setTextFormat(textFormat,3,textBox.text.length);
textBox.setTextFormat(textFormat); textFormat.htmlText = "无效";
* CSS样式示例:(注意:只对CSS定义之后的代码有效)
var css:StyleSheet = new StyleSheet();
var styleObj:Object = {color:"#FF0000"};
css.setStyle(".stdStyle",styleObj);
textBox.styleSheet = css;
textBox.htmlText = "不潮不用花钱";
this.addChild(textBox);
Example:
5.格式化用户输入的文本
解决方法:
应用TextFormat对象到文本框的defaultTextFormat属性上
Example:
6.设置文本字体
解决方法:
使用HTML的<font>标签,或者设置TextFormat对象的font属性,或者通过CSS的font-family属性
修改字体有多种方法,如果使用HTML的话可通过<font> 标签更改:field.htmlText = "<font face='Arial'>Formatted text</font>";
也可设置TextFormat对象的font属性:formatter.font = "Arial";
或者在CSS中定义font-family 属性:p {font-family: Arial;}
需要注意的是电脑中必须要有你所指定的字体,因为有些电脑上可能没有安装相应的字体,这是可指定多种字体:formatter.font = "Arial, Verdana, Helvetica";
如果都没有指定字体,默认使用系统字体。
另外我们还可使用字体组,字体组是系统默认字体的一个分类,有三种: _sans, _serif, 和_typewriter。
_sans 组包含如Arial 或Helvetica,_serif组包含如Times 或Times New Roman,_typewriter 组包含如Courier 或Courier New
解决方法:
设置autoSize属性可自动根据内容调整文本框大小。可用值为RIGHT, LEFT, CENTER,和NONE,都是flash.text.TextFieldAutoSize 类常量。
默认值为NONE,表示不自动调整大小。 另外,wordWrap属性设置为true,则当内容超出范围时自动换行。貌似必须先设置autoSize才生效。
Example:
package { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFieldType; public class Sample0323 extends Sprite { public function Sample0323() { var text:TextField = new TextField(); text.type = TextFieldType.INPUT; text.border = true; text.borderColor = 0xFFFF00; text.height = 20; text.autoSize = TextFieldAutoSize.CENTER; text.wordWrap = true; this.addChild(text); } } }
2.滚动文本
解决方法:
水平滚动的单位为像素(最小值为0),垂直滚动的单位为行(最小值为1)。设置WheelEnabled=false可以禁用滚轮滚动行。 scrollH -- 水平滚动的像素
maxScrollH -- 文本的最大水平像素值,只读
scrollV -- 垂直滚动的行数
maxScrollV -- 文本的最大行数,只读
bottomScrollV -- 当前最后一行是第几行(在整个行数里),只读
Example:
package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFieldType; public class Sample0323 extends Sprite { private var textBox:TextField; private var textInfo:TextField; public function Sample0323() { textBox = new TextField(); textBox.type = TextFieldType.INPUT; textBox.width = 250; textBox.height = 100; textBox.border = true; textBox.borderColor = 0xFFFF00; textBox.background = true; textBox.backgroundColor = 0xFFFFFF; //测试水平滚动则为false,垂直滚动则为true; textBox.wordWrap = true; this.addChild(textBox); var label:TextField = new TextField(); label.x = 200; label.y = 100; label.background = true; label.backgroundColor = 0xCCCCCC; label.autoSize = TextFieldAutoSize.CENTER; label.text = "水平滚动10个像素"; label.addEventListener(MouseEvent.CLICK,scrollH_onClick); this.addChild(label); label = new TextField(); label.x = 200; label.y = 150; label.background = true; label.backgroundColor = 0xCCCCCC; label.autoSize = TextFieldAutoSize.CENTER; label.text = "水平滚动至结尾"; label.addEventListener(MouseEvent.CLICK,maxScrollH_onClick); this.addChild(label); label = new TextField(); label.x = 200; label.y = 200; label.background = true; label.backgroundColor = 0xCCCCCC; label.autoSize = TextFieldAutoSize.CENTER; label.text = "垂直滚动3行"; label.addEventListener(MouseEvent.CLICK,scrollV_onClick); this.addChild(label); label = new TextField(); label.x = 200; label.y = 250; label.background = true; label.backgroundColor = 0xCCCCCC; label.autoSize = TextFieldAutoSize.CENTER; label.text = "垂直滚动至结尾"; label.addEventListener(MouseEvent.CLICK,maxScrollV_onClick); this.addChild(label); textInfo = new TextField(); textInfo.type = TextFieldType.INPUT; textInfo.y = 300; textInfo.autoSize = TextFieldAutoSize.CENTER; this.addChild(textInfo); } private function scrollH_onClick(event:MouseEvent):void { textBox.scrollH += 10; } private function maxScrollH_onClick(event:MouseEvent):void { textBox.scrollH = textBox.maxScrollH; } private function scrollV_onClick(event:MouseEvent):void { textBox.scrollV += 3; textInfo.text = "当前最后一行在整个行数里是第"+textBox.bottomScrollV+"行"; } private function maxScrollV_onClick(event:MouseEvent):void { textBox.scrollV = textBox.maxScrollV; } } }
3.响应滚动事件
解决方法:
当水平或垂直滚动产生时会触发scroll事件,flash.events.Event类的SCROLL常量即代表该事件。
Example:
package { import flash.display.Sprite; import flash.events.Event; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFieldType; import flash.utils.getTimer; public class Sample0323 extends Sprite { private var textBox:TextField; private var textInfo:TextField; public function Sample0323() { textBox = new TextField(); textBox.type = TextFieldType.INPUT; textBox.width = 250; textBox.height = 100; textBox.border = true; textBox.borderColor = 0xFFFF00; textBox.background = true; textBox.backgroundColor = 0xFFFFFF; textBox.wordWrap = true; textBox.addEventListener(Event.SCROLL,onScroll); this.addChild(textBox); textInfo = new TextField(); textInfo.type = TextFieldType.INPUT; textInfo.y = 300; textInfo.autoSize = TextFieldAutoSize.CENTER; this.addChild(textInfo); } private function onScroll(event:Event):void { textInfo.text = flash.utils.getTimer()+" -- 滚动了"; } } }
4.格式化文本框文本
解决方法:
有三种方式对文本进行格式化:HTML标签格式化、使用TextFormat对象、CSS样式
* HTML标签格式化示例: textBox.htmlText = "<b>粗体文本</b><u>下划线文本</u>";
* TextFormat对象示例:(注意:只对setTextFormat之前的代码有效)
var textFormat:TextFormat = new TextFormat();
textFormat.color = 0xFF0000;
textFormat.htmlText = "有效";
//也可以对部分文字进行格式化,例如:textBox.setTextFormat(textFormat,3,textBox.text.length);
textBox.setTextFormat(textFormat); textFormat.htmlText = "无效";
* CSS样式示例:(注意:只对CSS定义之后的代码有效)
var css:StyleSheet = new StyleSheet();
var styleObj:Object = {color:"#FF0000"};
css.setStyle(".stdStyle",styleObj);
textBox.styleSheet = css;
textBox.htmlText = "不潮不用花钱";
this.addChild(textBox);
Example:
package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.net.URLLoader; import flash.net.URLRequest; import flash.text.StyleSheet; import flash.text.TextField; import flash.text.TextFieldAutoSize; public class Sample0324 extends Sprite { private var label:TextField; private var text:String = "here comes <span class='stdText'>wayne</span>"; public function Sample0324() { var btnRed:TextField = new TextField(); btnRed.text = "应用红色样式"; btnRed.y = 100; btnRed.addEventListener(MouseEvent.CLICK,onRedClick); this.addChild(btnRed); var btnGreen:TextField = new TextField(); btnGreen.text = "应用绿色样式"; btnGreen.y = 200; btnGreen.addEventListener(MouseEvent.CLICK,onGreenClick); this.addChild(btnGreen); label = new TextField(); label.autoSize = TextFieldAutoSize.CENTER; label.htmlText = text; this.addChild(label); } private function onRedClick(event:MouseEvent):void { var loader:URLLoader = new URLLoader(); loader.load(new URLRequest("StdStyle.css")); loader.addEventListener(Event.COMPLETE,onLoadComplete); } private function onGreenClick(event:MouseEvent):void { var loader:URLLoader = new URLLoader(); loader.load(new URLRequest("StdStyle2.css")); loader.addEventListener(Event.COMPLETE,onLoadComplete); } private function onLoadComplete(event:Event):void { var css:StyleSheet = new StyleSheet(); css.parseCSS((event.target as URLLoader).data); label.styleSheet = css; label.htmlText = text; } } }
5.格式化用户输入的文本
解决方法:
应用TextFormat对象到文本框的defaultTextFormat属性上
Example:
package { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldType; import flash.text.TextFormat; public class Sample0324 extends Sprite { public function Sample0324() { var textBox:TextField = new TextField(); textBox.type = TextFieldType.INPUT; textBox.border = true; textBox.borderColor = 0xFFFF00; var textFormat:TextFormat = new TextFormat(); textFormat.color = 0xFF0000; textFormat.italic = true; textBox.defaultTextFormat = textFormat; this.addChild(textBox); } } }
6.设置文本字体
解决方法:
使用HTML的<font>标签,或者设置TextFormat对象的font属性,或者通过CSS的font-family属性
修改字体有多种方法,如果使用HTML的话可通过<font> 标签更改:field.htmlText = "<font face='Arial'>Formatted text</font>";
也可设置TextFormat对象的font属性:formatter.font = "Arial";
或者在CSS中定义font-family 属性:p {font-family: Arial;}
需要注意的是电脑中必须要有你所指定的字体,因为有些电脑上可能没有安装相应的字体,这是可指定多种字体:formatter.font = "Arial, Verdana, Helvetica";
如果都没有指定字体,默认使用系统字体。
另外我们还可使用字体组,字体组是系统默认字体的一个分类,有三种: _sans, _serif, 和_typewriter。
_sans 组包含如Arial 或Helvetica,_serif组包含如Times 或Times New Roman,_typewriter 组包含如Courier 或Courier New
相关文章推荐
- AS3 CookBook学习整理(五)
- AS3 CookBook学习整理(五)
- AS3 CookBook学习整理(十三)
- AS3 CookBook学习整理(六)
- AS3 CookBook学习整理(四)
- AS3 CookBook学习整理(七)
- AS3 CookBook学习整理(十四)
- AS3 CookBook学习整理文章索引
- AS3 CookBook学习整理(一)
- AS3 CookBook学习整理(八)
- AS3 CookBook学习整理(十五)
- AS3 CookBook学习整理(一)
- AS3 CookBook学习整理(九)
- AS3 CookBook学习整理(二)
- AS3 CookBook学习整理(六)
- AS3 CookBook学习整理(二)
- AS3 CookBook学习整理(十)
- AS3 CookBook学习整理(十六)
- AS3 CookBook学习整理(三)
- AS3 CookBook学习整理(十一)