【问题标题】:How to clear formatting on a selection in TLF?如何清除 TLF 中所选内容的格式?
【发布时间】:2018-07-14 06:14:43
【问题描述】:

我正在尝试删除所选内容的格式,而我目前仅在所选内容位于段落内时删除所选内容的格式。如果所选内容延伸到另一个段落,则不会删除格式。

这是我目前所拥有的:

var currentFormat:TextLayoutFormat;
var currentParagraphFormat:TextLayoutFormat;
var containerFormat: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);
    }

    currentFormat = editManager.getCommonCharacterFormat(operationState);
    currentParagraphFormat = editManager.getCommonParagraphFormat(operationState);
    containerFormat = editManager.getCommonContainerFormat(operationState);

    editManager.clearFormat(currentFormat, currentParagraphFormat, containerFormat);

}

【问题讨论】:

    标签: actionscript-3 flash apache-flex tlf


    【解决方案1】:

    似乎SelectionManager.getCommonCharacterFormat() 并没有完全按照我的想法去做。我需要获取所选字符的格式,而该功能似乎没有这样做。

    如果我得到一个ElementRange,然后循环遍历它,我可以创建一个TextLayoutFormat,其中包含元素范围内所有叶子的格式。

    var currentFormat:TextLayoutFormat;
    var currentParagraphFormat:TextLayoutFormat;
    var containerFormat: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);
        }
    
        // following lines were change
        elementRange = ElementRange.createElementRange(richEditableText.textFlow, selectionStart, selectionEnd);
        currentFormat = getElementRangeFormat(elementRange);
    
        editManager.clearFormat(currentFormat, currentParagraphFormat, containerFormat);
    
    }
    
    // method to get format of the selected 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      • 2017-05-12
      • 2010-12-14
      • 2016-01-15
      • 2018-07-08
      相关资源
      最近更新 更多