In this recipe we will take a look at how easy it is to draw stylized text with an outline on to the view.
Following on from our previous recipe, create a new Single View Application, and name it DrawTextExample.
To begin, follow these simple steps as outlined in the following order:
DrawTextExample
folder, choose File | New | File…DrawTextView
as the name of the class name to be created.UIView
as the type of subclass to create from the Subclass of drop-down list.DrawTextView
class. Our next step is to implement the functionality and methods used by this class.DrawTextView.m
implementation file from the Project Navigator window.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.
ViewController.m
implementation file from the Project Navigator window.// 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"
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]; }
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.
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.
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