标签:as as3.0 actionscript 开发技术 textfield
下面是封装了一个可设置垂直居中及左右居中的Label,同时可以设置label中的字体样式。代码如下:
package myComponent { import flash.display.Shape; import flash.display.Sprite; import flash.text.StyleSheet; import flash.text.TextField; import flash.text.TextLineMetrics; /** * ... * @author liang */ public class MyLabel extends Sprite { private var _labelText:TextField = new TextField();//记录文本 private var _backgroundColor:uint = 0xFFFFFF;//sprite的背景颜色 private var _myHeight:uint = 27;//MyLabel的高度 private var _myWidth:uint = 60;//MyLabel的宽度 private var _myShape:Shape;//用于创建一个矩形 private var _backgroundAlpha:uint = 1;//背景的透明度 private var _text:String = ‘label‘;//label的文本 private var _fontSize:int = 15;//字体大小 private var _fontFamily:String = ‘‘;//字体系列名 private var _fontColor:String = ‘#000000‘;//字体颜色 private var _fontDisplay:String = ‘block‘; //label内容以块的方式显示 private var _textAlign:String = ‘center‘;//label内容位置显示方式 private var _fontStyle:String = ‘normal‘;//label中文本样式 private var _fontWeight:String = ‘normal‘;//label中文本粗细 private var _styleObj:Object = new Object();//提供给外部的样式设置对象 public function MyLabel() { } public function doInitDraw():void { this.init(); } private function init():void { this._myShape = new Shape(); this._myShape.graphics.beginFill(this._backgroundColor, this._backgroundAlpha); this._myShape.graphics.drawRect(0, 0, this._myWidth, this._myHeight); this.addChild(this._myShape); var newStyle:StyleSheet = new StyleSheet(); if (!this._styleObj.fontWeight) { this._styleObj.fontWeight = this._fontWeight; } if (!this._styleObj.color) { this._styleObj.color =this._fontColor; } if (!this._styleObj.fontSize) { this._styleObj.fontSize = this._fontSize; } if (!this._styleObj.fontStyle) { this._styleObj.fontStyle = this._fontStyle; } if (!this._styleObj.display) { this._styleObj.display = this._fontDisplay; } if (!this._styleObj.textAlign) { this._styleObj.textAlign = this._textAlign; } if (!this._styleObj.fontFamily) { this._styleObj.fontFamily = this._fontFamily; } newStyle.setStyle(".defStyle", this._styleObj); this._labelText.styleSheet = newStyle; this._labelText.htmlText = ‘<span class="defStyle">‘ + this._text + ‘</span>‘; this.addChild(this._labelText); var textinfo :TextLineMetrics = this._labelText.getLineMetrics(0); this._labelText.height = textinfo.ascent + textinfo.descent + 4; this._labelText.width = this._myWidth; this._labelText.y = (this._myHeight - this._labelText.height) / 2; } public function get labelText():TextField { return _labelText; } public function set labelText(value:TextField):void { _labelText = value; } public function get backgroundColor():uint { return _backgroundColor; } public function set backgroundColor(value:uint):void { _backgroundColor = value; } public function get myHeight():uint { return _myHeight; } public function set myHeight(value:uint):void { _myHeight = value; } public function get myWidth():uint { return _myWidth; } public function set myWidth(value:uint):void { _myWidth = value; } public function get myShape():Shape { return _myShape; } public function set myShape(value:Shape):void { _myShape = value; } public function get backgroundAlpha():uint { return _backgroundAlpha; } public function set backgroundAlpha(value:uint):void { _backgroundAlpha = value; } public function get text():String { return _text; } public function set text(value:String):void { _text = value; } public function get fontSize():int { return _fontSize; } public function set fontSize(value:int):void { _fontSize = value; } public function get fontColor():String { return _fontColor; } public function set fontColor(value:String):void { _fontColor = value; } public function get fontDisplay():String { return _fontDisplay; } public function set fontDisplay(value:String):void { _fontDisplay = value; } public function get textAlign():String { return _textAlign; } public function set textAlign(value:String):void { _textAlign = value; } public function get fontStyle():String { return _fontStyle; } public function set fontStyle(value:String):void { _fontStyle = value; } public function get fontWeight():String { return _fontWeight; } public function set fontWeight(value:String):void { _fontWeight = value; } public function get styleObj():Object { return _styleObj; } public function set styleObj(value:Object):void { _styleObj = value; } } }
上面封装类的使用实例如下:
package { import flash.display.Sprite; import flash.text.TextField; import myComponent.MyLabel; [SWF(backgroundColor=‘0xFFFFFF‘,width=‘400‘,height=‘300‘,frameRate=‘29‘)] public class Main extends Sprite { private var userNameInfo:TextField; public function Main() { this.myInit(); } private function myInit():void { var mylabel:MyLabel = new MyLabel(); mylabel.x = 10; mylabel.y = 20; mylabel.myHeight = 100; mylabel.myWidth = 200; mylabel.backgroundColor = 0xcccccc; var objStyle:Object = new Object(); objStyle.fontSize = 36; objStyle.color = ‘#ff0000‘; objStyle.fontFamily = ‘Century Gothic,Microsoft yahei‘; mylabel.styleObj = objStyle; mylabel.doInitDraw(); this.addChild(mylabel); } private function doDrawInit():void { this.addChild(this.userNameInfo); } } }
实例执行结果:
说明:上面封装的Label,只是个人对TextField的小小封装,TextField用于字体的设置,包涵其的Sprite则对文字的位置进行设置,这两者相结合成了一个可个人小Label。
本文出自 “我的IT生涯” 博客,请务必保留此出处http://quietnight.blog.51cto.com/7163892/1676091
as3.0 封装一个可垂直居中及左右居中的label,并可以设置其字体样式
标签:as as3.0 actionscript 开发技术 textfield
原文地址:http://quietnight.blog.51cto.com/7163892/1676091