iPhone中自绘实现步骤
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 }