0

検索結果のテキストで、検索されたキーワードを強調表示したい検索結果ページがあります。これにはTextLineを使用することが提案されましたが、それを機能させる方法を理解するのに苦労しています。私はシンプルでコンパイル可能なダミーアプリケーションを開始し、誰かが続行する方法についていくつかのヒントを教えてくれることを望んでいました。

<?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"
 initialize="initApp();">

 <fx:Script>
  import flash.display.Sprite;
  import flash.text.engine.*;

  private var textLine:TextLine;

  private function initApp():void {

   var normalFormat:ElementFormat = new ElementFormat(null, 12, 0x000000);
   var highlightFormat:ElementFormat = new ElementFormat(null, 14, 0xff0000);

   var textBlock:TextBlock = new TextBlock(new TextElement("This is text that has KEYWORDS. I would like to highlight these KEYWORDS by changing their font color and adding a light yellow background graphic.", normalFormat)); 

   textLine = textBlock.createTextLine();
   textLine.y = 100;

   embeddedFontHolder.addChild(textLine); 
  }
 </fx:Script>

 <mx:UIComponent width="100%" id="embeddedFontHolder" />
</s:Application>

誰かアイデアはありますか?

乾杯、バズ

4

2 に答える 2

1

Treur に感謝しますが、さらに良い方法を見つけました: setFormatOfRange()

この関数は基本的に、RichEditableText コンポーネント内の文字範囲の形式 (背景/前景) を変更します。だから私がしなければならないのは:

var highlightFormat:TextLayoutFormat = new TextLayoutFormat();
highlightFormat.backgroundColor = 0xffee66;

var keywordsArray:Array = model.keywords.toLowerCase().split(' ');
var indexOfKeyword:int = 0;
for each (var currentKeyword:String in keywordsArray) {
    while((indexOfKeyword = this.text.toLowerCase().indexOf(currentKeyword, indexOfKeyword)) >= 0) {                
        this.setFormatOfRange(highlightFormat, indexOfKeyword, indexOfKeyword + currentKeyword.length);
        indexOfKeyword++;
    }
}

掃除。

于 2010-06-01T16:34:52.507 に答える
0

キーワードの周りに単純な html タグを配置し、TextLine コンポーネントの代わりに RichText コンポーネントを使用してテキストを表示できます。

flex での html の詳細については、http://blog.flexexamples.com/2009/10/06/displaying-html-formatted-text-in-a-spark-richtext-control-in-flex-4/を参照してください。

さらに、文字列内で強調表示する単語を検索するには、String.replace メソッドを使用します: http://livedocs.adobe.com/flex/3/langref/String.html#replace()

于 2010-05-31T12:21:00.123 に答える