1

Is there a way to get the format of the current selection? Here is what I have so far:

var currentFormat:TextLayoutFormat;
var selectionStart:int;
var selectionEnd:int;
var operationState:SelectionState;
var editManager:IEditManager;

if (richEditableText.textFlow && richEditableText.textFlow.interactionManager is IEditManager) {
    editManager = IEditManager(richEditableText.textFlow.interactionManager);

    selectionStart = Math.min(richEditableText.selectionActivePosition, richEditableText.selectionAnchorPosition);
    selectionEnd = Math.max(richEditableText.selectionActivePosition, richEditableText.selectionAnchorPosition);

    if (operationState == null) {
        operationState = new SelectionState(richEditableText.textFlow, selectionStart, selectionEnd);
    }

    // this does not work
    currentFormat = editManager.getCommonCharacterFormat(operationState);

}
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231

1 Answers1

0

I couldn't find anything so here is what seems to work:

/**
 * Get format of element range
 **/
public static function getElementRangeFormat(elementRange:ElementRange):TextLayoutFormat {      
    var leaf:FlowLeafElement = elementRange.firstLeaf;
    var attr:TextLayoutFormat = new TextLayoutFormat(leaf.computedFormat);

    for (;;)
    {
        if (leaf == elementRange.lastLeaf)
            break;
        leaf = leaf.getNextLeaf();
        attr.concatInheritOnly(leaf.computedFormat);
    }

    return Property.extractInCategory(TextLayoutFormat, TextLayoutFormat.description, attr, Category.CHARACTER, false) as TextLayoutFormat;
}

Inspired by ElementRange.getCommonCharacterFormat().

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231