Ahmad Flex

Flex Ideas

Flex Scrolling Label (Marquee)

Posted by ahmadflex on May 28, 2009

var x;


Bookmark and Share

Hi,

This Label control, is specially created to marquee when text is truncated and mouse is over the label as a cool way instead of the tooltip thing in flex that appears when text is trucated. you can also set scrollAlways property to true to get this to always scroll.

change property “scrollSeperationText” to change the “…” that appears before text gets repeated again, and property “scrollFramesPerMove” can be changed to change the marquee effect speed.

Please note this does not support the htmlText.

example , in the window that will popup click on the file icon file Icon to save and see the example swf

Here is the class code:

////////////////////////////////////////////////////////////////////////////////
//
//  Ahmad Khudairy
//  Copyright 2009 Ahmad Flex
//  All Rights Reserved.
//
//  NOTICE: You can use, and modify this file. Just please
//          Keep this comment undeleted.
//
////////////////////////////////////////////////////////////////////////////////

import flash.events.Event;
import flash.events.MouseEvent;

import mx.controls.Label;

public class ScrollLabel extends Label
{
//————————————————————————–
//
//  Constructor
//
//————————————————————————–

public function ScrollLabel()
{
super();

scrollWhenTruncated = true;

truncateToFit = true;
}

//————————————————————————–
//
//  Variables
//
//————————————————————————–

/**
* @private
* Holds reference of scrolling frame
*/
private var currentFrame:int=0;

/**
* @private
* Holds reference of scrolling index
*/
private var currentStartIndex:int = 0;

/**
* @private
* Holds the text value
*/
private var originalText:String = “”;

/**
* @private
* True if truncation happened to text
*/
protected var truncated:Boolean;

/**
* If text is scrolling this will be true, and fasle otherwise
*/
public var isScrolling:Boolean;

//————————————————————————–
//
//  Properties
//
//————————————————————————–

/**
* Number of frames to wait before scrolling text each time. <br>
* A value of 1 is the fastest
*/
public var scrollFramesPerMove:int = 5;

/**
* Text that will seperate scroll rounds
*/
public var scrollSeperationText:String = “       …         “;

/**
* @private
*/
private var _scrollWhenTruncated:Boolean = false;

/**
* If set to true, then text will scroll when mouse is over the label
* and text was truncated
*/
public function get scrollWhenTruncated():Boolean
{
return _scrollWhenTruncated;
}

/**
* @private
*/
public function set scrollWhenTruncated(value:Boolean):void
{
if (_scrollWhenTruncated == value)
{
return;
}

_scrollWhenTruncated = value;

if (_scrollWhenTruncated)
{
scrollAlways = false;
}

addMouseOverHandlers(_scrollWhenTruncated);
}

/**
* @private
*/
var _scrollAlways:Boolean;

/**
* If set to true, then text will always scroll
*/
public function get scrollAlways():Boolean
{
return _scrollAlways;
}

/**
* @private
*/
public function set scrollAlways(value:Boolean):void
{
if (_scrollAlways == value)
{
return;
}

_scrollAlways = value;

if (_scrollAlways)
{
scrollWhenTruncated = false;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
else
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}

//————————————————————————–
//
//  Overriden Methods
//
//————————————————————————–

override public function set text(value:String):void
{
super.text = value;
originalText = text;
}

override public function set htmlText(value:String):void
{
trace(“Scrolling is not supported for htmlText”);
super.htmlText = value;
}

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
textField.text = originalText;
truncated = textField.truncateToFit();
}

//————————————————————————–
//
//  Methods
//
//————————————————————————–

/**
* @private
* Adds/Removes event listeners to <code>MouseEvent.ROLL_OVER</code> and
* <code>MouseEvent.ROLL_OUT</code> events
*
* @param listenToMouseOver if true then it adds the event listeners and
* removes them otherwise
*/
private function addMouseOverHandlers(listenToMouseOver:Boolean):void
{
if (listenToMouseOver)
{
addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
}
else
{
removeEventListener(MouseEvent.ROLL_OVER, onMouseOver);
removeEventListener(MouseEvent.ROLL_OUT, onMouseOut);
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}

/**
* @private
* Handles <code>MouseEvent.ROLL_OVER</code> event. It add an event
* listener to <code>Event.ENTER_FRAME</code> if text was truncated.
*
* @param event MouseEvent object
*/
private function onMouseOver(event:MouseEvent):void
{
if (truncated)
{
addEventListener(Event.ENTER_FRAME, onEnterFrame);
isScrolling = true;
super.text = text + ” “;
}
}

/**
* @private
* Handles <code>MouseEvent.ROLL_OUT</code> event. It removes event
* listener to <code>Event.ENTER_FRAME</code>, and reset <code>text</code>
*
* @param event MouseEvent object
*/
private function onMouseOut(event:MouseEvent):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
super.text = originalText;
isScrolling = false;
currentStartIndex = 0;
currentFrame = 0;
}

/**
* @private
* Handles <code>Event.ENTER_FRAME</code> event. It calls the
* <code>scrollText</code> function
*
* @param event Event object
*/
private function onEnterFrame(event:Event):void
{
scrollText();
}

/**
* @private
* Scrolls the text
*/
private function scrollText():void
{
currentFrame++;
if (currentFrame == scrollFramesPerMove)
{
if (currentStartIndex < originalText.length)
{
textField.text = originalText.substring(currentStartIndex, originalText.length ) + scrollSeperationText + originalText;
currentStartIndex++;
}
else if (currentStartIndex >= originalText.length)
{
textField.text = scrollSeperationText.substring(currentStartIndex – originalText.length, scrollSeperationText.length ) + originalText;
currentStartIndex++;

if (currentStartIndex > originalText.length + scrollSeperationText.length)
{
currentStartIndex = 1;
}
}

if (!truncated)
{
var i:int;
var scrollSeperationTextLength:int = scrollSeperationText.length;
var fullLength:int = scrollSeperationTextLength + originalText.length;
var tempText:String = textField.text;
var textIsFit:Boolean = false;

while (!textIsFit)
{
for (i = 0; i < fullLength; i ++)
{
if (i < scrollSeperationTextLength)
{
tempText = tempText + scrollSeperationText.charAt(i);
}
else
{
tempText = tempText + originalText.charAt(i – scrollSeperationTextLength);
}

textField.text = tempText;
if (textField.truncateToFit())
{
textIsFit = true;
textField.text = tempText;
break;
}
}
}
}

currentFrame = 0;
}
}
}

Bookmark and Share

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.