如何获取当前选择的边界框?

时间:2011-01-25 23:28:16

标签: flex richeditabletext

我想做一些奇特的选择指标。如何获取当前所选字符的边界框?

2 个答案:

答案 0 :(得分:1)

这不重要。首先,选择可能需要多个矩形。接下来,没有方便的方法。

这就是我必须做的事情:

        var start:int = op.activePosition < op.anchorPosition ? op.activePosition : op.anchorPosition;
        var end:int = op.activePosition > op.anchorPosition ? op.activePosition : op.anchorPosition;

        var textFlow:TextFlow = this.textFlow;
        var rectangles:Dictionary = new Dictionary();

        // For each selected character, make a box
        for( var i:int=start; i < end; i++) {

            var flowLine:TextFlowLine = textFlow.flowComposer.findLineAtPosition( i, true );

            if( rectangles[ flowLine.absoluteStart ] == null ) {
                rectangles[ flowLine.absoluteStart ] = new Rectangle();
                (rectangles[ flowLine.absoluteStart ] as Rectangle).x = 0xffffff;
                (rectangles[ flowLine.absoluteStart ] as Rectangle).right = 0;
            }
            var currentRect:Rectangle = rectangles[ flowLine.absoluteStart ];

            var textLine:TextLine = flowLine.getTextLine(true);
            var atomIndex:int = textLine.getAtomIndexAtCharIndex( i );
            if( atomIndex >= 0) {
                var atomBounds:Rectangle = textLine.getAtomBounds( atomIndex );

                var pt:Point = this.globalToLocal( textLine.localToGlobal( new Point( atomBounds.left, atomBounds.top ) ) );                    
                if( pt.x <= currentRect.left ) {
                    currentRect.left = pt.x;
                    currentRect.top = pt.y;
                }

                pt = this.globalToLocal( textLine.localToGlobal( new Point( atomBounds.right, atomBounds.bottom) ) );
                if( pt.x >= currentRect.right ) {
                    currentRect.right = pt.x;
                    currentRect.bottom = pt.y;
                }                   
            }
        } 
        return rectangles;

答案 1 :(得分:0)

我不相信有一个微不足道的方法可以完全控制这个,从我看到的文档中看到一点: http://opensource.adobe.com/wiki/display/flexsdk/Spark+Text+Primitives#SparkTextPrimitives-FTE

[Style(name =“focusedTextSelectionColor”,type =“uint”,format =“Color”,inherit =“yes”)]

[Style(name =“inactiveTextSelectionColor”,type =“uint”,format =“Color”,inherit =“yes”)]

[Style(name =“unfocusedTextSelectionColor”,type =“uint”,format =“Color”,inherit =“yes”)]

还要注意:

锚点位置 - 一个字符索引,指定当您使用箭头键扩展选择时保持固定的选择结束。

活动位置 - 一个字符索引,指定使用箭头键扩展选择时移动的选择的结束。

由于这些都是唯一的颜色(或指数),我不知道他们是否会完全满足你想要做的那种幻想。我在Flex 3中处理了一些事情来处理自定义文本选择控件,最后使用了各种各样的“屏幕外缓冲区”,我在屏幕外放置了一个与屏幕上相同属性的TextField,然后逐个转储字符1直到我达到所需的宽度,然后我可以找出控件被丢弃的字符中的位置(有点像android选择)。

我建议你在SDK中搜索上面的样式(特别是在RichEditableText及其超类中,我会这样做但是现在有很多版本,并且不知道你使用哪个版本,TLF和看起来FTE似乎有点不稳定)。一旦找到这些样式的使用位置,您可能会在选择指标绘图代码附近,并且可能需要从任何类扩展以覆盖适当的方法。

对不起,我不能给你一个直接的回答,但希望这有帮助,或者如果有一个更简单的方法,其他人可以加入。

肖恩

相关问题