<?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>Easy Flex &#187; baseline</title>
	<atom:link href="http://evtimmy.com/tag/baseline/feed/" rel="self" type="application/rss+xml" />
	<link>http://evtimmy.com</link>
	<description>Evtim on Flex SDK</description>
	<lastBuildDate>Thu, 03 Nov 2011 07:18:58 +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>Extending HorizontalLayout to Support Baseline (Align to Text)</title>
		<link>http://evtimmy.com/2010/02/extending-horizontallayout-to-support-baseline-align-to-text/</link>
		<comments>http://evtimmy.com/2010/02/extending-horizontallayout-to-support-baseline-align-to-text/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 08:06:42 +0000</pubDate>
		<dc:creator>Evtim</dc:creator>
				<category><![CDATA[Custom Layout]]></category>
		<category><![CDATA[Spark Layouts]]></category>
		<category><![CDATA[align to text]]></category>
		<category><![CDATA[alignment]]></category>
		<category><![CDATA[baseline]]></category>
		<category><![CDATA[Layout]]></category>

		<guid isPermaLink="false">http://evtimmy.com/?p=265</guid>
		<description><![CDATA[The current Spark HorizontalLayout unfortunately doesn&#8217;t support baseline alignment. If I want to have, for example, a Label and a Button positioned so that their text aligns, I usualy use absolute position (nudging things up or down) or baseline constraints with BasicLayout/Canvas. But then again, I always find myself needing this feature in a HorizontalLayout [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://evtimmy.com/wordpress/wp-content/uploads/2010/02/hlayoutbaseline/HLayoutBaseline.swf"><img class="alignleft size-thumbnail wp-image-266" style="margin-left: 10px; margin-right: 10px;" title="hlayoutbaseline" src="http://evtimmy.com/wordpress/wp-content/uploads/2010/02/hlayoutbaseline-150x150.png" alt="hlayoutbaseline" width="150" height="150" /></a>The current Spark <em>HorizontalLayout </em>unfortunately doesn&#8217;t support <em>baseline </em>alignment. If I want to have, for example, a <em>Label </em>and a <em>Button</em> positioned so that their text aligns, I usualy use absolute position (nudging things up or down) or <em>baseline </em>constraints with <em>BasicLayout</em>/<em>Canvas</em>.  But then again, I always find myself needing this feature in a <em>HorizontalLayout </em>and so I finally decided to tackle it on. It turns out it&#8217;s pretty easy to extend the stock Spark <em>HorizontalLayout</em>, not to mention using it is a breeze with the Spark layout architecture (yay Spark!).<span id="more-265"></span></p>
<h3>Baseline Concepts in Flex</h3>
<ul>
<li>Each <em>IVisualElement </em>has a <em>baselinePosition</em>. This is the vertical distance from element&#8217;s top (origin) to its text base.</li>
<li>To align an <em>IVisualElement</em>&#8216;s text to a specific point within its parent, say A=100, we roughly do the following <span style="color: #ff6600;">element.y = A &#8211; element.baselinePosition</span>.</li>
<li>Each IVisualElement has a <em>baseline</em> constraint. How this is going to be used is up to the parent layout. By default <em>BasicLayout</em>/<em>Canvas </em>interpret it as the position within the parent to which the element&#8217;s text base should be aligned, meaning that the actual calculation is <span style="color: #ff6600;">element.y = element.baseline &#8211; element.baselinePosition</span>.</li>
</ul>
<h3>What I Want</h3>
<ol>
<li>Still keep all the cool functionality of the HorizontalLayout without copying code.</li>
<li>Ability to specify a <span style="color: #339966;"><em>global baseline</em></span>. The layout should position all the elements such that their text aligns to that <span style="color: #339966;"><em>global baseline</em></span>. I also want the ability to individually offset the elements from the <em><span style="color: #339966;">global baseline</span></em>. I want the element&#8217;s <em>baseline </em>constraint, if specified, to serve as such an offset.</li>
<li>If I&#8217;m too lazy to specify the <em><span style="color: #339966;">global baseline</span></em>, the layout should calculate it for me. In that case there shouldn&#8217;t be any extra space around the elements and the layout&#8217;s borders.</li>
<li>The <span style="color: #339966;"><em>global baseline</em></span> should be <span style="color: #0000ff;">[Bindable]</span>. Since the layout is calculating it in certain cases, I want to be able to read it back whenever its value changes.</li>
</ol>
<p>Here&#8217;s an example of the layout in action:  <a href="http://evtimmy.com/wordpress/wp-content/uploads/2010/02/hlayoutbaseline-simple.png"><img class="alignleft size-full wp-image-297" title="hlayoutbaseline-simple" src="http://evtimmy.com/wordpress/wp-content/uploads/2010/02/hlayoutbaseline-simple.png" alt="hlayoutbaseline-simple" width="232" height="47" /></a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Application</span> xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #000000;">               xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #000000;">               xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span></span>
<span style="color: #000000;">               xmlns:local=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:BorderContainer</span> horizontalCenter=<span style="color: #ff0000;">&quot;0&quot;</span> verticalCenter=<span style="color: #ff0000;">&quot;0&quot;</span></span>
<span style="color: #000000;">                       minWidth=<span style="color: #ff0000;">&quot;0&quot;</span> minHeight=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:layout</span><span style="color: #7400FF;">&gt;</span></span>
             <span style="color: #000000;"><span style="color: #7400FF;">&lt;local:HBaselineLayout</span> verticalAlign=<span style="color: #ff0000;">&quot;baseline&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:layout</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:CheckBox</span> label=<span style="color: #ff0000;">&quot;hello&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> label=<span style="color: #ff0000;">&quot;button&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
        <span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> text=<span style="color: #ff0000;">&quot;and label&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:BorderContainer</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></td></tr></table></div>

<h3>Implementation</h3>
<ol>
<li>I extended the <em>HorizontalLayout </em>class and had to override the <em>verticalAlign </em>property getter in order to modify the enum metadata and add &#8220;baseline&#8221; as an option.</li>
<li>I added a <span style="color: #339966;"><em>globalBaseline</em> </span>property. I had to override the <em>updateDisplayList()</em> method. After the super.updateDisplayList() has finished with sizing and positioning the elements, I&#8217;d iterate over them and position them using something like this <span style="color: #ff6600;">element.setLayoutBoundsPosition(element.getLayoutBoundsX(), globalBaseline + element.baseline &#8211; element.baselinePosition)</span>.</li>
<li>Calculating the <span style="color: #339966;"><em>globalBaseline </em></span>is easy enough. If all the elements are aligned at the <span style="color: #339966;"><em>globalBaseline</em></span>, then a certain portion of each element will be above that line and the rest will be below the line &#8211; top portion and bottom portion. Then I have <span style="color: #ff6600;">topPortion = element.baseline &#8211; element.baselinePosition</span> and <span style="color: #ff6600;">bottomPortion = element.height &#8211; element.baseline + element.baselinePosition</span>. Now since I don&#8217;t want any extra space around the elements I want the element that sticks out the most above the <span style="color: #339966;"><em>globalBaseline </em></span>to touch the top edge of the container, while the element that sticks out the most below the <span style="color: #339966;"><em>globalBaseline </em></span>to touch the bottom edge of the container. Therefore the <span style="color: #339966;"><em>globalBaseline </em></span>is going to be the maximum of the top portions, while the measured height of the container is going to be the<span style="color: #ff6600;"> <span style="color: #339966;"><em>globalBaseline </em></span></span>+ the maximum of the bottom portions. I created a helper function to calculate the maximums of the top and bottom portions and used it in the overridden <em>measure()</em> and <em>updateDisplayList()</em> methods.</li>
<li>Finally, I added a property <span style="color: #339966;"><em>actualBaseline </em></span>and made sure to dispatch an event when it changes. I update the <em><span style="color: #339966;">actualBaseline </span></em>at the end of <em>updateDisplayList()</em> so that at the time of the event it&#8217;s certain that all elements are already sized and positioned correctly.</li>
</ol>
<p>Here&#8217;s an <a href="http://evtimmy.com/wordpress/wp-content/uploads/2010/02/hlayoutbaseline/HLayoutBaseline.swf">example</a> of a working app, to get the source right-click on the application and select &#8220;view source&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://evtimmy.com/2010/02/extending-horizontallayout-to-support-baseline-align-to-text/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

