1. 繼承@interface MyView : UIView {

2. 實現- (void)drawRect:(CGRect)rect

3. 調用addSubView把新生成的view加入進來顯示:addSubView[window addSubview:viewController.view];

4.示例代碼

 1 - (void)drawRect:(CGRect)rect {
 2     // create the bitmap context
 3     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
 4     CGContextRef context = CGBitmapContextCreate(nil,100,100,8,400, colorSpace,kCGImageAlphaPremultipliedLast);
 5     CFRelease(colorSpace);
 6     
 7     //    create an arrow image
 8     // set the fill color
 9     CGColorRef fillColor = [[UIColor blackColor] CGColor];
10     CGContextSetFillColor(context, CGColorGetComponents(fillColor));
11     
12     CGContextBeginPath(context);
13     CGContextMoveToPoint(context, 8.0f, 13.0f);
14     CGContextAddLineToPoint(context, 24.0f, 4.0f);
15     CGContextAddLineToPoint(context, 24.0f, 22.0f);
16     CGContextClosePath(context);
17     CGContextFillPath(context);
18     CGContextSelectFont ( context, "Arial", 10.f, kCGEncodingMacRoman );
19     CGContextSetRGBFillColor ( context, 0.0f, 0.0f, 0.f, 1.f );
20     CGContextSetShouldAntialias ( context, 0 );    
21     CGContextShowText(context, "hh", 2);
22     
23     // convert the context into a CGImageRef
24     CGImageRef image = CGBitmapContextCreateImage(context);
25     CGContextRelease(context);
26     
27     UIImage* image2 = [UIImage imageWithCGImage:image];
28     [image2 drawInRect:CGRectMake(0, 0, 120, 160)];
29     
30     NSString* myStr = @"中文";
31     UIFont* font = [UIFont systemFontOfSize:12.0];
32     [myStr drawInRect: CGRectMake(160, 240, 100, 130) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
33     
34 }

From:CNBLOGS        
 




創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()