CoreText

As promised in one of the previous articles on Core Graphics, I want to talk about the wonderful Core Text. Consider the topic as a followup of the previous articles on Core Graphics. If you are new and would rather start with Core Graphics first, you can do so here. And for all, enjoy!

When I first learned Core Text, I thought it was such a great and powerful technology that I was convinced it was already used in many different applications. Unfortunately, I was wrong. And more, I was astonished when I found out that the digital press industry that should be taking the most of it was hardly using it. So, after reviewing the current world press applications published in the AppStore, I started to believe that only a small bunch of people understood the potential of Core Text. Magazines as Wired, newspapers as El Mundo or the so discussed The Daily are not great at all. I mean, there is such a poor design work behind these and other newspapers and magazines that it’s sad to believe how so many designers, creatives and experts cannnot put together something “usable”. Yes, because this is the main problem. A PDF reader is too far away from what a well done iPad or iPhone application can provide.

When the iPad appeared on the market, the press industry started to criticize saying that it wouldn’t have any impact on them. They could not be more wrong. After a while, the press industry realized that the iPad could be the solution to their current crisis: people prefer to read the news in the Internet for free more than in a newspaper. So, the iPad represented the answer to the problem and they began to launch the digital editions of their magazines just to have some presence in this market, just in case. But, the easiest thing to do was simply to copy what others had already done: scan the paper magazine pages and, voilà, the digital version of the magazine was ready to go.

Publishers haven’t understood the real truth: the iPad is a completely new communication channel with lots of capabilities and, as every new technology, it deserves a little bit of research and a hint of creativity. However, under the digital rush to the iPad, we’ve seen terrible mistakes: 500 MB magazines that are so slow and heavy to download, very slow and completely unintuitive user interfaces and, in some of them the simple page zoom is not allowed, and the icing on the cake, newspapers created exclusively for the iPad… But what does it exactly mean? Well, it only means that they do not print on paper, while the rest is just another “old-fashion digitized newspaper” and you can consider yourself lucky if the app does not crash in the middle of your reading.

I don’t really understand why many publishers keep investing so little or so badly in technology to come up with the umpteenth digital publication of a newspaper. What I really wanted to say is that to create a great iPad or iPhone app, the developer has to use the best possible technology and for magazines and newspapers, they should consider seriously Core Text.

Core Text is a C-based framework available iOS from version 3.2 designed to ease the handling of advance text layout and font rendering requirements. If you are already familiar with OS X, you may know that it has been part of this OS for years. So, we are not talking about a completely new framework.

The Core Text Framework is a low-level technology which Apple defines as a text drawing engine. Core Text is not meant to replace the Cocoa text system. If you can deal with high-level constructs, you’d better use Cocoa. So, for example, if you need to display small amounts of text, just use UITextField. If you need to display a larger text, use UITextView. You don’t need much code and is easier to implement.

At this point, you could ask me: “Hey, wait a second! Why should I use Core Text for, if Cocoa offers me plenty of easier solutions to handle text?”. Well, you should be using Core Text, if you need to render text in a very special way (for example, around or inside a shape) or to import your fonts or if you want to draw mixed-style text. You should also consider using Core Text, when you want to display web content and the UIWebView class is a real limitation of what you want to achieve.

In those cases, you will need to dig into the Core Text framework and that’s exactly what I want to show you here. Core Text provides different levels of usage, meaning that it comes with high-level objects, which allow you to draw simple things in an easy way, but it has also low-level objects, which allow you to draw complex text composition and are harder to use.

Jumping on it

So, let’s start with the simplest one, which lets you draw individual text lines. To accomplish this, you have to create an attributed string and then just draw the line in the context. An attributed string consists of a string and some metadata describing formatting attributes for portions of the string. In the iOS, attributed strings are represented by the Objective-C class NSAttributedString and the Core Foundation type CFAttributedStringRef. These are toll-free bridged to one another, so you can use them interchangeably.

You will need to create a dictionary to store the attributes you want to apply to the string and then, combine the dictionary with a string generating a NSAttributedString.

Here the steps:

  1. Launch Xcode and create a new view-based iPad project. Name it FunnyText.
  2. Add the Core Text framework to your project.
  3. Import the Core Text header in this view controller.

Open the FunnyTextViewController.xib and change the Class Name of the View object to CustomView.

Go back to Xcode and generate an Objective-C class, subclass of UIView and name it Custom View. In this way, the view of the view controller FunnyTextViewController is your custom view and you can now use Core Graphics to render what you need (in this case a simple line). Import the Core Text header in this class:

In the CustomView class, create an outlet and its property for an NSAttributedString *attString. Don’t forget to synthesize it. Then, in the CustomView.m edit the drawRect: method as follows:

Here, we call the drawRect: on the super class to let it do all the setups. Then, we get a reference to the current graphic context to draw a line on which the text will be displayed.

Now, it is important to know that CoreText uses a reference coordinate system with the origin on the lower-left corner of the screen with the x growing from left to right and the y growing from bottom to top. So, if we just draw some text, it will appear upside down. To avoid that, we need to reverse the coordinate system. This is accomplished by the transformation generated by the lines 2-1, 3-1 and 4-1.

Line 5-1 creates a text line from the attributed string that we will create later. Line 6-1 fixes the text position and finally, we draw the text line and release it.

Now, go to the FunnyTextViewController.m and import the CustomView header:

Modify the -viewDidLoad method as follows:

Here, we first create a reference to a font. We used the Chalkduster with size 36.0.

Now, we need to create the attributed string. First, we create a dictionary of attributes (Line 10-1). In this particular case, we set the font, the text color, the stroke color and the stroke width. Notice that the stroke width should be negative for stroke and fill and positive only for stroke. Check the Core Text String Attribute Reference, if you want to apply additional customizations. Finally, we apply the attributes to the string using the initWithString:attributes: method (Line 12-1).

Line 13-1 passes the created attributed string to the custom view and line 14-1 releases the attributed string, since we created with a alloc/init pattern.

Wonderful, we are done. Now, just build and go and if you did not make any mistake you should see the following result:

1

Writing on shapes

In the previous chapter, you saw how to create a NSAttributedString object, which is a string with a set of attributes, and draw it in a graphic context. You also noticed that the used method was limited to draw only one text line each time and, depending on the length of the text line, the text could draw outside the graphic context. So, how do we deal with a long text? That’s exactly what we are going to explore here: we are going to draw multiple lines in our graphic context.

Core Text allows you to draw a multi-line paragraph within an arbitrarily sized rectangular area very easily. You have to define again an attributed text string and a rectangle to provide the display area in the current graphic context.

Start a new view-based iPad project and follow the steps described in the previous chapter. The difference is represented by few lines of code we are going to see now.

CUSTOM DRAWING

In the drawRect: method, you get a reference to the current graphic context using the UIGraphicsGetCurrentContext function. Then, you reverse the coordinate system to avoid the text to appear upside down (Lines 1-1 to 4-1 from Part 1 remain the same). After that, replace the rest of the code with the following lines of code:

Here, I just created a mutable path (a rectangle). We need this to define the text frame. Additionally, we need to create a framesetter that uses the attributed string and the path to generate the text frame.

Finally, we draw the text:

Remember to release all the objects allocated in memory (Lines 9-2, 10-2 and 12-2).

Now, a CTFramesetter is used to generate a text frame. It takes the attributed string object and a shape descriptor object and calls into the typesetter to create line objects that fill that shape. The output is a CTFrame object containing an array of lines. The frame can then draw itself directly into the current graphic context.

IN THE VIEW CONTROLLER

In this viewDidLoad method, we are going to create the attributed string as we did in Core Text Part I. The steps are exactly the same, but I am going to modify a few things:

  • To give a sense to this example, you need a long text. Just, copy some text from somewhere. Instead, I used a small Mac OS application named LittleIpsum, which is a Latin Generator Software and works really great for this.
  • I changed the attributes associated to the string. I want to use a simpler layout and I like to play with different fonts and sizes.

Let’s write the dictionary containing the attributes, since that’s essentially the only thing that is going to change. The font used is Didot and the size is 30.0f:

Then, apply the attributes to the string using the initWithString:attributes: method (Line 12-1 from Part 1), where you should replace the existing text by a longer text. Line 13-1 from Part I passes the created attributed string to the custom view and line 14-1 from Part I releases the attributed string, since we created with a +alloc/-init pattern.

That’s it. Just build and you should see the following result:

2

I’m guessing that at this point you are not impressed. I know you are thinking: “We wrote a lot of code to obtain something so relatively simple?”. However, do not underestimate this exercise, because with the basic knowledge you acquired and some creativity from your side, you are ready to create things like the ones I’m going to show you here.

3

Here, the rectangle was replaced by a circle. Very easy, right? Just append a circle to the mutable graphic path, instead of a rectangle and you are done!

In the next pictures, I defined multiple rectangles to create the well-known newspaper column layout, with as many columns as you like. Note the text is justified.

4

5

Details in the text

I’m sure you have noticed that in all our examples we have been using a single set of attributes for the whole text, so all the text contained in the shape defined by the CGPath had the same font and color. What happens when we need to edit rich text inside the CGPath? I’m going to show you how you can mix and match fonts and colors!

Open again the Xcode project previously created. Now, we want to create different groups of attributes (font, color, etc.) in the viewDidLoad method in your view controller. Modify that method as follows (see in bold the modifications to the existing method):

Line 1-3 and 2-3 are modifications to the existing code. In those two line we are defining that a new font type and size, and we are increasing the length of text.

The following lines should look very familiar to you, because we are starting over the process of creating a reference to a font (“Palatino-Bold”) (Line 3-3) and creating a dictionary of attributes (Line 4-3).

Line 5-3 is what brings some novelty to our code, defining the range of characters inside the string where the new attributes should be applied.
Let’s also change the dimensions of the rectangle that will contain our text. In the drawrect: method, change the dimensions as follows:

Now, if you build and run, you should get something similar to the following result.

6

Note that you can repeat the code from lines 3-3 to 6-3 to add as many attributes as you like, creating results as shown below:

7

8

I would also like to point out that the method I have shown you is not very practical: counting characters is not something that can be done extensively. But what I have shown you is only one simple method. Depending on the data and how they are structured, you could think about applying other methods too. Take a look at the following method:

This methods would allow to append attributed strings creating a single attributed string.

Rendering on a curved shape

Now, let’s explore how to draw our text on a curved shape.

Open the Xcode project created until now. As usual, we are going to modify the existing code, so that we don’t have to repeat the same steps. Let’s define the different attributes groups (font, color, etc.) in the viewDidLoad of the view controller.

Modify the viewDidLoad method as follows (see in bold the modifications to the existing method):

Line 1-4 and 2-4 are modifications to the existing code. In those two lines, we are defining that a new font type and size, and we are increasing the length of text. Before we can go on, we have to clarify some concept that we are going to need later:

A character is the smallest unit of written language that carries meaning, e.g. uppercase A.

A glyph is a concrete form of a character. In our previous example, the character uppercase A can be drawn with a different size or a different stroke thickness. It can lean or be vertical, and have certain optional variations in form. The following figure represents glyphs of the character A:

Example Glyphs

Note that characters and glyphs do not have a one-to-one correspondence. In some cases, a character may be represented by multiple glyphs, such as an é, which may be an e glyph combined with an acute accent glyph ´ (accent). In other cases, a single glyph may represent multiple characters, as in the case of a ligature, or joined letter. The following figure represents the ligature case:

Example of Glyphs

A glyph-run is a set of consecutive glyphs sharing the same attributes and direction.

Now, let’s go now to the drawrect: method, where we are going to draw our text along a semi-circle. Since there are many lines of code, I’m going to group them by task.

Line 3-4 calls the drawRect: on the super class to let it do all the setups. Then, line 4-4 gets a reference to the current graphic context. Lines 5-4 to 7-4 flip the coordinate system, so our text doesn’t appear upside down and finally, line 8-4 adds a rotation of 90 degrees counterclockwise.

Line 9-4 creates a text line from the attributed string. Line 10-4 extracts the number of glyphs and line 11-4 the number of runs. At this point, you should already know the numbers that will come up. Try guessing and see if you understood the concepts previously explained (characters, glyphs and runs): how many glyphs and how many glyph-runs exist in the text “iNVASIVECODE”?

Now, we need to create an array to store the width of each glyph. Line 12-4 is where the array is created and then after running through the run glyphs, line 13-4 stores the different widths of each glyph.

We have to divide the length of the line where we are going to draw the text into slices. Each slice has to cover the distance from one glyph’s center to the next. In our case, we are going to store the angles of the semi-circle into an array. Line 14-4 creates a new array and line 15-4 stores the angles.

Let’s do the actual drawing. We have previously calculated the angle offset for each glyph relative to the previous glyph, so let’s draw them rotating the context in line 16-4 after each glyph so they appear spread along a semi-circular path. Line 17-4 centers this glyph by moving left by half its width and offset text position leftwards by this glyph’s width in preparation for the next glyph.

Don’t forget to release the objects.

Now, if you build and run, you should get something similar to the following result. I set a dark background view in IB. I thought it would look more cool!

iNVASIVECODE

I pulled out a screenshot with the glyphs bounding boxes…did you answered correctly to my previous questions? How many glyphs do you find in the text? Here they are:

Glyphs

And another screenshot with the bounding boxes defined by the line metrics.

Metrics

Go ahead and play with all the parameters that we have set along the code. You can get results like the ones I’m showing you below.

Example 1

Example 2

iOS Consulting | INVASIVECODE

iOS Training | INVASIVECODE

(Visited 238 times, 1 visits today)