2010-03-25 4 views
1

Je souhaite trouver un moyen d'obtenir l'index des caractères dans un RichEditableText basé sur Spark en fonction de la position x, y de la souris. Le mx.controls.TextArea possède une méthode protégée getCharIndexAtPoint() mais je ne trouve pas d'équivalent dans le Spark RichEditableText qui soit décevant.getCharIndexAtPoint() équivalent dans Spark RichEditableText

Des idées ou des recommandations?

Répondre

1

Je cherchais une solution similaire après le heads up de back2dos je suis venu avec la solution suivante, probablement besoin d'un peu de travail, mais il fonctionne

http://www.justinpante.net/?p=201

0

Voici ce que je:

private function getCharAtPoint(ta:RichEditableText, x:Number, y:Number) : int 
{ 

    var globalPoint:Point = ta.localToGlobal(new Point(x, y)); 

    var flowComposer:IFlowComposer = ta.textFlow.flowComposer; 

    for (var i:int = 0; i < flowComposer.numLines; i++){ 

     var textFlowLine:TextFlowLine = flowComposer.getLineAt(i); 

     if (y >= textFlowLine.y && y < textFlowLine.height + textFlowLine.y) 
     { 
      return textFlowLine.absoluteStart 
       + textFlowLine.getTextLine(true) 
        .getAtomIndexAtPoint(globalPoint.x, globalPoint.y); 
     } 
    } 

    return -1; 
} 
0

J'ai eu le même problème. La réponse que Mien a donnée n'a pas fonctionné pour moi, initialement.

Avec les changements ci-dessous je l'ai fonctionné.

 var globalPoint:Point = new Point(stage.mouseX, stage.mouseY); 

     var flowComposer:IFlowComposer = this.textFlow.flowComposer; 

     for (var i:int = 0; i < flowComposer.numLines; i++) 
     { 
      var textFlowLine:TextFlowLine = flowComposer.getLineAt(i); 
      var textLine:TextLine = textFlowLine.getTextLine(true); 
      var textRect:Rectangle = textLine.getRect(stage); 

      if (globalPoint.y >= textRect.top && globalPoint.y < textRect.bottom) 
      { 
       return textFlowLine.absoluteStart + textLine.getAtomIndexAtPoint(globalPoint.x, globalPoint.y); 
      } 
     } 

     return 0;