Shadowed Text using Gideros

Do you want to create text that has a shadow and control the colour of the Text and the shadow, all of it using Gideros? This article is a quick and easy way to create a text item with shadow.

In terms of learning, there are a couple of things that we learn here
1. How to create a complex hierarchy of items
2. How to add custom functions that seem like the native functions

The theory

If you were to use any programing language or even draw it yourself in Photoshop, to create a shadow, there are two layers containing the text, one with the textColor and one with the shadowColor. The shadow layer and the text layer are positioned one on top of the other with a slight offset for the direction of the shadow. If it is possible, the shadow layer is blurred to give the soft shadow feel. In our case since we cannot blur, the shadow will look like a hard shadow.

The issue

Actually there is no issue, the only problem that we might encounter is that we have to manage two text objects and keep them together. In Gideros we use the TextField object to create text objects and each of these has a setText(), setTextColor() that we can use. To keep them together, we can combine them into a single object or group as we would call it. From the program, we have access to only one object, so if we access the group, we lose all access to the items contained in this group, if we retain access to one of the text items, then how do we access the other? That is the issue here.

Solution

The solution is also rather easy, Gideros has an O-O type structure and due to that the way one can think of solutions also becomes easier than using a non O-O type framework.

1. Creating the group

This container can now be positioned, scaled, moved, etc like any other sprite object, and when it is transformed, it will apply all of the transformations to the children object inside.

2. Creating the Shadow object

3. Creating the Text Object

Now if we position the container using the container:setPosition(100,100) for example, we shall see the text move to the position with the shadow underneath.

4. Adding the helper functions
We need to add the helper functions that will help us modify this object and work with it. The functions that we need are

  • setText
  • setTextColor
  • setShadowColor
  • setFont

These functions are going to be member functions of the container object, hence they are prefixed with the container:

Putting it all together

The entire code can be wrapped in a function say newShadowText()

Now we can use this function to create text with shadows and change the font, colour or text as we want. It is as simple as using

NOTE: If you want to use fonts in Gideros, the .TTF file has to be included in the project and the fontName has to reflect the exact name of the .ttf file. If you have the font file included into your project, let’s say it is called customFont.ttf then you can use it as

Hope this is useful in your projects.

Social tagging: > > >

Comments are closed.