4.11.指定TextField中的HTML样式

4.11.1. 问题
我想通过外部加载的CSS 文件或是写在程序里的CSS 样式,来实现TextField 中的HTML的CSS 样式。
4.11.2. 解决办法
使用StyleSheet parseStyle 方法来对TextArea 进行字符串解析和样式表分配。
4.11.3. 讨论
StyleSheet 对象可以把任何正确的CSS 当成字符串解析,处理它的所有信息,把它分配到一个组件或是在HTML 文本中应用。要实现这个,你需要用一个URLLoader 对象加载文件,把加载的数据当作字符串传递给parseCSS 方法,也可以把字符串写出来直接传给StyleSheet对象。下面的例子就是把样式写成字符串,当creationComplete 事件发生时,就将它解析。

TextArea 的htmlText 属性在样式被应用后就会进行设置,确保样式被完全应用到HTML 里。

使用如下样式属性会<span>的样式:
"<span class='largered'>
This style is specified in the string that will be passed to the StyleSheet.parseStyle method:
.largered { font-family:Arial, Helvetica; font-size:16; color: #ff0000; }

例子:
+展开
-XML
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="1000height="800creationComplete="createStyle()">

<mx:Script>
<![CDATA[
//note even though names are camel cased here when used, all lowercase
private var styleText:String = '.largered { fontfamily: Arial, Helvetica; font-size:16; color: #ff0000; }' +
'.smallblue { font-size: 11; color: #0000ff; font-family:Times New Roman, Times; }' +
'li { color:#00ff00; font-size:14; }' +
'ul {margin-left:50px;}';
[Bindable]
private var lipsum:String = "<span class='largered'>Nulla metus.</span> Nam ut dolor vitae risus condimentum auctor."+
" <span class='smallblue'>Cras sem quam,</span> malesuada eu, faucibus aliquam, dictum ac, dui. Nullam blandit"+
" ligula sed arcu. Fusce nec est.<ul><li> Etiam</li><li> aliquet,</li> <li>nunc</li></ul> eget pharetra dictum, magna"+
" leo suscipit pede, in tempus erat arcu et velit. Aenean condimentum. Nunc auctor"+
" nulla vitae velit imperdiet gravida";
[Bindable]
private var style:StyleSheet;
private function createStyle():void
{
style = new StyleSheet();
style.parseCSS(styleText);
text.styleSheet = style;
text.htmlText = lipsum;
}

]]>
</mx:Script>
<mx:TextArea id="textwidth="200height="300"/>
</mx:Application>

加支付宝好友偷能量挖...


评论(0)网络
阅读(126)喜欢(0)flash/flex/fcs/AIR