Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Text formatting disappears when I clear my textfield

Posted on 2013-06-04
10
Medium Priority
?
377 Views
Last Modified: 2013-06-17
Hey there.

Hope someone can help me here..

I have a textfield with some defaultTextFormat – if the user hasn't typed anything in the textfield the textfield will have the following text: "Write caption here.." - this text is in grey.

What I want is that if the user clicks the textfield the text goes away and the text color changes to black when the user starts typing. The annoying thing is that If I keep one character in the textfield it changes the color correctly to black, but if I clear the textfield as I want to the new textFormat is not applied.

Please have a look at the code here:

// activate caption textfield
		private function activateCapTxtFld(e:FocusEvent):void {
			trace("activateTxtFld: captionNotSet = "+captionNotSet);
			if(captionNotSet) {
				captionTxt.text = "test"; // the new textformat is applied
				//captionTxt.text = ""; // the new textformat is NOT applied
				var TF:TextFormat = new TextFormat();
				TF.color = 0x000000;
				captionTxt.setTextFormat(TF);
			}
			e.target.removeEventListener(FocusEvent.FOCUS_IN, activateCapTxtFld);
		}

Open in new window



I hope someone can help me on this..

Thanks..
0
Comment
Question by:RapperOnStep
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39226197
Without a link to the page so we can see what is happening, there is nothing we can do but guess.  My guess is that the text is a placeholder in a textarea, and the behavior you are describing in the native implementation in a modern browser, and AFAIK it is not overridable.

Cd&
0
 

Author Comment

by:RapperOnStep
ID: 39231248
hey Cd&.

Thanks for your answer. Just cleaned up the coding and attaching an example here with all the code neccessary. I hope you can help me shine some light on this problem.

package {
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextFormat;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.AntiAliasType;
	import flash.events.FocusEvent;
	
	
	public class setSelection2 extends MovieClip {
		
		private var captionNotSet:Boolean;
		private var headlineNotSet:Boolean;
		private var captionTxtFormat:TextFormat;
		private var headlineTxtFormat:TextFormat;
		private var defaultTxtFormat:TextFormat;
		public var headlineTxtFld:TextField;
		public var captionTxt:TextField;
		private var caption:String;
		private var headline:String;
		
		
		public function setSelection2() {
			setDefaultTxt();
			addCaptionTxtFld();
		}
		private function setDefaultTxt():void {
			caption = "Write caption here..";
			captionNotSet = true;
		}
		
		private function setDefaultTxtFormat(textIsNotSet:Boolean):void {
			
			trace("setDefaultTxtFormat");
			defaultTxtFormat = new TextFormat();
			defaultTxtFormat.font = "Georgia";
			defaultTxtFormat.size = 16;
			if(textIsNotSet) {
				defaultTxtFormat.color = 0x666666;
			}
			else {
				defaultTxtFormat.color = 0x000000;
			}
			
		}
		
		
		
		// addCaptionTxtFld
		private function addCaptionTxtFld():void {
			captionTxt = new TextField();
			captionTxt.type = TextFieldType.INPUT;
			captionTxt.height = 55;
			captionTxt.background = true;
			captionTxt.border = true;
			captionTxt.wordWrap = true;
			captionTxt.antiAliasType = AntiAliasType.ADVANCED;
			captionTxt.width = 500;
			captionTxt.height = 300;
			captionTxt.x = 20;
			captionTxt.y = 20;
			captionTxt.htmlText = caption;
			setDefaultTxtFormat(captionNotSet);
			captionTxt.setTextFormat(defaultTxtFormat);
			captionTxt.addEventListener(FocusEvent.FOCUS_IN, activateCapTxtFld);
			addChild(captionTxt);
			captionTxt.defaultTextFormat = defaultTxtFormat;
		}
		
		
		// activate caption textfield
		private function activateCapTxtFld(e:FocusEvent):void {
			if(captionNotSet) {
				//captionTxt.text = "test"; // the new textformat is applied
				captionTxt.text = ""; // the new TF textformat is NOT applied
				var TF:TextFormat = new TextFormat();
				TF.color = 0x000000;
				TF.size = 20;
				captionTxt.setTextFormat(TF);
			}
			e.target.removeEventListener(FocusEvent.FOCUS_IN, activateCapTxtFld);
		}
		
		
	}
}

Open in new window

0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39242004
Still waiting for a link.

Cd&
0
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 

Author Comment

by:RapperOnStep
ID: 39242136
Why do u need a link? I posted all the code to make it easier..
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39242180
Fine. I am moving on to other things.  Good luck finding an answer.

Cd&
0
 

Author Comment

by:RapperOnStep
ID: 39242458
Wauw... I'm not an expert - that's why I'm asking.
0
 

Accepted Solution

by:
RapperOnStep earned 0 total points
ID: 39243818
I solved it myself. Strange behaviour - I never posted a link in here before. Always just the code.

Here is the solution:

package 
{

	import flash.display.*;
	import flash.events.*;
	import flash.text.TextFormat;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.AntiAliasType;
	import flash.events.FocusEvent;


	public class setSelection2 extends MovieClip
	{

		private var captionNotSet:Boolean;
		private var headlineNotSet:Boolean;
		private var captionTxtFormat:TextFormat;
		private var headlineTxtFormat:TextFormat;
		private var defaultTxtFormat:TextFormat;
		public var headlineTxtFld:TextField;
		public var captionTxt:TextField;
		private var caption:String;
		private var headline:String;


		public function setSelection2()
		{
			setDefaultTxt();
			addCaptionTxtFld();
		}
		private function setDefaultTxt():void
		{
			caption = "Write caption here..";
			captionNotSet = true;
		}

		private function setDefaultTxtFormat(textIsNotSet:Boolean):void
		{

			defaultTxtFormat = new TextFormat();
			defaultTxtFormat.font = "Georgia";
			defaultTxtFormat.size = 16;
			defaultTxtFormat.color = 0x666666;
		}



		// addCaptionTxtFld
		private function addCaptionTxtFld():void
		{
			captionTxt = new TextField();
			captionTxt.type = TextFieldType.INPUT;
			captionTxt.height = 55;
			captionTxt.background = true;
			captionTxt.border = true;
			captionTxt.wordWrap = true;
			captionTxt.antiAliasType = AntiAliasType.ADVANCED;
			captionTxt.width = 500;
			captionTxt.height = 300;
			captionTxt.x = 20;
			captionTxt.y = 20;
			captionTxt.htmlText = caption;
			setDefaultTxtFormat(captionNotSet);
			captionTxt.setTextFormat(defaultTxtFormat);
			captionTxt.addEventListener(MouseEvent.CLICK, activateCapTxtFld);
			addChild(captionTxt);
			captionTxt.defaultTextFormat = defaultTxtFormat;
		}


		// activate caption textfield
		private function activateCapTxtFld(e:MouseEvent):void
		{
			if (captionNotSet) {
				captionTxt.text = " ";
				var newformat:TextFormat = new TextFormat();
				newformat.color = 0x000000;
				captionTxt.setTextFormat(newformat);
				captionTxt.setSelection(0,0);
			}
			e.target.removeEventListener(MouseEvent.CLICK, activateCapTxtFld);
		}


	}
}

Open in new window

0
 

Author Closing Comment

by:RapperOnStep
ID: 39252629
For some reason I didn't get a solution from an expert so I had to solve it myself :(
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Six Sigma Control Plans

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question