Drawing customized text

In this recipe we will take a look at how easy it is to draw stylized text with an outline on to the view.

Getting ready

Following on from our previous recipe, create a new Single View Application, and name it DrawTextExample.

How to do it...

To begin, follow these simple steps as outlined in the following order:

  1. Select the DrawTextExample folder, choose File | New | File…
  2. Select the Objective-C class template from the list of available templates.
  3. Click on the Next button to proceed to the next step in the wizard.
  4. Enter in DrawTextView as the name of the class name to be created.
  5. Ensure that you have selected UIView as the type of subclass to create from the Subclass of drop-down list.
  6. Click on the Next button to proceed to the next step of the wizard.
  7. Then, click on the Create button to save the file to the folder location specified.
  8. We have successfully finished creating our DrawTextView class. Our next step is to implement the functionality and methods used by this class.
  9. Open the DrawTextView.m implementation file from the Project Navigator window.
  10. Modify the drawRect: method as shown in the following code snippet.
    - (void)drawRect:(CGRect)rect
    {
       // Set up and initialize our Graphics Context
       CGContextRef context = UIGraphicsGetCurrentContext();
       CGContextClearRect(context, rect);
       
       // Specify the text color and font fill style
       CGContextSetFillColorWithColor(context, [UIColor
       yellowColor].CGColor);
       CGContextSetTextDrawingMode(context, kCGTextFillStroke);
       
       // Finally, draw the text using the specified font.
       NSString *theString = @"GENIESOFT STUDIOS...";
       [theString drawAtPoint:CGPointMake(20.0f, 100.0f) 
       withFont:[UIFont fontWithName:@"Verdana-Bold" 
       size:25]];
    }

Our next step is to create the code functionality that will be responsible for adding our custom view as a sub-view to our existing view controller.

  1. Open the ViewController.m implementation file from the Project Navigator window.
  2. Include the following class reference as highlighted in the code snippet:
    //  ViewController.m
    //  TextDrawExample
    //  Created by Steven F Daniel on 20/11/12.
    //  Copyright (c) 2012 GenieSoft Studios. All rights reserved.
    
    #import  "ViewController.h"
    #import "DrawTextView.h"
    
  3. Next, modify the viewDidLoad: method as shown in the following code snippet:
    - (void)viewDidLoad
    {
      [super viewDidLoad];
    
      // Declare and add our custom view as a subView
      // to the current view.
      DrawTextView *drawTextView = [[DrawTextView alloc] 
      initWithFrame:self.view.bounds];
      [self.view addSubview:drawTextView];
    }
  4. Then, Build and Run the application by choosing Product | Run from the Product menu, or alternatively pressing Command + R.

When the compilation completes, the iOS Simulator appears and displays the text we created within this recipe. The image has been rotated within the device.

How to do it...

How it works...

In this recipe, we created a new custom DrawTextView class that inherits from the UIView class. We then modify the drawRect: method that will be used to draw our text to the view. We then declare the variable context that will be used to obtain the current graphics context using the UIGraphicsGetCurrentContext function.

Next, we clear the view using the CGContextClearRect function and set the color of our text and pass the FillStroke value to our CGContextSetTextDrawingMode method and then call our drawAtPoint: method to draw the text using the font details specified by the UIFont class. Finally, we initialize our DrawTextView custom class and then add this as a subview using the same dimensions of our parent view controller.

Note

For more information on the CoreGraphics class, you can refer to the Apple Developer Documentation location at the following URL: https://developer.apple.com/library/ios/#documentation/coregraphics/reference/coregraphics_framework/_index.html

See also

  • The Drawing lines and curves recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset