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:[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