iPhone UIStringDrawing usage
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]];
开发者_StackOverflow社区
self.view = view;
[view release];
}
@end
The line
[string drawAtPoint:CGPointMake(50, 50) withFont:[UIFont systemFontOfSize:14]];
needs to be in the view's drawRect:
method.
This is because just before drawRect:
is called, the rectangle passed as an argument to drawRect:
is erased. So if you try to do custom drawing anywhere other than a view's drawRect:
method, the stuff you draw will get erased whenever drawRect:
is called. (Not to mention that calling drawAtPoint:
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 custom drawRect:
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 like
- (void)addString:(NSString *)string atPoint:(CGPoint)point withFont:(UIFont *)font;
That method can store that information, in, e.g., three NSMutableArray
s (one of strings, one of points, and one of fonts), and increment a counter of how many strings have been added. Then, your view's drawRect:
method can draw the strings described in those arrays. To add a string, your view controller just calls addString:atPoint:withFont:
on your view.
精彩评论