【问题标题】:How do I get a bounding box for the current selection?如何获得当前选择的边界框?
【发布时间】:2011-01-25 23:28:15
【问题描述】:

我想做一些花哨的选择指示器。如何获取当前选中字符的边界框?

【问题讨论】:

    标签: apache-flex richeditabletext


    【解决方案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;
    

    【讨论】:

      【解决方案2】:

      我不相信有一个简单的方法可以完全控制这个,通过查看文档我看到了这个: 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 x 1 转储直到我达到所需的宽度,然后我才能弄清楚控件在字符中的位置(有点像 android 选择)。

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

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

      肖恩

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多