Could someone please explain to me how to draw a
string using UIStringDrawing instead of using a label? here is my code but for some reason it compiles and the screen is blank…
//
// MainViewController.m
// DotH
//
#define WINDOW_FRAME [[UIScreen mainScreen] bounds]
#define SCREEN_FRAME [UIScreen mainScreen].applicationFrame
#define GRAY [UIColor grayColor]
#define BLACK [UIColor blackColor]
@implementation MainViewController
- (void)loadView {
UIView *view = [[UIView alloc] initWithFrame:SCREEN_FRAME];
[view setBackgroundColor:GRAY];
[BLACK setFill];
NSString *string = @"Hey Dude!";
[string drawAtPoint:CGPointMake(50, 50) withFont:[UIFont systemFontOfSize:14]];
self.view = view;
[view release];
}
@end
The line
needs to be in the view’s
drawRect:method.This is because just before
drawRect:is called, the rectangle passed as an argument todrawRect:is erased. So if you try to do custom drawing anywhere other than a view’sdrawRect:method, the stuff you draw will get erased wheneverdrawRect:is called. (Not to mention that callingdrawAtPoint:is meaningless if not done by code within a UIView.)You will need to make a custom subclass of
UIView, and that subclass will need a customdrawRect:method. If you still want the view controller to be the entity responsible for deciding what string should be drawn and how, you should give your UIView subclass a method likeThat method can store that information, in, e.g., three
NSMutableArrays (one of strings, one of points, and one of fonts), and increment a counter of how many strings have been added. Then, your view’sdrawRect:method can draw the strings described in those arrays. To add a string, your view controller just callsaddString:atPoint:withFont:on your view.