Flex中利用TextRange类高亮(hightlight)显示文本内容的例子

在显示搜索结果,标记重点文本内容等很多时候都需要用到文本高亮显示,接下来的例子演示了Flex中如何利用TextRange类,高亮(hightlight)显示文本内容。下面的Demo中用了两个Slider,拖动来选择高亮文本范围。
让我们先来看一下Demo(可以右键View Source或 点击这里察看源代码 ):
 
下面是完整代码(或 点击这里察看):
Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" verticalAlign="middle" backgroundColor="white" viewSourceURL="srcview/index.html">
  3.     <mx:Script>
  4.         <![CDATA[
  5.             import mx.events.SliderEvent;
  6.             import mx.controls.textClasses.TextRange;
  7.             private var tr:TextRange;
  8.             private function slider_change(evt:SliderEvent):void {
  9.                 updateTextRange();
  10.             }
  11.             private function textArea_change(evt:Event):void {
  12.                 // Recalculate length.
  13.                 slider.maximum = textArea.length;
  14.                 updateTextRange();
  15.             }
  16.             private function updateTextRange():void {
  17.                 try {
  18.                     /* Default the TextArea control's text color back to black. */
  19.                     tr = new TextRange(textArea);
  20.                     tr.color = "black";
  21.                     tr.textDecoration = "normal";
  22.                     /* Set the text color to red for the values in the Slider. */
  23.                     tr = new TextRange(textArea, false, slider.values[0], slider.values[1]);
  24.                     tr.color = colorPicker.selectedColor;
  25.                     tr.textDecoration = "underline";
  26.                 } catch (err:RangeError) {
  27.                     /* Somethin' ain't right! I dare say you have no text, son! */
  28.                 }
  29.             }
  30.         ]]>
  31.     </mx:Script>
  32.     <mx:VBox width="380">
  33.         <mx:HBox>
  34.             <mx:Label text="Text selection color:" />
  35.             <mx:ColorPicker id="colorPicker" selectedColor="red" />
  36.         </mx:HBox>
  37.         <mx:HBox>
  38.             <mx:Label text="Text selection range:" />
  39.             <mx:HSlider id="slider" thumbCount="2" liveDragging="true" snapInterval="1" dataTipPrecision="0" minimum="0" maximum="{textArea.length}" change="slider_change(event)" />
  40.             <mx:Label text="({slider.values.getItemAt(0)}, {slider.values.getItemAt(1)})" />
  41.         </mx:HBox>
  42.         <mx:TextArea id="textArea" width="100%" height="120" change="textArea_change(event)">
  43.             <mx:text><![CDATA[The quick brown fox jumped over the lazy dog.]]></mx:text>
  44.         </mx:TextArea>
  45.     </mx:VBox>
  46. </mx:Application>

你可能感兴趣的:(职场,休闲)