<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>indieas &#187; textfield</title>
	<atom:link href="http://www.indieas.org/tag/textfield/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.indieas.org</link>
	<description>independent actionscript developer team</description>
	<lastBuildDate>Tue, 19 Oct 2010 13:43:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Onscreen Keyboard with AIR</title>
		<link>http://www.indieas.org/2009/11/onscreen-keyboard-with-air/</link>
		<comments>http://www.indieas.org/2009/11/onscreen-keyboard-with-air/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 18:20:20 +0000</pubDate>
		<dc:creator>mich</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[air]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[htmlloader]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[keyboard]]></category>
		<category><![CDATA[onscreen]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[textarea]]></category>
		<category><![CDATA[textfield]]></category>
		<category><![CDATA[touchschreen]]></category>

		<guid isPermaLink="false">http://www.indieas.org/?p=158</guid>
		<description><![CDATA[In one of my last projects I had to develop an air application with an onscreen keyboard that is working with flash as with the internal html browser of air. I realized pretty quickly that this isn&#8217;t that easy as it seams. There are multiple problems especially with the html view: how do i get [...]]]></description>
			<content:encoded><![CDATA[<p>In one of my last projects I had to develop an air application with an <strong>onscreen keyboard</strong> that is working with <strong>flash</strong> as with the <strong>internal html browser</strong> of air.</p>
<p>I realized pretty quickly that this isn&#8217;t that easy as it seams. There are multiple problems especially with the html view:</p>
<ul>
<li>how do i get the focus in the html view to add letters.</li>
<li>how do i get the selection to add chars on the right position.</li>
<li>how do i know when a user clicks and inputfield/textarea.</li>
<li>how do i prevent that flash do set the focus on the keyboard when I click on a letter sprite.</li>
</ul>
<p>After some experiments I discovered the really nice javascript support of the internal browser. So my first problem was already gone, when I found the javascript command:</p>
<pre class="brush: as3; title: ; notranslate">
var webView:HTMLLoader = new HTMLLoader();
//...later, to get the element with the focus call:
var element:Object = webView.window.document.activeElement;
</pre>
<p>I wasn&#8217;t really into javascript so I was surprised to discover even more possibilities. Now with the focus I can place an onscreen keyboard made with flash and get the activeElement of the htmlloader and add characters to it. something like this:</p>
<pre class="brush: as3; title: ; notranslate">
var inputField:Object = webView.window.document.activeElement;
if( inputField != null ) {
   var select:Number = addScreenBoardText( e.char, inputField, &quot;value&quot;, &quot;selectionStart&quot;, &quot;selectionEnd&quot; );
   inputField.setSelectionRange( select, select );
}
</pre>
<p>The function where I add the char and return the selection where the cursor should be set:</p>
<pre class="brush: as3; title: ; notranslate">
private function addScreenBoardText( insert:String, object:Object, textAttribute:String, selectionStartAttribute:String, selectionEndAttribute:String ):Number {
  var selectionStart:Number = object[ selectionStartAttribute ];
  var selectionEnd:Number = object[ selectionEndAttribute ];
  var original:String = object[ textAttribute ];
  var newText:String = original.substring( 0, selectionStart ) + insert + original.substr( selectionEnd );
  object[ textAttribute ] = newText;
  return selectionStart + insert.length;
}
</pre>
<p>Why I make this weird looking function is because I can use it also for the flash TextField just with different parameters. So now we can add letters to a textfield in a html view as a flash TextField. But one big problem is that we loose the focus every time we click on our on screen keyboard. And there is a way to prevent the change of the focus which is very handy in this situation:</p>
<pre class="brush: as3; title: ; notranslate"> // in our main application class we listen to the mouse focus change event.
addEventListener( FocusEvent.MOUSE_FOCUS_CHANGE, onMouseFocus );

// in the listener we check if the relatedObject is a Key,Screenboard ( these are my classes for the Keyboard that get mouseclicks )
// then we prevent the default behaviour of the event. which means no focus change is triggered.
private function onMouseFocus( fe:FocusEvent ):void {
if( fe.relatedObject is Key || fe.relatedObject is ScreenBoard ) fe.preventDefault();
}
</pre>
<p>We saw earlier that is was possible to write to javascript objects as normal in actionscript. I was even more surprised as its possible to listen to javascript events with actionscript functions. That allows me to listen to javascript focus changes and hide/show the keyboard. This way I can make an overlay keyboard that is only visible when I click in a textfield which I find is a very nice solution. So this is how I listened to the javascript events ( keep in mind that this is all actionscript ):</p>
<pre class="brush: as3; title: ; notranslate">
// register everytime the page has loaded!
private function onPageLoaded( e:Event ):void {
  webView.window.addEventListener( &quot;focus&quot;, onJavaScriptFocusIn, true );
  webView.window.addEventListener( &quot;blur&quot;, onJavaScriptFocusOut, true );
 }

// if we get a focus in event from js, check if its a inputfield or a textarea. then show the keyboard.
private function onJavaScriptFocusIn( e:Object ):void {
  if( e.target.localName == &quot;input&quot; || e.target.localName == &quot;textarea&quot; ) {
    dispatchEvent( new JavaScriptEvent( JavaScriptEvent.FOCUS_IN ) );
  }
}

// we lost focus - hide keyboard
private function onJavaScriptFocusOut( e:Object ):void {
  dispatchEvent( new JavaScriptEvent( JavaScriptEvent.FOCUS_OUT ) );
}
</pre>
<p>With all this you can build your onscreen keyboard that is working in <strong>flash as in html</strong>. I made a<a href="http://www.indieas.org/wp-content/uploads/2009/11/screenboard.zip"> quick flexbuilder air project</a> that you can download with a keyboard, textfield and htmlloader.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.indieas.org/2009/11/onscreen-keyboard-with-air/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>TextField, StyleSheet and TextFormat</title>
		<link>http://www.indieas.org/2009/09/textfield-stylesheet-and-textformat/</link>
		<comments>http://www.indieas.org/2009/09/textfield-stylesheet-and-textformat/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 09:08:35 +0000</pubDate>
		<dc:creator>mich</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[defaultTextFormat]]></category>
		<category><![CDATA[stylesheet]]></category>
		<category><![CDATA[textfield]]></category>
		<category><![CDATA[textformat]]></category>

		<guid isPermaLink="false">http://www.indieas.org/?p=88</guid>
		<description><![CDATA[I&#8217;m sure that everybody that ever try to apply a stylesheet to a textfield in Flash encountered some problems. Thats why I though I will write a summary about the workaround that I found so far. First thing to know is how flash can apply css on text. If you set for example: You can [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure that everybody that ever try to apply a stylesheet to a textfield in Flash encountered some problems. Thats why I though I will write a summary about the workaround that I found so far.</p>
<p>First thing to know is how flash can apply css on text. If you set for example:</p>
<pre class="brush: as3; title: ; notranslate">
textField.text = &quot;Hello is it &lt;b&gt;me&lt;/b&gt; you &lt;h1&gt;looking&lt;/h1&gt; for.&quot;;
</pre>
<p>You can style the text between tags here: <em>&lt;b&gt;..&lt;/b&gt;</em> and <em>&lt;h1&gt;&#8230;&lt;/h1&gt;</em> if you define css definition for these tags. An example css file would look like:</p>
<pre class="brush: css; title: ; notranslate">
b {  font-weight: bold; }
h1 {  size:16; }
</pre>
<p>and the corresponding as source to set the styleSheet would be:</p>
<pre class="brush: as3; title: ; notranslate">
private function onLoaded( e:Event ):void {
// The TextLoader is a class from the indieas library
var loader:TextLoader = e.target as TextLoader;
var css:StyleSheet = loader.getDataAsCSS();
textField.styleSheet = css;
textField.text = &quot;Hello is it &lt;b&gt;me&lt;/b&gt; you &lt;h1&gt;looking&lt;/h1&gt; for.&quot;;
}
</pre>
<p>Here its important that you set the text property after setting the styleSheet otherwise it will not apply the styles.</p>
<p>So far everything works as expected but what if you also want to style the normal text without any tags. You could wrap the text content in another tag like:</p>
<pre class="brush: xml; title: ; notranslate">&lt;text&gt;Hello is it &lt;b&gt;me&lt;/b&gt; you &lt;h1&gt;looking&lt;/h1&gt; for.&lt;/text&gt;</pre>
<p>But thats not very handy instead we use the TextField own property defaultTextFormat. We can transform a css into a TextFormat and apply that as defaultTextFormat like this:</p>
<pre class="brush: as3; title: ; notranslate">
var styleObject:Object = css.getStyle( &quot;*&quot; );
var defaultStyle:TextFormat = css.transform( styleObject );
textField.defaultTextFormat = defaultStyle;
textField.styleSheet = css;
textField.text = &quot;Hello is it &lt;b&gt;me&lt;/b&gt; you &lt;h1&gt;looking&lt;/h1&gt; for.&quot;;
</pre>
<p>Again here the order of applying defaultTextFormat, styleSheet and text is important. If you have a styleSheet applied and you want to set the defaultTextFormat it will throw an error. So if you want to reapply the defaultTextFormat do it like this:</p>
<pre class="brush: as3; title: ; notranslate">
var style:StyleSheet = textField.styleSheet; // save the styles to reapply them later
textField.styleSheet = null; // set this null to allow seting the defaultTextFormat
textField.defaultTextFormat = // new format;
textField.styleSheet = style; // reapply styles
textField.text = textField.text; // reapply text
</pre>
<p>The last thing to mention is the textField.condenseWhite property. Very often you load your content from an xml file and like to show a node content in a textField. But as the xml itself has linespaces and breaks the shown text is weird formatted. To avoid this use textField.condenseWhite = true. To make a new line just add a &lt;br/&gt; tag to your xml content and make sure your textfield has multiline set to true.</p>
<p><strong>Summary:</strong></p>
<ul>
<li>use defaultTextFormat for default text style.</li>
<li>transform the defaultTextFormat from a StyleSheet style.</li>
<li>properties set order: defaultTextFormat, styleSheet, text.</li>
<li>set condenseWhite = true when setting xml content to the texfield.</li>
</ul>
<p>To play around download a demo FlexBuilder project <a href="http://www.indieas.org/wp-content/uploads/2009/09/textfield_css_demo.zip" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.indieas.org/2009/09/textfield-stylesheet-and-textformat/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Problem with TextField.textWidth/TextField.width</title>
		<link>http://www.indieas.org/2009/08/problem-with-textfield-textwidthtextfield-width/</link>
		<comments>http://www.indieas.org/2009/08/problem-with-textfield-textwidthtextfield-width/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 10:12:25 +0000</pubDate>
		<dc:creator>mich</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[problem]]></category>
		<category><![CDATA[textfield]]></category>
		<category><![CDATA[width]]></category>

		<guid isPermaLink="false">http://www.indieas.org/?p=22</guid>
		<description><![CDATA[The problem: Recently I struggled over another problem with the TextField where the textWidth / width was not calculated right. On the screenshot you can see textfields stacked from the left to the right corresponding to the width of the textfield. If you look closely to the borders you see that the leading and tailing [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The problem:</strong></p>
<p>Recently I struggled over another problem with the TextField where the textWidth / width was not calculated right.</p>
<p><img class="alignnone size-full wp-image-24" title="Picture" src="http://www.indieas.org/wp-content/uploads/2009/08/Picture.png" alt="Picture" width="600" height="34" /></p>
<p>On the screenshot you can see textfields stacked from the left to the right corresponding to the width of the textfield. If you look closely to the borders you see that the leading and tailing spaces around the words are not equal at all. Especially at the word &#8220;Newsletter&#8221; the tailing space too small. The texfield was setup like this;</p>
<pre class="brush: as3; title: ; notranslate">
label.autoSize = TextFieldAutoSize.LEFT;
label.wordWrap = false;
label.multiline = false;
label.antiAliasType = AntiAliasType.ADVANCED;
label.gridFitType = GridFitType.PIXEL;
label.embedFonts = true;
label.border = true;
var test:TextFormat = new TextFormat();
test.kerning = true;
test.font = &quot;Futura&quot;;
label.setTextFormat( test );
</pre>
<p><strong>The solution:</strong></p>
<p>It seems that this is a flash bug and you can get over this by changing the <strong>GridFitType.PIXEL</strong> to<strong> GridFitType.SUBPIXEL</strong>.<br />
The resulting textfields look like this;</p>
<p><img class="alignnone size-full wp-image-23" title="Picture 1" src="http://www.indieas.org/wp-content/uploads/2009/08/Picture-1.png" alt="Picture 1" width="586" height="33" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.indieas.org/2009/08/problem-with-textfield-textwidthtextfield-width/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

