I'm going to echo Brent's suggestion. I'd suggest looking at the ApplyElementUserStyleOperation -- create one and then call editManager.doOperation with it. Sorry, there ought to be a method for it on the EditManager, but you can do it yourself with just this small extra step. Note that the API for this is different from applyFormat, because with this you specify the actual element it applies to. In your case, this might always be the leaf node, but the API allows you to add at any of the FlowElement hierarchy.
Wow, this is exactly what I was looking for!
I still have 2 questions, I'm a bit confused of:
1) Why does "value" argument of ApplyelementUserStyleOperation constructor is int? I guess small mistake is in the document (http://help.adobe.com/en_US/Flex/4.0/langref/flashx/textLayout/operations/ApplyElementUser StyleOperation.html).
2) Why not to move userStyle directly to ITextLayoutFormat? Is this logically incorrect?
And another one question. I'm using Flex SDK 3.4 now. Shall I switch to Gumbo to get these nice userStyle features?
Thanks again! You helped me a lot!
Interesting. Looks like that could maybe be an ASDoc bug or something else going on. The actual implementation of the contructor for ApplyElementUserStyleOperation in my build of TLF looks like this:
public function ApplyElementUserStyleOperation(operationState:SelectionState, targetElement:FlowElement, styleName:String, value:*, relativeStart:int = 0, relativeEnd:int = -1)
I'm not sure how to answer #2, but my first thought is that there's a fairly good chance that whatever you end up using 'user styles' for will not be related to the actual formatting of the text. It could be some sort of metadata that's completely abstract from how the text is formatted or represented visually, which is where the text layout format applies. There are more technical reasons why it shouldn't be there also, but they're probably not worth the energy unless you really want to know. ..and Robin would be a better person to comment on that also.
I don't know what build of TLF (if any) is included in the Flex 3.4 SDK, but TLF does not have any dependencies on the Flex framework (or any particular version of it). The only requirement is that you are targetting Flash Player 10 or above. You can always download the latest (or a very recent) version of the TLF SWC's, RSL's, and source with the Flex 4 SDK from the Flex 4 download site at:
Logics of EditManager is very smart when working with TextLayoutFormat. It splits and merges leafs automatically when you call any applyFormat... method.
Applying user styles is not so smart unfortunately and this is why I was asking about moving user styles to TextLayouFormat.
However user styles is a great step forward and they will really help me to solve the issues with custom properties. The only disadvantage is a quite big number of extra code lines to be written to make it able to work like applyFormat... method
That's a good point. I hadn't considered the fact that there isn't currently good support for applying a particular user style to an abitrary text range.
The ApplyFormatOperation, which does that for TextLayoutFormat's, utilizes methods in the behind-the-scenes ParaEdit class to apply the format and perform any leaf-splitting that is required to be able to apply that format for any given absolute start/end positions in the text. That ParaEdit class does not currently have support for doing the same thing with user styles, but you could probably leverage methods or approaches from it if you were to try to do it manually for user styles. It should be fairly easy to create an ApplyUserStyleOperation or something like that to encapsulate that logic and support undo/redo....
You're right. ApplyUserStyleOperation was quite easy to implement.
But now I faced the following problem. When you type the text manually InsertTextOperation is applied. It also uses some ParaEdit stuff. Of course, InsertTextOperation can be overriden and the EditManager.insertText method should also be overriden then.
The problem is that it widely uses SelectionState.pointFormat property to apply the format to the typed character. I have to add there something like pointUserStyle... Do you see the way how can I extend SelectionState with minimal overriding of other TLF classes or maybe you have another ideas how to implement InsertTextOperation correctly?
Thanks in advance.
Right now ApplyUserStyleOperation operates on a single element or a subset of that element. To make it work across multiple elements in a selection I'd suggest using beginCompositeOperation, then ApplyUserStyleOperation for each element in the range that you wish to apply the user style to and then endCompositeOperation to bundle them all into one operation. This would continue to treat ApplyUserStyleOperation as a simple atomic operation that can be composed into more complex operations.
pointFormat having no way to hold on to userStyles is a bug. I'll write it up.
Sorry for digging up somethin old, but i am curios on how to add custom value to a selected section of textflow. How excactly does this ApplyUserStyleOperation works? some code examples would be greatly appriciated. I am looking for a way to apply CMYK values onto a selection of text much like i do with regular TextLayoutFormat and setFormatOfRange for RichEditableTextfield.
Hope below code helps, it shows how to use ApplyElementUserStyleOperation to apply user styles.
var styleName:String = "userStyleName";
var origStyleValue:* = textFlow.getStyle(styleName);
var newStyleValue:String = "newTFStyleValue";
var editManager:EditManager = new EditManager();
textFlow.interactionManager = editManager;
This was to much of a challange for me to implement previously but now it's quite critical that i can do some custom values to the format of my textflow.
Has this changed annything since flex sdk 4.5? is it posible to extend TextLayoutFormat or add userStyle values to it?
The method of using ApplyElementUserStyleOperation is working but it gives me a warning that tells me it's deprecated and i don't know how to properly apply it for current selection (the way i can do with formats) and after that get from selection.
Looked into it, you are correct, from 2.0, ApplyElementUserStyleOperation is deprecated, for 3.0, you can uses ApplyFormatToElementOperation.
var styleName:String = "userStyleName";
var newStyleValue:String = "newUserStyleValue";
var format:TextLayoutFormat = new TextLayoutFormat();
format.fontSize = 10;
ApplyFormatToElementOperation(textFlow.interactionManager.getSelectionState(), textFlow, format);
Now i've been trying to get my head around this for quite some time. I want to be able to use this with selections (range of elements) using ElementRange class doesn't seem to work tho because every getCommonContainerFormat etc is defining a new TextLayoutFormat so every custom values are ignored.
So i can save a value but everytime i try to recieve them it's gone.