Mar 23
在这里看到有人问起TextArea中文本内容段落间的间隔为什么总是会那么大,想不到peterd这么快就为我们写了一个例子来说明这个问题。
接下来的例子演示了如何动态读入一个CSS文件并且通过设置TextArea控件的styleSheet属性将其CSS中的设置反映到TextArea控件中。
下面是完整代码:
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white"
- creationComplete="init();">
- <mx:Script>
- <![CDATA[
- import mx.controls.Alert;
- private var styleSheet:StyleSheet;
- private var urlLoader:URLLoader;
- private function init():void {
- urlLoader = new URLLoader();
- urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
- urlLoader.load(new URLRequest("styles.css"));
- }
- private function urlLoader_complete(evt:Event):void {
- var css:String = URLLoader(evt.currentTarget).data;
- // Convert text to style sheet.
- styleSheet = new StyleSheet();
- styleSheet.parseCSS(css);
- // Set the style sheet.
- textArea.styleSheet = styleSheet;
- }
- private function textArea_link(evt:TextEvent):void {
- Alert.show("text: " + evt.text, "Panel");
- }
- ]]>
- </mx:Script>
- <mx:String id="txt" source="text.html" />
- <mx:TextArea id="textArea"
- htmlText="{txt}"
- editable="false"
- condenseWhite="true"
- width="100%"
- height="100%"
- link="textArea_link(event);" />
- </mx:Application>
下面是CSS文件:
Download: styles.css
- h1 {
- font-size: 24;
- }
- p {
- font-size: 10;
- }
- a {
- text-decoration: underline;
- }
- a:hover {
- color: #FF0000;
- }
下面是Demo(可以右键或点击这里察看源代码):
你也可以通过<mx:HTTPService />动态的读入一个扩展CSS文件,下面是参考代码:
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white"
- creationComplete="init();">
- <mx:Script>
- <![CDATA[
- import mx.rpc.events.ResultEvent;
- import mx.controls.Alert;
- private var styleSheet:StyleSheet;
- private function init():void {
- httpService.send();
- }
- private function httpService_result(evt:ResultEvent):void {
- var css:String = evt.result as String;
- // Convert text to style sheet.
- styleSheet = new StyleSheet();
- styleSheet.parseCSS(css);
- // Set the style sheet.
- textArea.styleSheet = styleSheet;
- }
- private function textArea_link(evt:TextEvent):void {
- Alert.show("text: " + evt.text, "Panel");
- }
- ]]>
- </mx:Script>
- <mx:HTTPService id="httpService"
- url="styles.css"
- resultFormat="text"
- result="httpService_result(event);" />
- <mx:String id="txt" source="text.html" />
- <mx:TextArea id="textArea"
- htmlText="{txt}"
- editable="false"
- condenseWhite="true"
- width="100%"
- height="100%"
- link="textArea_link(event);" />
- </mx:Application>
下面是CSS文件:
Download: styles.css
- h1 {
- font-size: 24;
- }
- p {
- font-size: 10;
- text-align: justify;
- }
- a {
- text-decoration: underline;
- color: #0000FF;
- }
- a:hover {
- color: #FF0000;
- }
下面是Demo(可以右键或点击这里察看源代码):
原文作者:Peter deHaan 翻译:minidxer

March 27th, 2008 at 12:52 am
您好…我是flash 新手.. 算是未用過flash
請問如果純 FLASH textarea ,可以實現到 內文單字highlight嗎.
或在flex可以更容易實現嗎
March 27th, 2008 at 1:01 am
例子中的就是textarea
highlight根据自己的需要设置styles.css就可以了。