OK, reporting back...  Simply adding a call to invalidate properties 
in my data setter allowed me to move the TextFormat code back into 
commitProperties where it felt like it should be.  This seems to 
have ensured that it is called not only when the renderer is 
initially created but also when it is recycled (very good learning 
for me!).  Dan, you must be right about the TextFormat getting 
defaulted back at some point... haven't exactly identified where but 
I'll be on the look out for that...  Alex, I'll look into using 
styles as an alternative when I get some more time.

Thanks very much to both of you for your help!
-James

--- In [email protected], "jandersen1978" 
<[EMAIL PROTECTED]> wrote:
>
> Alex/Dan,
>    Thanks to you both for your comments!  Alex, I just took a 
quick 
> look at your item rendered examples and I think that's going to be 
> very helpful...  Perhaps using styles (and the UITextField) will 
be 
> a better approach because I'm not interested in mixing formats 
> within the text field.  Dan your comment about commitProperties 
gave 
> me an idea.  I noticed that commitProperties was only firing once 
> per item renderer in my chat window.  Having learned just now that 
> renderers are recycled I think I may just need to add a call to 
> invalidateProperties in my data setter below which would cause 
> commitProperties to be fired each time the data is changed.  
Perhaps 
> defaultTextFormat would be another solution.
> 
> Unfortunately, I'm don't get to do flex for my day job (yet!) so 
> I'll have to try this out soon and post the results.
> Thanks!
> -James
> 
> --- In [email protected], "Daniel Freiman" 
> <FreimanCQ@> wrote:
> >
> > I don't think TextFields are effected by styles (UITextFields 
> are).  It
> > might be easier to use UITextFields and use styles.
> > 
> > Another possibility is that setting the text property reverts the
> > TextField's textformat.  I forget if it reverts to the the 
format 
> of the
> > first character or to the defaultTextFormat property of the 
> TextField.  If
> > it reverts to defaultTextFormat, then that might be where the 
> problem is.
> > 
> > Was commitProperties even being called when you had the format 
> code in there
> > because I don't see where it would be called from in your code.
> > 
> > Sorry for the scattered advice.
> > 
> > - Dan Freiman
> > 
> > 
> > On 9/16/07, Alex Harui <aharui@> wrote:
> > >
> > >    Unless you want mixed formats, use styles instead of 
> TextFormat, as the
> > > style system might be overriding your TextFormat.  Also see the
> > > stylesFunction example on my blog (blogs.adobe.com/aharui)
> > >
> > >
> > >  ------------------------------
> > >
> > > *From:* [email protected] [mailto:flexcompone
> > > [EMAIL PROTECTED] *On Behalf Of *jandersen1978
> > > *Sent:* Saturday, September 15, 2007 10:09 PM
> > > *To:* [email protected]
> > > *Subject:* [flexcomponents] Where/when to apply TextFormats in 
> custom
> > > component?
> > >
> > >
> > >
> > > I've been struggling through one of my first custom 
components, a
> > > custom List item renderer for a simple list of chat messages 
> with help
> > > from the Alex Harui of flex coders list (included below for
> > > reference). It's pretty much working now but I'm pretty sure 
> there's
> > > room to improve.
> > >
> > > I want to apply a TextFormat to the two TextFields that I'm 
> using in
> > > the renderer. Currently I'm applying it in the 
UpdateDisplayList
> > > method which is working but I'm sure it's very inefficient.
> > >
> > > First I tried applying the TextFormat object in the 
> createChildren
> > > method but it seems to have no effect there (tried applying it 
> both
> > > before and after calls to addChild()... ).
> > >
> > > I tried applying it in the commitProperties method which seems 
> to make
> > > sense since the logic is only executed once there but I find 
> that when
> > > the logic is there only the last chat message in the list 
takes 
> the
> > > proper format, all others take the TextField default. Really 
not 
> sure
> > > why existing instances of the item rendered are apparently 
losing
> > > their formatting as each new instance of the renderer is added 
> to the
> > > list... Can anyone comment on why that might be happening?
> > >
> > > Where is the best spot to apply text formatting to textFields 
in 
> a
> > > custom component?
> > >
> > > package org.rolera.kys.components
> > > {
> > > import mx.core.UIComponent;
> > > import mx.core.IDataRenderer;
> > > import mx.events.FlexEvent;
> > > import mx.controls.listClasses.IListItemRenderer;
> > > import mx.containers.HBox;
> > > import flash.text.TextField;
> > > import flash.text.TextFieldAutoSize;
> > > import flash.text.TextFormat;
> > >
> > > public class MessageRendererBase extends UIComponent implements
> > > IDataRenderer, IListItemRenderer
> > > {
> > > protected var _usr:TextField;
> > > protected var _msg:TextField;
> > >
> > > public function MessageRendererBase(){
> > >
> > > }
> > >
> > > protected override function createChildren():void{
> > > _usr = new TextField();
> > > _msg = new TextField();
> > > _msg.wordWrap = true;
> > >
> > > addChild(_usr);
> > > addChild(_msg);
> > >
> > >
> > > }
> > >
> > > protected override function commitProperties():void{
> > >
> > > }
> > >
> > > protected override function measure():void{
> > > if(!isNaN(this.explicitWidth)){
> > >
> > > _usr.width = Math.round(this.explicitWidth * .19);
> > > _msg.width = Math.round(this.explicitWidth * .8);
> > > }
> > > this.measuredWidth = _usr.textWidth + _msg.textWidth;
> > > this.measuredHeight = _msg.textHeight;
> > > }
> > >
> > > protected override function updateDisplayList
> (unscaledWidth:Number,
> > > unscaledHeight:Number):void{
> > > super.updateDisplayList(unscaledWidth, unscaledHeight);
> > > var tf:TextFormat = new TextFormat();
> > > trace('expWidth: ' + this.explicitWidth + ' msgHeight: ' +
> > > _msg.height, _msg.text);
> > > tf.font = "Arial";
> > > _usr.setTextFormat(tf);
> > > _msg.setTextFormat(tf);
> > > _msg.x = _usr.width + 3;
> > >
> > > }
> > >
> > > // Internal variable for the property value.
> > > private var _data:Object;
> > >
> > > // Make the data property bindable.
> > > [Bindable("dataChange")]
> > >
> > > // Define the getter method.
> > > public function get data():Object {
> > > return _data;
> > > }
> > >
> > > // Define the setter method, and dispatch an event when the 
> property
> > > // changes to support data binding.
> > > public function set data(value:Object):void {
> > > _data = value;
> > > dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
> > > if(value != null){
> > > _usr.text = value.fromName;
> > > _msg.text = value.message;
> > > _usr.textColor = _msg.textColor = value.color;
> > > }
> > > }
> > >
> > > }
> > > }
> > >
> > >  
> > >
> >
>


Reply via email to