文本框架

    技术2022-05-19  25

    <?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"      xmlns:s="library://ns.adobe.com/flex/spark"      xmlns:mx="library://ns.adobe.com/flex/mx"      minWidth="955"      minHeight="600"      xmlns:htlf="com.htlf.*"> <s:layout>  <s:BasicLayout/> </s:layout> <fx:Declarations>  <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <htlf:TLFEditor horizontalCenter="0"     verticalCenter="0"     width="50%"     height="50%"> </htlf:TLFEditor></s:Application>  

    package com.htlf.graphic{ import flash.display.Sprite;  import mx.core.IIMESupport; import mx.core.UIComponent; import mx.managers.IFocusManagerComponent;

     public class SpriteWithIME extends UIComponent implements IIMESupport, IFocusManagerComponent {  private var _imeMode:String;  private var _tabFocusEnabled:Boolean;  private var _focusEnabled:Boolean;    public function SpriteWithIME()  {   super();  }

      public function get enableIME():Boolean  {   return true;  }

      public function get imeMode():String  {   return _imeMode;  }

      public function set imeMode(value:String):void  {   _imeMode=value;  }

      override public function get focusEnabled():Boolean  {   return true;  }

      override public function set focusEnabled(value:Boolean):void  {   focusEnabled=value;  }

      // For now! Should be dependent on Configuration.manageTabKey  override public function get tabFocusEnabled():Boolean  {   return true;  }

      override public function set tabFocusEnabled(value:Boolean):void  {   _tabFocusEnabled=value;  }

      override public function get hasFocusableChildren():Boolean  {   return false;  }

      override public function set hasFocusableChildren(value:Boolean):void  {  }

      override public function get mouseFocusEnabled():Boolean  {   return false;  }

      /*public function get tabEnabled():Boolean  {  return false;  }

      public function get tabIndex():int  {  return 0;  }*/

      override public function setFocus():void  {  }

      override public function drawFocus(isFocused:Boolean):void  {  } }}  

    package com.htlf.graphic{ import com.htlf.graphic.skins.TLFButtonSkin;  import spark.components.ToggleButton;

     public class TLFButton extends ToggleButton {  public var icon:Class;

      public function TLFButton()  {   super();//   setStyle("skinClass", TLFButtonSkin);  } }}

    <?xml version="1.0" encoding="utf-8"?><s:SkinnableContainer xmlns:fx="http://ns.adobe.com/mxml/2009"       xmlns:s="library://ns.adobe.com/flex/spark"       xmlns:mx="library://ns.adobe.com/flex/mx"> <s:layout>  <s:HorizontalLayout gap="0"/> </s:layout> <fx:Declarations> </fx:Declarations></s:SkinnableContainer>

    <?xml version="1.0" encoding="utf-8"?><s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"   xmlns:s="library://ns.adobe.com/flex/spark"   xmlns:mx="library://ns.adobe.com/flex/mx"   width="100%"   height="100%"   xmlns:graphic="com.htlf.graphic.*"   creationComplete="group1_creationCompleteHandler(event)"> <s:layout>  <s:VerticalLayout/> </s:layout>

     <fx:Script>  <![CDATA[   import com.htlf.controller.TextFlowController;      import flash.text.engine.FontWeight;      import flashx.textLayout.compose.StandardFlowComposer;   import flashx.textLayout.container.ContainerController;   import flashx.textLayout.conversion.ConversionType;   import flashx.textLayout.conversion.TextConverter;   import flashx.textLayout.edit.EditManager;   import flashx.textLayout.edit.ElementRange;   import flashx.textLayout.edit.IEditManager;   import flashx.textLayout.edit.ISelectionManager;   import flashx.textLayout.edit.SelectionFormat;   import flashx.textLayout.edit.SelectionState;   import flashx.textLayout.elements.Configuration;   import flashx.textLayout.elements.ParagraphElement;   import flashx.textLayout.elements.SpanElement;   import flashx.textLayout.elements.TextFlow;   import flashx.textLayout.events.SelectionEvent;   import flashx.textLayout.formats.BlockProgression;   import flashx.textLayout.formats.TextAlign;   import flashx.textLayout.formats.TextDecoration;   import flashx.textLayout.formats.TextLayoutFormat;   import flashx.undo.IUndoManager;   import flashx.undo.UndoManager;      import mx.controls.Alert;   import mx.events.FlexEvent;      import spark.components.ToggleButton;   import spark.events.IndexChangeEvent;

       private var _textFlow:TextFlow;

       private var undoManager:IUndoManager;   private var editorManager:IEditManager;

       public function get textFlow():TextFlow   {    return _textFlow;   }

       public function set textFlow(value:TextFlow):void   {    _textFlow=value;

        _textFlow.flowComposer=new StandardFlowComposer();    undoManager=new UndoManager();    editorManager=new EditManager(undoManager);    _textFlow.interactionManager=editorManager;    _textFlow.flowComposer.addController(new ContainerController(spriteWithIME, spriteWithIME.width, spriteWithIME.height));    _textFlow.addEventListener(SelectionEvent.SELECTION_CHANGE, onTextSelectionChange);    _textFlow.flowComposer.updateAllControllers();    dofocus();   }

       private function initializeConfiguration():void   {    var config:Configuration=TextFlow.defaultConfiguration;    config.unfocusedSelectionFormat=new SelectionFormat(0xffffff, 1.0, BlendMode.DIFFERENCE, 0xffffff, 1.0, BlendMode.DIFFERENCE, 0);    config.inactiveSelectionFormat=new SelectionFormat(0xffffff, 1.0, BlendMode.DIFFERENCE, 0xffffff, 1.0, BlendMode.DIFFERENCE, 0);    config.manageTabKey=true;        var initialFormat:TextLayoutFormat=new TextLayoutFormat();    initialFormat.fontFamily="宋体";    initialFormat.fontSize=14;    initialFormat.paddingLeft=2;    initialFormat.paddingTop=2;    initialFormat.paddingRight=2;    initialFormat.paddingBottom=2;    config.textFlowInitialFormat=initialFormat;   }

       //选择文本;   protected function onTextSelectionChange(event:SelectionEvent):void   {    // Find selected element range    var range:ElementRange=event.selectionState ? ElementRange.createElementRange(event.selectionState.textFlow, event.selectionState.absoluteStart, event.selectionState.absoluteEnd) : null;

        if (range.characterFormat.fontWeight == FontWeight.BOLD) //粗体     boldfaceBtn.selected=true;

        else     boldfaceBtn.selected=false;

        if (range.characterFormat.textDecoration == TextDecoration.UNDERLINE) //下划线     italicBtn.selected=true;    else     italicBtn.selected=false;

        if (range.characterFormat.fontStyle == FontStyle.ITALIC) //斜体;     italicBtn.selected=true;    else     italicBtn.selected=false;

       }

       //更新组件状态;   protected function updateForSelectedElementRange(selectionState:SelectionState):void   {    if (selectionState.pointFormat.fontWeight == "bold")    {     boldfaceBtn.selected=true;     italicBtn.selected=true;    }    else    {     boldfaceBtn.selected=false;     italicBtn.selected=false;    }   }

       //创建空文本流;   protected function group1_creationCompleteHandler(event:FlexEvent):void   {    //初始化配置;    initializeConfiguration();    textFlow=creatEmptyFlow();   }

       protected function creatEmptyFlow():TextFlow   {    var tFlow:TextFlow=new TextFlow();    var pElement:ParagraphElement=new ParagraphElement();    var spanElement:SpanElement=new SpanElement();    pElement.addChild(spanElement);    tFlow.addChild(pElement);    return tFlow;   }

       //设置焦点;   private function dofocus():void   {    if (textFlow)    {     var selectionManager:ISelectionManager=textFlow.interactionManager;     if (selectionManager)     {      selectionManager.setFocus();     }    }   }

       protected function boldfaceBtn_clickHandler(event:MouseEvent):void   {    var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();

        if (ToggleButton(event.currentTarget).selected)     textLayoutFormat.fontWeight=FontWeight.BOLD;    else     textLayoutFormat.fontWeight=FontWeight.NORMAL;

        var editorManager:IEditManager=_textFlow.interactionManager as IEditManager;    editorManager.applyLeafFormat(textLayoutFormat);   }

       protected function underlineBtn_clickHandler(event:MouseEvent):void   {    var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();    if (ToggleButton(event.currentTarget).selected)     textLayoutFormat.textDecoration=TextDecoration.UNDERLINE;    else     textLayoutFormat.textDecoration=TextDecoration.NONE;

        var editorManager:IEditManager=_textFlow.interactionManager as IEditManager;    editorManager.applyLeafFormat(textLayoutFormat);   }

       protected function italicBtn_clickHandler(event:MouseEvent):void   {    var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();    if (ToggleButton(event.currentTarget).selected)     textLayoutFormat.fontStyle=FontStyle.ITALIC;    else     textLayoutFormat.fontStyle=FontWeight.NORMAL;

        var editorManager:IEditManager=_textFlow.interactionManager as IEditManager;    editorManager.applyLeafFormat(textLayoutFormat);   }

       protected function strikethroughBtn_clickHandler(event:MouseEvent):void   {    var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();    if (ToggleButton(event.currentTarget).selected)     textLayoutFormat.lineThrough=true;    else     textLayoutFormat.lineThrough=false;

        var editorManager:IEditManager=_textFlow.interactionManager as IEditManager;    editorManager.applyLeafFormat(textLayoutFormat);   }

       protected function sourceBtn_clickHandler(event:MouseEvent):void   {    var textString=TextConverter.export(textFlow, TextConverter.TEXT_LAYOUT_FORMAT, ConversionType.STRING_TYPE) as String;    Alert.show(textString);   }

       protected function alignLeftHandler(event:MouseEvent):void   {    var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();    if (event.currentTarget == alignLeftBtn)    {     textLayoutFormat.textAlign=TextAlign.LEFT;     alignCenterBtn.selected=false;     alignRightBtn.selected=false;    }    else if (event.currentTarget == alignCenterBtn)    {     textLayoutFormat.textAlign=TextAlign.CENTER;     alignLeftBtn.selected=false;     alignRightBtn.selected=false;    }    else if (event.currentTarget == alignRightBtn)    {     textLayoutFormat.textAlign=TextAlign.RIGHT;     alignLeftBtn.selected=false;     alignCenterBtn.selected=false;    }    editorManager.applyParagraphFormat(textLayoutFormat);   }

       protected function orientationDrop_changeHandler(event:IndexChangeEvent):void   {    var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();    if (event.newIndex == 0)    {     textLayoutFormat.blockProgression=BlockProgression.TB;    }else{     textLayoutFormat.blockProgression=BlockProgression.RL;    }        editorManager.applyFormatToElement(textFlow,textLayoutFormat);   }

       protected function alignTopBtn_clickHandler(event:MouseEvent):void   {    // TODO Auto-generated method stub   }

       protected function alignBottomBtn_clickHandler(event:MouseEvent):void   {    // TODO Auto-generated method stub   }

       protected function alignMiddleBtn_clickHandler(event:MouseEvent):void   {    // TODO Auto-generated method stub   }

      ]]> </fx:Script>

     <fx:Declarations> </fx:Declarations> <graphic:TLFButtonBar>  <graphic:TLFButton id="sourceBtn"         label="源码"         click="sourceBtn_clickHandler(event)"/> </graphic:TLFButtonBar> <graphic:TLFButtonBar>  <graphic:TLFButton id="boldfaceBtn"         label="粗体"         click="boldfaceBtn_clickHandler(event)"/>  <graphic:TLFButton id="italicBtn"         label="斜体"         click="italicBtn_clickHandler(event)"/>  <graphic:TLFButton id="underlineBtn"         label="下划线"         click="underlineBtn_clickHandler(event)"/>  <graphic:TLFButton id="strikethroughBtn"         label="删除线"         click="strikethroughBtn_clickHandler(event)"/>  <graphic:TLFButton id="alignLeftBtn"         label="左对齐"         click="alignLeftHandler(event)"/>  <graphic:TLFButton id="alignCenterBtn"         label="中间对齐"         click="alignLeftHandler(event)"/>  <graphic:TLFButton id="alignRightBtn"         label="右对齐"         click="alignLeftHandler(event)"/>  <graphic:TLFButton id="alignTopBtn"         label="顶对齐"         click="alignTopBtn_clickHandler(event)"/>  <graphic:TLFButton id="alignMiddleBtn"         label="中间对齐"         click="alignMiddleBtn_clickHandler(event)"/>  <graphic:TLFButton id="alignBottomBtn"         label="底对齐"         click="alignBottomBtn_clickHandler(event)"/>  <s:DropDownList id="orientationDrop"      change="orientationDrop_changeHandler(event)">   <s:dataProvider>    <s:ArrayCollection>     <fx:String>横向</fx:String>     <fx:String>垂直</fx:String>    </s:ArrayCollection>   </s:dataProvider>  </s:DropDownList> </graphic:TLFButtonBar> <graphic:SpriteWithIME id="spriteWithIME"         width="100%"         height="100%"/></s:Group>

     


    最新回复(0)