0

我有这样的代码

var str:string = "GeoCode: 3";

在这段代码中,我希望“:”为粗体。

我该怎么做,这应该在火花中完成

请帮我

谢谢!

4

2 回答 2

2

The simplest approach is to use TextFlowUtil.importFromString() and assign the resulting TextFlow object to a RichText component, like this:

<s:RichText id="textDisplay"/>

textDisplay.textFlow = 
   TextFlowUtil.importFromString('GeoCode<span fontWeight="bold">:</span> 3');

If you'd like to keep your original Strings intact, you can do a replace on them to add the span:

var str:String = "GeoCode: 3";
str = str.replace(/:/, '<span fontWeight="bold">:</span>');
textDisplay.textFlow = TextFlowUtil.importFromString(str);
于 2013-06-10T22:46:10.560 回答
0

我会做这样的事情:

在此处输入图像描述

组件的 text 属性通过“:”符号分成几部分。然后每个部分都像标签一样添加到 HGroup 中。冒号加粗。

//应用

<?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:com="com.*">

<s:VGroup left="20" top="20">
    <com:GeoLabel text="GeoCode: 3"/>
    <com:GeoLabel text="GeoCode: 5: Some Info: 123"/>
</s:VGroup>
</s:Application>

//地理标签组件

<?xml version="1.0" encoding="utf-8"?>
<s:HGroup 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="120" height="25" gap="0">
<fx:Script>
    <![CDATA[
        import spark.components.Label;
        private var _text:String;

        public function get text():String
        {
            return _text;
        }

        public function set text(value:String):void
        {
            _text = value;

            createMembers();
        }

        private function insertLabel(str:String, bold:Boolean):void
        {
            var la:Label = new Label();
            la.text = str;

            if (bold)
                la.setStyle("fontWeight", "bold");

            this.addElement(la);
        }

        private function createMembers():void
        {
            this.removeAllElements();

            var arr:Array = text.split(":");

            for (var i:int = 0; i < arr.length; i++)
            {
                if (i != 0)
                    insertLabel(":", true);
                insertLabel(arr[i], false);
            }
        }
    ]]>
</fx:Script>
</s:HGroup>
于 2013-06-10T22:17:15.183 回答