Introducing the TextFieldLinkManager class
Yesterday, I spent a few hours working on a new class, TextFieldLinkManager. It overcomes a number of downsides with AS3’s TextField class, including a few bugs and some missing functionality. I know the name is a mouthful, but I feel it’d be best to follow standard practices and be as descriptive as possible.
The primary objective of TextFieldLinkManager is to provide individual links within a TextField with the same MouseEvents that InteractiveObjects are given. Currently, link clicks can only be listened to via the TextEvent.LINK event, but the link’s URL requires the “event:” prefix. On top of that, TextField links are known for their click issues. Sporadically, clicks are ignored, requiring multiple clicks, especially when using a Wacom pen.
TextFieldLinkManager dispatches events for all links, prefixed with “event:” or not, and fixes the click issues without the need for additional Sprites as hitAreas. Because of this, it only uses 152 bytes of memory. And, as you can see, it’s as simple as can be to implement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package app.test { import com.destroytoday.events.TextFieldLinkManagerEvent; import com.destroytoday.text.TextFieldLinkManager; import flash.text.TextField; public class Test extends Sprite { public var _TextField:TextField; public var _linkManager:TextFieldLinkManager; public function Test () { __construct (); __init (); } private function __construct ():void { _TextField = addChild (new TextField ()) as TextField; _linkManager = new TextFieldLinkManager (_TextField); } private function __init ():void { _linkManager.addEventListener (TextFieldLinkManagerEvent.LINK_OVER, __linkOver__); _linkManager.addEventListener (TextFieldLinkManagerEvent.LINK_OUT, __linkOut__); _linkManager.addEventListener (TextFieldLinkManagerEvent.LINK_CLICK, __linkClick__); _linkManager.addEventListener (TextFieldLinkManagerEvent.CONTEXT_MENU, __contextMenu__); } // // Event Handlers // } } |
After implementing TextFieldLinkManager in DestroyTwitter, I added a few extra functions that it really called for. Since I’ll be taking advantage of ContextMenus in the next release, I improved the TextField’s menu by providing the option to disable it. As a result, I can display a custom ContextMenu for TextFields, with the ability to add more useful items like “Preview Link” and remove the unnecessary ones like “Cut”, “Paste”, and “Delete”.
TextFieldLinkManager also fixes a ContextMenu bug that I came across recently. At times, sequential right-clicks will prevent the mouse’s position from updating. This results in a ContextMenu displaying in the wrong place. With my new-found method for displaying ContextMenus, I was able to set this issue to rest.
I’m certain this class will continue to improve over the next few days and I have nothing but high expectations for it. Stay tuned.
Can’t wait to see where this goes and the source code if you decide to post it.
hi! Don’t you have any live demo? If not, can you please tell me where I can find this class working on Destroy Twitter? Thank you!