2019-1-9 zhulinan Flash
在Air应用中为动态文本设置文本格式,源码如下:
package { import flash.events.*; import flash.display.MovieClip; import flash.display.Loader; import flash.display.LoaderInfo; import flash.text.TextField; import flash.text.TextFieldType; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.text.Font; import flash.net.URLRequest; import flash.system.ApplicationDomain; import flash.utils.getDefinitionByName; import fl.controls.ColorPicker; //此行不能省略且colorPicker组件要添加到库里 import fl.controls.ComboBox; public class Main extends MovieClip { private var font1Cls:Class; private var font2Cls:Class; private var font3Cls:Class; public function Main() { addEventListener(Event.ADDED_TO_STAGE, addedHandler); } private function addedHandler(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE, addedHandler); var loader:Loader = new Loader(); var url:URLRequest = new URLRequest("fonts.swf"); //加载字体文件 loader.load(url); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandle); addEventListener(Event.REMOVED_FROM_STAGE, removedHandler); } private function loaderCompleteHandle(e:Event):void { var swfTarget:LoaderInfo = e.target as LoaderInfo; var appDomain:ApplicationDomain = swfTarget.applicationDomain as ApplicationDomain; font1Cls = appDomain.getDefinition("songti") as Class; //宋体 Font.registerFont(font1Cls); font2Cls = appDomain.getDefinition("lishu") as Class; //隶书 Font.registerFont(font2Cls); font3Cls = appDomain.getDefinition("yahei") as Class; //微软雅黑 Font.registerFont(font3Cls); myCp.addEventListener(Event.CHANGE, colorChangeHandler); myCb.setSize(100, 22); myCb.addItem({label: "宋体", data:"font1"}); myCb.addItem({label: "隶书", data:"font2"}); myCb.addItem({label: "微软雅黑", data:"font3"}); myCb.addEventListener(Event.CHANGE, fontChangeHandler); var myFont:Font = new font1Cls() as Font; var format:TextFormat = new TextFormat(); format.font = myFont.fontName; format.color = 0xff0000; format.size = 16; txt1.defaultTextFormat = format; txt1.text = "中国\r北京\r海淀\r上地\r嘉华大厦"; txt1.setTextFormat(format); //txt1.embedFonts = true; //反锯齿对微软雅黑无效,原因未知 txt1.addEventListener(FocusEvent.FOCUS_OUT, textFocusOutHandler); btnSetting.addEventListener(MouseEvent.CLICK, btnSettingClickHandler); } private function colorChangeHandler(e:Event):void { var cp:ColorPicker = e.target as ColorPicker; var format:TextFormat = new TextFormat(); format = txt1.getTextFormat(); format.color = cp.selectedColor; txt1.setTextFormat(format, txt1.selectionBeginIndex, txt1.selectionEndIndex); } private function fontChangeHandler(e:Event):void { var cb:ComboBox = e.target as ComboBox; var myFont:Font; if (cb.selectedItem.data == "font1") { myFont = new font1Cls() as Font; } else if (cb.selectedItem.data == "font2") { myFont = new font2Cls() as Font; } else if (cb.selectedItem.data == "font3") { myFont = new font3Cls() as Font; } var format:TextFormat = new TextFormat(); format = txt1.getTextFormat(); format.font = myFont.fontName; txt1.setTextFormat(format, txt1.selectionBeginIndex, txt1.selectionEndIndex); } private function textFocusOutHandler(e:FocusEvent):void { stage.focus = txt1; //让文本不失去焦点 } private function btnSettingClickHandler(e:MouseEvent):void { //获取txt1文本格式信息 字体、颜色 var str:String = txt1.text; var len:uint = str.length; if (len == 0) return; var format1:TextFormat = txt1.getTextFormat(0, 1); var format2:TextFormat; var begin:uint = 0; var end:uint; str = ""; for (var i:uint = 1; i < len; i++) { format2 = txt1.getTextFormat(i, i + 1); if (format2.font != format1.font || format2.color != format1.color) { end = i; if (str == "") { str = '{"begin":' + String(begin) + ',"end":' + String(end) + ',"font":"' + format1.font + '","color":"' + format1.color + '"}'; } else { str += ',{"begin":' + String(begin) + ',"end":' + String(end) + ',"font":"' + format1.font + '","color":"' + format1.color + '"}'; } begin = end; format1 = format2; } } end = len; if (str == "") { str = '{"begin":' + String(begin) + ',"end":' + String(end) + ',"font":"' + format1.font + '","color":"' + format1.color + '"}'; } else { str += ',{"begin":' + String(begin) + ',"end":' + String(end) + ',"font":"' + format1.font + '","color":"' + format1.color + '"}'; } str = '[' + str + ']'; //为txt2设置文本格式 txt2.text = txt1.text; var arr:* = JSON.parse(str); for each(var k in arr) { format1 = new TextFormat(); format1.font = k.font; format1.color = k.color; format1.size = 16; txt2.setTextFormat(format1, k.begin, k.end); } trace(str); } private function removedHandler(e:Event):void { // none } } }
运行界面:
源码(不含字体文件fonts.swf)下载:241.zip
字体文件fonts.swf太大了,241.zip文件里没包含字体文件。如何生成字体文件请参考“Flash CS6为动态文本设置可消除锯齿的外部字体”这篇文章。