+1

Adding Clickable links and TextArea.html

Flex

I really wanted clickable links in my Twitter pod and I saw how easy Steve did it so I used his really nice regex. 

As per my original design I really wanted my Twitter feed to be a bit less generic so I remove my name from the start of each post and add a date stamp each entry looping over the 10 most recent tweets.

I'm quite happy with the result.

At first I was only using TextArea.text and not getting nice links when I saw there is also TextArea.html this is really nice and I can see many places I can use this regEx in any data centric application.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="180" height="194"
	creationComplete="remoteObject.getData()"  cornerRadius="16" 
	color="#000">
	<mx:RemoteObject id="remoteObject" destination="ColdFusion" source="paulcfc.Twitter" showBusyCursor="true">
		<mx:method name="getData" result="getDataSuccess(event)" />
	</mx:RemoteObject>
	<mx:Script>
		<![CDATA[
			import mx.formatters.DateFormatter;
			import mx.rpc.events.ResultEvent;
			import mx.collections.ArrayCollection;
			
			private var resultData:ArrayCollection;
			
			private function getDataSuccess(e:ResultEvent):void{
				resultData = e.result as ArrayCollection;
				for( var i:int = 0; i < 10; ++i){
					ta.text += niceDateFormatter.format(resultData.getItemAt(i).PUBLISHEDDATE) + 
					": " + resultData.getItemAt(i).CONTENT.replace("paulkukiel: ", "") + "\n\n";
				}
				ta.htmlText = URLsToLinks(ta.text);
			}
			
			private function URLsToLinks(theString:String):String {
	
				// define the RegEx strings
				var URLRE:String = "(https?)://([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+([a-zA-Z]{2,9})(:\\d{1,4})?([-\\w/#~:.?+=&%@~]*)";
				var twitterRE:String = "(?<![\\w\\.])(?<=@)([-\\w]*)(?![\\w\\.])";
				
				// Create the RegEx objects
				var urlPattern:RegExp = new RegExp(URLRE,"ig");
				var twitterPattern:RegExp = new RegExp(twitterRE,"ig");
				
				// replace text in the string that matches the RegEx objects
				var result:String = theString.replace(urlPattern,'<a href="$&" target="_blank"><font color="#0000FF"><u>$&</u></font></a>');
				result = result.replace(twitterPattern,'<a href="http://twitter.com/$&" target="_blank"><font color="#0000FF"><u>$&</u></font></a>');
			
				// return the formatted string
				return result;
			}
			
		]]>
	</mx:Script>
			
			<mx:DateFormatter id="niceDateFormatter" formatString="DD MMM" />
			<mx:LinkButton label="Twitter" textAlign="left" 
				click="navigateToURL(new URLRequest('http://twitter.com/paulkukiel'))" 
				y="0" x="10"/>
			<mx:TextArea id="ta" width="160" height="163" editable="false" y="21" x="10"/>
	
</mx:Application>

Steve said:
 
Glad to see the RegEx function getting some use! Nice widget too!
 
posted 125 days ago
Add Comment Reply to: this comment OR this thread
 

Search