Skip to content

Instantly share code, notes, and snippets.

@heartcode
Created March 1, 2012 15:16
Show Gist options
  • Save heartcode/1950439 to your computer and use it in GitHub Desktop.
Save heartcode/1950439 to your computer and use it in GitHub Desktop.
A simple class for creating TLFTextFields
package com.waste.assets.tlf
{
import fl.text.*;
import flash.text.*;
import flash.text.engine.FontLookup;
import flashx.textLayout.formats.*;
import flashx.textLayout.formats.TextLayoutFormat;
import flashx.textLayout.elements.TextFlow;
/**
* ...
* @author ...
*/
public class TLFFactory
{
/**
* Creates a TLFTextField with the passed formatting
* @param fontName
* @param color
* @param size
* @return
*/
public static function create(fontName:String, color:Number, size:uint, align:String="right"):TLFTextField {
var tlf:TLFTextField = new TLFTextField();
tlf.antiAliasType = AntiAliasType.ADVANCED;
tlf.direction = Direction.RTL;
tlf.embedFonts = true;
tlf.border = true;
tlf.selectable = false;
tlf.mouseEnabled = false;
var fmt:TextLayoutFormat = new TextLayoutFormat();
fmt.fontLookup = FontLookup.EMBEDDED_CFF;
fmt.color = color;
fmt.textAlign = align === TextFormatAlign.LEFT || align === TextFormatAlign.RIGHT || align === TextFormatAlign.CENTER ? align : TextFormatAlign.LEFT;
fmt.fontFamily = fontName;
fmt.fontSize = size;
var flow:TextFlow = tlf.textFlow;
flow.invalidateAllFormats();
flow.hostFormat = fmt;
flow.flowComposer.updateAllControllers();
return tlf;
};
/**
* Initializes the textfield
* @param tf
* @param align
*/
public static function initTLF(tlf:TLFTextField, fontName:String, align:String = "right"):void
{
tlf.antiAliasType = AntiAliasType.ADVANCED;
tlf.direction = Direction.RTL;
tlf.selectable = false;
tlf.embedFonts = true;
tlf.borderColor = 0xff00ff;
tlf.border = false;
var fmt:TextLayoutFormat = new TextLayoutFormat();
fmt.fontLookup = FontLookup.EMBEDDED_CFF;
fmt.textAlign = align;
fmt.fontFamily = fontName;
var flow:TextFlow = tlf.textFlow;
flow.invalidateAllFormats();
flow.hostFormat = fmt;
flow.linkNormalFormat = { color:0x44A7CA, textDecoration:TextDecoration.NONE };
flow.flowComposer.updateAllControllers();
};
/**
* Sets the content of the textfield
* @param tf
* @param text
*/
public static function setText(tlf:TLFTextField, text:String):void
{
tlf.htmlText = text;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment