Objective-C (iOS): 図形を描画

iOSシミュレータのスクリーンショット 2013.01.03 12.14.04

Objective-C: MyDrawViewController.h

#import <UIKit/UIKit.h>
#import "MyDrawView.h"

@interface MyDrawViewController : UIViewController

@end

Objective-C: MyDrawViewController.m

#import "MyDrawViewController.h"

@interface MyDrawViewController ()

@end

@implementation MyDrawViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    MyDrawView *drawView = [[MyDrawView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:drawView];
}

@end

Objective-C: MyDrawView.h

#import <UIKit/UIKit.h>

@interface MyDrawView : UIView

@end

Objective-C: MyDrawView.m

#import "MyDrawView.h"

@implementation MyDrawView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 色を定義
    CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
    CGFloat blue[4] = {0.0f, 0.0f, 1.0f, 1.0f};
    CGFloat cyan[4] = {0.5f, 0.8f, 1.0f, 1.0f};
    CGFloat green[4] = {0.0f, 1.0f, 0.0f, 1.0f};
    
    // グリッドを描画
    CGContextSetStrokeColor(context, cyan);
    CGContextBeginPath(context);
    CGContextSetLineWidth(context, .5f);
    for (int y = 50; y < self.bounds.size.height; y+=50) {
        CGContextMoveToPoint(context, 0, y);
        CGContextAddLineToPoint(context, self.bounds.size.width, y);
    }
    for (int x = 50; x < self.bounds.size.width; x+=50) {
        CGContextMoveToPoint(context, x, 0);
        CGContextAddLineToPoint(context, x, self.bounds.size.height);
    }
    CGContextStrokePath(context);
    
    // 矩形を描画
    CGContextSetFillColor(context, green);
    CGContextFillRect(context, CGRectMake(50, 50, 50, 50));
    
    // 円を描画
    CGContextSetFillColor(context, blue);
    CGContextFillEllipseInRect(context, CGRectMake(100, 100, 50, 100));
    
    // 多角形を描画
    CGContextSetFillColor(context, red);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 200, 150);
    CGContextAddLineToPoint(context, 250, 200);
    CGContextAddLineToPoint(context, 250, 100);
    CGContextFillPath(context);
    
    // テキストを描画
    UIFont *font = [UIFont fontWithName:@"AvenirNext-Italic" size:20.0f];
    [[UIColor colorWithRed:.8f green:0 blue:0 alpha:1.0f] set];
    [@"hogehoge" drawAtPoint:CGPointMake(50, 300) withFont:font];
}

@end