Skip to main content
Participant
July 14, 2009
Question

getting the format of unselected text range in TextFlow

  • July 14, 2009
  • 1 reply
  • 1005 views

Hello,

I am trying to mimic the functionality of TextField's "getTextFormat()/setTextFormat()" in a class using TLF.  These methods allow you to specify a beginIndex/endIndex and let you get all common styles to the specified range, or for the whole TextField if no range is specified.  I have found that the SelectionManager/EditManager will allow you to get/set common formats, which I could then use to create a TextFormat class with the appropriate values.  However, these methods only apply to a currently-selected range of text, rather than a programmatically-specified one.  Is there any way to do what I'm attempting without, in the example of "getTextFormat()":

1) caching a current selection

2) caching the focusSelectionFormat/inactiveSelectionFormat

3) changing the focusSelectionFormat/noFocusSelectionFormat properties to mimic the current inactiveSelectionFormat (so it doesn't look like I'm selecting them)

4) doing a setSelection() on the specified range

5) calling getCommonContainerFormat(), getCommonParagraphFormat(), getCommonCharacterFormat() to get the common values

6) creating a TextFormat that uses the values returned from the previous step

7) resetting to cached formats

😎 resetting to cached selection

This seems like a very difficult, as well as heavy, way to get the styles on a specified range, but I have been unable to find any functionality that does what SelectionManager::getCommonContainerFormat(), SelectionManager::getCommonParagraphFormat(), SelectionManager::getCommonCharacterFormat() and EditManager::applyFormat() does without relying on the current selection.  If anyone knows of something that allows me to do this, or just a better way of doing this than the above-described flow, I would greatly appreciate any suggestions or comments.

Thanks,

Christian Pillsbury

Digital Primates

This topic has been closed for replies.

1 reply

Aaronius9er9er
Inspiring
April 26, 2010

I'm likewise looking for the answer to this question.  If a text range is passed into SelectionManager.getCommonCharacterFormat and nothing in the text flow is selected, it will return the character formats for the first leaf, NOT the common character formats for all the text in the text range.  For example, if the first few letters of the text range have a font size of 10 and the next few letters in the text range are 15, getCommonCharacterFormat will return 10 for the font size attribute, even though it should return undefined since there's not a common font size.

Adobe Employee
April 26, 2010

That's a bug in getCommonCharacterFormat.  The getCommon*Format code is very simple.  I'd recommend writing your own function.  This compiles but I didn't test it.  I believe I have all the imports listed here as well.

    import flashx.textLayout.edit.ElementRange;

    import flashx.textLayout.elements.TextRange;

    import flashx.textLayout.formats.Category;

    import flashx.textLayout.formats.ITextLayoutFormat;

    import flashx.textLayout.formats.TextLayoutFormat;

    import flashx.textLayout.property.Property;

    import flashx.textLayout.tlf_internal;

    use namespace tlf_internal;

       
        static public function getCommonCharacterFormatFromRange(range:TextRange):ITextLayoutFormat
        {
            if (!range)
                return null;
           
            var selRange:ElementRange = ElementRange.createElementRange(range.textFlow, range.absoluteStart, range.absoluteEnd);
           
            var leaf:FlowLeafElement = selRange.firstLeaf;
            var attr:TextLayoutFormat = new TextLayoutFormat(leaf.computedFormat);

            for (;;)
            {
                if (leaf == selRange.lastLeaf)
                    break;
                leaf = leaf.getNextLeaf();
                attr.removeClashing(leaf.computedFormat);
            }
           
            return Property.extractInCategory(TextLayoutFormat, TextLayoutFormat.description, attr, Category.CHARACTER) as ITextLayoutFormat;
        }

Hope that helps,

Richard

Aaronius9er9er
Inspiring
April 26, 2010

Any idea if that's been reported before?  I saw the code and how it's working, but was unsure if that's how it was intended or if it was really a bug.  I did a search in jira and didn't find anything I saw that matched.  Thanks for the response.