Home > Flash Builder > Tired of Typing? No More left=”0″ right=”0″ top=”0″ bottom=”0″

Tired of Typing? No More left=”0″ right=”0″ top=”0″ bottom=”0″

August 15th, 2011 Leave a comment Go to comments

I often find myself typing the same old snipped of MXML code over and over again.  Useful nevertheless, I’m getting tired it!  If you are like me, it’s time to automate this.  Fortunately  Flash Builder code templates feature is the perfect solution!  Jump after the break to see my new love in action.

 

 

 

This is how I created my new favorite template:

  1. Go to FlashBuilder -> Preferences -> Flash Builder -> Editors -> Code Templates -> MXML
  2. Click new template
  3. Type “left” for the name
  4. Select “MXML attributes” from the Context drop-down
  5. Fill in suitable description
  6. Paste the template body:
    left="${values}" right="${values}" top="${values}" bottom="${values}" ${cursor}
  7. Hit “Apply” and close the dialog

 

Now I’m ready to use it – I start typing “left” and hit the ^space to bring up the suggestions…

 

…then I select my template and Flash Builder inserts the body and places my cursor on the first field…

 

…I enter the value – in this case a single “0” and Flash Builder fills in the values for all four attributes!

 

Finally, I press “tab” to get to the end of the code snippet and continue with the MXML.

Cheers!

 

PS : I looove code templates!  Almost as much as conditional breakpoints!!! 😉

 

  1. Florian
    August 16th, 2011 at 01:58 | #1

    Thanks for the tip

    Can you explain what’s the difference between left=”0″ right=”0″ bottom=”0″ top=”0″ and width=”100%” height=”100%”?

  2. August 16th, 2011 at 09:31 | #2

    @florian They both equate to the same thing, although if you wanted a 5px gap on just the left or right it wouldn’t be possible using the width property. Likewise setting something to half the height of the parent would be difficult using top and button (especially if it can be resized).

    If you are using width 100% and height 100% then I would stick with that, but some developers tend to always use top/right/bottom/left because it makes it easier to change later 🙂

  3. Evtim
    August 16th, 2011 at 11:03 | #3

    One extra benefit to using left/right/top/bottom is that GraphicElements with stroke will be positioned on pixel boundaries. If using stroke with x/y then you’d need to offset by half of the stroke weight.

    For example using a Rect like the following

    <s:Rect width=”100%” height=”100%” />
    <s:stroke>
    <s:SolidColorStroke color=”” … /> <!– defaults to 1 pixel stroke –>

    will place the stroke on half-pixel. Fortunately the player will then pixel-hint and nudge the horizontal/vertical lines and they will align with pixels so it will be unnoticeable. Unfortunately, if you’re using rounded corners, the curves won’t be pixel-hinted and the whole thing will look bad. The fix is to either use left/right/top/bottom or offset like this:

    <s:Rect width=”100%” height=”100%” x=”0.5″ y=”0.5″ />
    <s:stroke>
    <s:SolidColorStroke color=”” … /> <!– defaults to 1 pixel stroke –>

    That’s the reason I always use left/right/top/bottom for all MXML graphic elements 🙂

  4. August 16th, 2011 at 18:22 | #4

    You can default to 0 as a value with the following:
    left=”${0}” right=”${0}” top=”${0}” bottom=”${0}”

    I named the shortcut tlbr because I use:
    top=”${0}” left=”${0}” bottom=”${0}” right=”${0)”

    it’s fast to type tlbr then ctrl-space

  1. August 21st, 2011 at 21:03 | #1
  2. August 29th, 2011 at 10:02 | #2