Flash / Flex / ActionScript/Development/StyleSheet
Содержание
Formatting applied to a specific class of paragraph
<source lang="java">
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 = "
Always remember...
" + "Set styleSheet before htmlText!
" + "Otherwise, the stylesheet will not be applied.
";} }
}
</source>
Formatting text with a programmatically created style sheet
<source lang="java">
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 = "
ActionScript is fun!
";addChild(t); } }
}
</source>
Load a CSS document and use that data to populate a StyleSheet object
<source lang="java">
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 = "
Title
";} }
}
</source>
Set the font with CSS
<source lang="java">
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 = "
hello world
";} }
}
</source>
Text formatted with an external style sheet
<source lang="java">
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 = "
ActionScript is fun!
";addChild(t); } }
}
</source>
Use CSS to in TextField
<source lang="java">
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); } }
}
</source>