It's time to add Digits to our application
It's really great to have lines but how will users know how many inches they have if there are no numbers? Exactly ! In this video we take a look at how text works in ActionScript and get it to align correctly in the Air runtime.
Video Transcript
00:00Our last step left in building our application is to add actual the values of those inches to
00:05build our basic inch ruler. So the next step that we're going to do is we're going to go back into
00:10that refresh ruler and right underneath it what we want to do is you want to start adding the copy
00:15in. To do that I'm going to start off with creating a new text format because I need to
00:19decide how will the copy look like. To do that I'm going to create a new variable and I'm going
00:23to call it just tf for text format and create a new text format variable. Now just as a recap
00:30and remind you that because we're creating a new variable it will automatically import the
00:35text format in the flash.txt.txt format if you're working with a latest an up-to-date version of
00:41flasher that automatically does these imports for you. If not you will have to make sure that each
00:45time you're importing the correct library or make sure that you download our source files
00:50to find those correct libraries. So I'm going to create a new text format and once I create the
00:56new text format I could actually add all the information directly inside of the properties
01:01of the text format. Now the first one expects a font so we're going to give it one of the
01:06default fonts without embedding anything because we don't want to embed anything. To do that I'm
01:11just going to add basically a string called sans for sans serif font. You could pick whatever font
01:18you want but ideally you want to work with one of those underscore fonts. All right the next one
01:24because we don't want it we don't want to embed and make our application bigger than it needs to
01:28be. The next step is we want to figure out our size although it's telling us it's an object
01:32it's actually a number. So we're going to first start off with 10 points because that seems like
01:36a big enough number to be legible and be able to read. The next step is we're going to figure
01:41out what color we want it to be. In our state we want to set it to be basically totally black
01:47so we could set it even by the way this is a shortcut for it because zero is the same thing
01:50as zero zero zero zero it's basically the same thing so we're just going to set it as basically
01:55zero. The next thing that we want to set is do we want it to set it bold do we want to set it to be
02:00italic is there any other property that we want to send and really the rest we could set with
02:04the default value that's there and we don't care about the rest. Although we do care about the
02:08line what we could do is or just start putting here null null null because we don't really care
02:12about the other properties we want to use the default or we could close it up and just go to
02:17our text format and just make sure that we approach our text the line itself and set it to be center
02:22because we want it to be in the center. All right so once we configure that and we have our text
02:26format it's time for us to actually add our text in. To do that we're going to use one of those
02:31loops I'm just going to grab one of those loops and I'm just going to instead of typing a loop
02:35each time so I'm just going to and again we have here this i variable because we don't have
02:39any definition of the i variable in this function that's fine and our total steps we really want
02:44to get what is the actual total steps and actually to do that we might need to grab this variable.
02:52Another way around it by the way is one thing that we could do is we could actually set it
02:56to be when we're actually drawing our lines we could actually set our value to return the total
03:00steps so we could return an integer and all the rest of the times it's not going to do
03:04anything with it but in the last last time we could have set it to basically oh no that's
03:09we can't because our step sizes changes twice right and we actually are going to need to use
03:14it twice we're actually going to need the same structure twice so I'm going to just grab that
03:19because we're actually going to need it we might if you want as an exercise as a homework try to
03:24figure out a way to reuse that draw lines method those are draw other stuff beyond copy but we're
03:28not going to do that we're just going to duplicate that functions again and all we're
03:32going to change in it is that the step size is going to be inches the way it was originally.
03:37Everything here the rest of everything inside of here and again inches the rest of everything
03:42inside here should basically work the same way as as it did before only thing is that we don't want
03:47to draw anything instead of drawing we want to actually add copy so I'm just going to delete
03:51everything inside of the actual for loop itself all right so now that we have our for loops we
03:57know the exact amount of times that we actually want to type something out the next step is
04:01we want to actually add the copy inside to do that I'm going to need to create a new variable
04:06and a new text field each time I do that so I'm going to create a variable for that outside here
04:11that we're going to use inside of our loops which is going to be called a text field
04:16whoopsie text we're going to call it text and it's going to be a text field inside of our
04:23first for loop let's start by creating a new text field once we created our new text field
04:32there are a couple of stuff that we want to configure to it the first thing is we want to
04:36set a default text format which is the new text format we've just created once we created the text
04:41and we added the text format a couple of properties we want to make sure are set to be correctly such
04:47as that we don't want it to be selectable so we want to set that to be false we don't want the
04:51user's mouse to be trapped on top of our text field nor do we want our mouse or anything to
04:57really uh make it clickable so we're going to make it mouse enabled as false so it's not really
05:01going to be clickable either the next step is we want to set the actual width and height of the
05:06text field and because our copy is going to be 10 pixels in length so i'm going to just make sure
05:12that my text uh um width is going to be just about maybe let's do it 11 uh let's make it 12
05:18points or 13 just to make sure that we're going to get everything inside there i'm going to set
05:23that to be basically a square that's about 12 by 12 all right so we could always modify it if
05:28that didn't work out well now next step that i want to do is i want to actually add it somewhere
05:32and i'm going to add it directly into my ruler now probably what we want to do before anything else
05:38also is define actually a copy for it so let's set our copy in our text we could set the text
05:43itself and what will the text itself be probably the value of our i value so we're just going to
05:49take that um the value inside of our i and we're going to add that into our text last
05:55almost last but not least so we want to figure out the position right so we're going to figure
05:59out the position as well so we're going to set our text x position and we could steal that x
06:03position by looking again at our steps here so i know our step size and zero in our case it's not
06:09going to be zero you know you have your line length as well so very that's a good step to
06:13start from so if we set our x to be um the size itself and maybe we set our y to be initially
06:21we'll set it to be the line length itself and we can always tweak it once we see how it looks like
06:26and we let's make sure also that we actually have the line length property and we don't actually
06:31have the property as line length but we do still have the property that we send in to our initial
06:37function which is full step size so we could use that full step size variable directly instead
06:43of using that internal variable all right so we set everything that we needed more or less at least
06:48for our first for loop all that's left is to figure out where do we want to add it onto the screen
06:53so i'm going to add it into our ruler variable and i'm going to add it a child and add our text
06:58field into it and notice and don't forget that we're going to be adding it as many times as
07:02we're actually going to have inches or as many steps as we're going to have so first of all we
07:06want to make sure that this is working before we do anything else so i'm just going to save
07:10that i'm going to run my application here one time and first of all i'm just going to go right
07:16ahead to fix whatever errors i have here so i'm just going to double click right into your
07:20first of all i'm putting inside of my text field actual number so there's a few ways to solve that
07:25one is to basically wrap it around with the string command basically tell method basically
07:31telling it well i want to treat it as a string forcing it to be a string that's one option so
07:35that's going to be good for us let's run it again and see what other error we have access
07:39done to find property step size and that's because there isn't a variable called step size
07:44instead of that um let's find what is going to be our step size which is going to be full step size
07:50that is the actual size itself well actually it's going to be one inch right so we're going to change
07:55that to inch i'm going to save that run it one more time right as part of debugging once you
08:02built it out want to update it based on the right information that should go inside there all right
08:06so give it a second and here we go so first of all we could see our one two three four five
08:11and we can see that we need to make it a little bit bigger for it to be actually visible and you
08:14probably want to move our position to be into the actual center of our point so let's fix both of
08:21these issues first of all so one is we probably made our width and height a little bit too small
08:26and as there is nothing there that actually is selectable it doesn't really matter if it's too
08:30big because we we made sure that our copy is centered with our text align and now that we
08:35know that we're actually in the center itself and we know that it's going to be 20 pixels
08:39width we could actually make sure that our x position is not starting from the exact point
08:45where it needs to be but from the point it needs to be minus its actual width so to do that i could
08:50go to minus text width divide by two making sure that my x position is going to be exactly in the
08:56center of that point so let's take that let's take it for a ride again and see how it looks
09:00like now that we've done that change and we can see now our position is beautiful it's exactly
09:08where we want it to be i'm happy with the size as well all that's left for us to do and this
09:12actually really works well also because we might be in a scenario where we have a bigger text field
09:17so actually in a scenario what if there's a device has 100 pixels so we could without
09:22any problem also define our width to be 50 it doesn't really matter because it's going to have
09:26enough space there and as it's centering it and it's not selectable it doesn't really matter how
09:31big it is all right so even if we set it to be this big it'll be it'll still be good
09:35now the next thing that we want to do is we want to implement this osu for our for loop
09:39but again because almost everything here is redone over and over it probably makes sense for us to
09:44create a separate function just for this process of creating a new text field so to do that i'm
09:49just going to create quickly a new function that's going to be called create text the create text
09:55function at first we're not going to have anything in it but it's going to return a text field
10:01now let's see what our first of all let me just copy everything inside of here
10:04and move it right inside of our uh move it basically everything inside of here i'm just
10:10going to move into our create text field and at first we're going to we're going to modify
10:17it as we need so first of all we probably want to create here that actual text field variable
10:22we could actually create right here as well just making sure that we're moving everything out so
10:27we moved everything related to the text field in here we could also return the actual text field
10:33if need be right so we set as a text field we're returning the text field okay so first of all we
10:38don't have our text format so we want to make sure that our format is going to be sent over
10:42text field text format should be sent over so we're going to go first of all create that text
10:47field put it inside of our loop and we're just going to make sure that we're sending our text
10:52field all right so technically this should still work so if we look at this one more time
10:58everything should remain the same and should work without any issue we just need to test
11:02it out and see if it really does so i'm just going to click on that command return and we'll
11:06just fix whatever typos we got down there so first of all there is no i so we probably need to send
11:12that i as well so we're going to set we're going to send it to um the i value itself as an int as
11:17well and go right back here into my create text and send the i position as well i'm going to save
11:24that run it one more time and we're just going to do that until we fixed all of our
11:28bugs and until it runs just the same way as it did before and now it does so the next step is
11:33we want to actually do the exact same thing only this time around we want to do it also for our
11:38for loop now we we couldn't do it in a couple of ways so right now we moved everything out but
11:44really the only thing that changes is actual x and y positions instead of that what we could do
11:49is we could extract the x and y positions and once we do that we could actually paste it right
11:56underneath where we created our text field and all we have to do is make sure that we set the value of
12:01the create text to be text because we're returning our text field in line 76 so that way we're
12:07basically configuring it but only the only thing that changes is outside of that creation process
12:13and by doing that we could basically copy everything here and paste it right underneath here
12:18and all that's left for us to do is to figure out the right position for our x and y so first
12:23of all for our x position our x position is not going to change anymore we just need to figure
12:28out what that is and in our case it's probably going to be the full size and a little bit more
12:33because contrary to the last time our full size might overlap with the actual position but for
12:39now let's leave it the way it is now let's look at the other at the other pointer i'm just going
12:44to delete this extra information for a second is this right for the positioning probably it's true
12:49we might need to do a couple of adjustments but before we do let's just run it by by flipping
12:53everything and getting rid of the things that don't matter so i'm going to save that and run
12:57the application it's the best way just look at the screen and based on that make our decision
13:03all right so we have a couple of errors so let's fix them so i'm just going to double click on
13:07the errors and the first thing that we can see is because we took out our variable of text
13:12then it stopped working so i'm just going to go up here now that we're actually using the
13:15text again i'm going to just make sure that i have a variable so we have we don't get errors
13:20because we're using the variable text again so i'm just going to save that again and run it one
13:24more time so we don't get that error hopefully and we're not getting any error and here we go
13:32so first of all we're already in a much better state now this is probably way too far away so
13:37we probably want to fix that around the reason is because we have a big text area and that big
13:42big text error basically is just too far away the one the next thing that we want to do is
13:47we want to figure out basically um basically this is going to be a little bit more complicated so
13:52first of all we want to make sure that it goes up a bit let's start by there so we want to change
13:56actual value of our y property so just going to open up our y again and go right to our y step
14:02and basically subtract oh to the actual height itself of our text field half of it at least
14:07so i'm going to subtract the actual height divided by two that should on its own fix the
14:12height issue and once it's low as we should be able to see it's in the right height position
14:18or more or less the right height position we see that it's probably a little bit too much up now
14:22the reason why it's a little bit too much up is basically because right now we're we're
14:26configuring the actual width and actual height of our element in a non not very smart way we
14:34could do it much smarter we could basically define the size of our width to be exactly what
14:38it has to be so let's do that because that's going to make our life way easier so in our
14:43create text field i'm going to go there in line 73 and i'm going to force the width to be the text
14:48width by doing that i'm going to resize my text box to be exactly the size it needs to be to
14:54be able to render everything in our copy i'm going to do the exact same thing for the height as
14:58well i'm going to force it to be the text height once i do that and i run my application
15:03it's going to be much easier for me to work with my numbers because the boxes and excuse me
15:07for that my battery is running low once i do that first of all we'll see that we're having a small
15:12little issue because our copy wasn't yet added on to the stage and because we're changing these
15:18these these um this information before it was added into the stage basically we're making our text
15:23field to be zero zero or something very close to that so what i want to do is i'm going to go
15:28back into that creation of the text field and move my width and height to after i've actually
15:33added it on to the stage which is going to be in line 77 and i'm also going to add here a little
15:38trace just to make sure that we're not adding here something that's just not going to work
15:50all right so we're going to trace out the height and the width the width and the height
15:53and we just want to see what values come back if it's going to be rational values or completely
15:58unrational values and again we're going to look at the when we look at this the width and height
16:02seem to be seem to be right but if you look really close you will see that we have the start
16:07of some copy but nothing more beyond that what we could do next is just add a little bit of
16:13padding to make sure that our copy is visible because it is bringing back valid information of
16:18the actual size of our copy but it's not giving us the information that we need um of the actual
16:23surrounding box area that is needed so to do that what we might be able to do is basically
16:28re-add a little bit more instead of adding it automatically we could just like manually just
16:33add a few more points to try to figure out what is that perfect size so i'm just going
16:37to add another eight pixels in every single direction and obviously this is not incredibly
16:42accurate but it doesn't have to be too accurate as long as it works you'll see that now we're much
16:47better our copy is also centered exactly as the way we wanted it to be we probably could have
16:52added a little bit less than the height because we're a little bit off here so i could even in
16:56theory just for the for testing this out we might just add maybe seven instead of eight
17:02and this is really you know this is where the science goes away and it's really about
17:06um testing until it works well and you just test a couple devices and once you do and you know that
17:11it works and you're more or less set and one thing that you could see is one of the issues here is
17:14that our box is probably not big enough so it's actually pushed up a bit so i'm gonna just resize
17:19it again but this time around i'm gonna set it to be eight i'm gonna set it to be nine instead
17:23of eight and we're gonna leave it there now before we wrap up this video and we created the
17:28right text field i want to go back and we could delete this trace right here i want to go back
17:32into our y position because we did set our y but we didn't do anything with our x and let me show
17:37you and let's talk about why would we want to add something into the x because we really want to
17:41debug this and make sure that it works because it's going to be a real application that goes on
17:44to this android store hopefully so one of the stuff that we that could become problematic
17:50is if we have you like 100 pixels it would then probably it might overlap our content
17:54to be able to figure out and see if that would be if that would happen really what we probably
17:58should do is just force the copy to be longer so to do that i'm just going to go into my copier and
18:04i think that three digits is probably the maximum that it could actually get to ever i'm going to
18:09find the text area and i'm just going to pin to it another 10 just to make sure that it turns
18:14into a three digit i'm going to save that and run and literally this is just for testing to make
18:18sure that we get our alignments as best as we could and by the way the lines will never be
18:23perfect because one of the stuff with alignment says that the font itself will render differently
18:27on different devices so it's not about making it perfect but making it visible in a way that makes
18:31sense and you can see that even when we get to a really big number where we don't have any issues
18:36of overlapping our content with our lines so really we're extremely happy and we've completed
18:41building the actual application we could delete this testing point it was another point that you
18:46really want to always kind of think about where are those scenarios where things could go wrong
18:51and try to test them out as thoroughly as you can that way it's going to work best on the most
18:55amount of devices without you needing to worry about them as much all right so that's it about
19:00actually creating the application the next few videos we're going to start wrapping up our new
19:04application we've created to get it ready to go on to a store
