Flash / Flex / ActionScript/Development/StyleSheet

Материал из Web эксперт
Перейти к: навигация, поиск

Formatting applied to a specific class of paragraph

 
package{
  import flash.display.Sprite;
  import flash.text.*;
  public class Main extends Sprite{
    public function Main(){
        var specialnoteDeclarationBlock:Object = new Object(  );
        specialnoteDeclarationBlock.fontFamily = "Arial"
        specialnoteDeclarationBlock.fontSize   = "20";
        specialnoteDeclarationBlock.fontWeight = "bold";
        
        var styleSheet:StyleSheet = new StyleSheet(  );
        styleSheet.setStyle(".specialnote", specialnoteDeclarationBlock);
        
        var t:TextField = new TextField(  );
        t.width     = 300;
        t.wordWrap  = true;
        t.multiline = true;
        t.styleSheet = styleSheet;
        t.htmlText = "<p>Always remember...</p>"
          + "<p class="specialnote">Set styleSheet before htmlText!</p>"
          + "<p>Otherwise, the stylesheet will not be applied.</p>";
    }
  }
}



Formatting text with a programmatically created style sheet

 
package{
  import flash.display.Sprite;
  import flash.text.*;  
  public class Main extends Sprite{
    public function Main(){
        var pDeclarationBlock:Object = new Object(  );
        
        pDeclarationBlock.fontFamily = "Arial"
        pDeclarationBlock.fontSize   = "20";
        pDeclarationBlock.fontWeight = "bold";
        
        var styleSheet:StyleSheet = new StyleSheet(  );
        
        styleSheet.setStyle("selector", pDeclarationBlock);
        
        styleSheet.setStyle("p", pDeclarationBlock);
        var t:TextField = new TextField(  );
        t.width  = 200
        t.styleSheet = styleSheet;
        
        t.htmlText = "<p>ActionScript is fun!</p>";
        
        addChild(t);
    }
  }
}



Load a CSS document and use that data to populate a StyleSheet object

 
package {
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.events.Event;
  import flash.text.TextFieldAutoSize;
  import flash.text.StyleSheet;
  import flash.net.URLLoader;
  import flash.net.URLRequest;
  public class CSSText extends Sprite {
      
    public function CSSText(  ) {
         var loader:URLLoader = new URLLoader(  );
         loader.addEventListener(Event.ruPLETE, onLoadCSS);
         var request:URLRequest = new URLRequest("styles.css");
         loader.load(request);
    }
    
    private function onLoadCSS(event:Event):void {
      var css:StyleSheet = new StyleSheet(  );
      css.parseCSS(URLLoader(event.target).data);
         var field:TextField = new TextField(  );
         field.autoSize = TextFieldAutoSize.LEFT;
         field.wordWrap = true;
         field.width = 200;
         addChild(field);
         field.styleSheet = css;
         field.htmlText = "<p><span class="emphasis">Title</span> </p>";
    }
  }
}



Set the font with CSS

 
package{
  import flash.display.Sprite;
  import flash.text.*;
  public class Main extends Sprite{
    public function Main(){
        var styleSheet:StyleSheet = new StyleSheet(  );
        var pStyle:Object = new Object(  );
        pStyle.fontFamily = "fontName";
        styleSheet.setStyle("p", pStyle);
        var t:TextField = new TextField(  );
        t.embedFonts = true;
        t.styleSheet = styleSheet; // Assign styleSheet before assigning htmlText!
        t.htmlText = "<p>hello world</p>";
    }
  }
}



Text formatted with an external style sheet

 
package {
  import flash.display.*;
  import flash.text.*;
  import flash.events.*;
  import flash.net.*;
  public class Main extends Sprite {
    public function Main (  ) {
      var urlLoader:URLLoader = new URLLoader(  );
      urlLoader.addEventListener(Event.ruPLETE, completeListener);
      urlLoader.load(new URLRequest("styles.css"));
    }
    private function completeListener (e:Event):void {
      var styleSheet:StyleSheet = new StyleSheet(  );
      styleSheet.parseCSS(e.target.data);
      var t:TextField = new TextField(  );
      t.width = 200;
      t.styleSheet = styleSheet;
      t.htmlText = "<p>ActionScript is fun!</p>";
      addChild(t);
    }
  }
}



Use CSS to in TextField

 
package {
  import flash.display.Sprite;
  import flash.text.TextField;
  public class Main extends Sprite {
    public function Main(  ) {
      var field:TextField = new TextField(  );
    
      var css:flash.text.StyleSheet = new flash.text.StyleSheet(  );
      css.parseCSS("a {color: #0000FF;} a:hover {text-decoration: underline;}");
      field.styleSheet = css;
      field.htmlText = "<a href="http://www.wbex.ru">Website</a>";
      
        addChild(field);
    }
  }
}