• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

Truncate Text Until Last Visible Word?

New Here ,
Oct 13, 2011 Oct 13, 2011

Copy link to clipboard

Copied

Hello,

I have a RichEditableText populated by markup similar to:

<p><span font="xyz" font-size="12">This is a string of text that may be cut-off on screen depending on the size of the container and the size/style of the selected font.</span></p>

I would like to be able to identify the point in the text where it is no longer visible on screen, then save only the visible chars to a db. For example, if the font were very big, and the container relatively small, we may only be able to see up to the word "cut-off" - in which case I would like to save the string "This is a string of text that may be cut-off" to my db. If the font/style or size changes, the string would be cut-off at a different point and I would liek to save up to there.

Anyone have an idea of how to go about doing that?

Cheers,

Baz

TOPICS
Text layout framework

Views

1.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Oct 13, 2011 Oct 13, 2011

Copy link to clipboard

Copied

1. get the first and last TextLine on RichEditableText by getChildAt and RichEditableText.numChildren  ( getChildAt(numChildren -1) equals null sometimes and numChildren-2 is the last TextLine. Just pay attention.)

2. get the counterpart TextFlowLine, TextLine.userData as TextFlowLine

3. get the beginning position by firstTextFlowLine.absoluteStart

4. get the ending position by lastTextFlowLine.absoluteStart + lastTextFlowLine.textLength

5. truncate text according to the beginning and ending position got in step 3 & 4

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 14, 2011 Oct 14, 2011

Copy link to clipboard

Copied

Thank you very much Jin-Huang. What is strange is that userData is comming back null in the TextLines - I'm not sure why. The code I tried is:

private var richEditableTextMarkup:XML =

<p direction="ltr" justificationRule="auto" justificationStyle="auto" leadingModel="romanUp" paragraphEndIndent="0">

     <span alignmentBaseline="useDominantBaseline" breakOpportunity="auto" cffHinting="horizontalStem">

          Auhtor: Baz Maz Rataz

     </span>

     <span alignmentBaseline="useDominantBaseline">

          This text in my code is much longer than displayed here.

     </span>

</p>;


public function creationCompleteHandler(e:Event):void {

   richEditableText.textFlow = TextFlowUtil.importFromXML(richEditableTextMarkup);

}

public function truncateVisibleText():void {

    var firstTextLine:TextLine = richEditableText.getChildAt(0) as TextLine;

    var lastTextLine:TextLine = richEditableText.getChildAt(richEditableText.numChildren - 1) as TextLine;

    var firstTextFlowLine:TextFlowLine = firstTextLine.userData as TextFlowLine;

    var lastTextFlowLine:TextFlowLine = lastTextLine.userData as TextFlowLine;

    var beginningPosition:int = firstTextFlowLine.absoluteStart;

    var endPosition:int = lastTextFlowLine.absoluteStart + lastTextFlowLine.textLength;

}

<s:RichEditableText id="richEditableText" width="300" height="100" />

<s:Button label="Truncate" click="clickHandler(event)" />

The TextLine's come in fine, but when I introspect their userData property in the debugger it comes back null. Any idea why?

I did find another way to go about doing it, but it uses tlf_internal:

public function truncateVisibleText(textFlow:TextFlow):String {

     var textFlowComposer:StandardFlowComposer = textFlow.flowComposer as StandardFlowComposer;

     var lastContainerController:ContainerController = textFlowComposer.getControllerAt(textFlowComposer.numControllers - 1);

     var lastTFLine:TextFlowLine = lastContainerController.tlf_internal::getLastVisibleLine();

     var endPosition:int = lastTFLine.absoluteStart + lastTFLine.textLength;

     var truncatedText:String = richEditableText.text.slice(0, endPosition);

 

     return truncatedText;

}

Do you think this is an acceptable solution?

Finally, I would like to take the truncated text, and after saving it to the db, I'd like to also truncate it on screen. Of course, if I just replace the text of the RichEditableText container, all the formatting would be lost. Do you have any suggestions on the logic or strategy I should look into using to truncate the text while maintaining the formatting (there are a couple of P tags for example).

Cheers!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Oct 16, 2011 Oct 16, 2011

Copy link to clipboard

Copied

LATEST

The last child of RichEditableText, richEditableText.getChildAt(richEditableText.numChildren - 1), is not the last TextLine due to the implementation of RichEditableText (RichEditableText isn't developed by TLF, but Flex SDK), which I have mentioned in my last reply.

BTW, RichEditableText take advantage of flashx.textlayout.container.TextContainerManager, which has only one containerController. textFlowComposer.numControllers - 1 is always zero.

If you can use namespace tlf_internal, getFirstVisibleLine() and get LastVisibleLine() is a better and safer way.

For your new requirement, a simple way is you take advantage of flashx.textLayout.element.TextRange, flashx.textLayout.edit.TextScrap, flashx.textLayout.operation.deleteTextOperation, and flashx.textLayout.operation.pasteOperation.

Another way is that you 1)get the TextRange 2) then TextScrap 3)and finally replace the children of textFlow with the children of TextScrap.textFlow.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines