iphone 自定义控件

iPhone中自绘实现步骤


1. 继承@interface MyView : UIView {
2. 实现- (void)drawRect:(CGRect)rect
3. 调用addSubView把新生成的view加入进来显示

addSubView[window addSubview:viewController.view];


4.示例代码
- (void)drawRect:(CGRect)rect {
    // create the bitmap context
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(nil,100,100,8,400, colorSpace,kCGImageAlphaPremultipliedLast);
    CFRelease(colorSpace);
    
    //    create an arrow image
    // set the fill color
    CGColorRef fillColor = [[UIColor blackColor] CGColor];
    CGContextSetFillColor(context, CGColorGetComponents(fillColor));
    
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 8.0f, 13.0f);
    CGContextAddLineToPoint(context, 24.0f, 4.0f);
    CGContextAddLineToPoint(context, 24.0f, 22.0f);
    CGContextClosePath(context);
    CGContextFillPath(context);
    CGContextSelectFont ( context, "Arial", 10.f, kCGEncodingMacRoman );
    CGContextSetRGBFillColor ( context, 0.0f, 0.0f, 0.f, 1.f );
    CGContextSetShouldAntialias ( context, 0 );    
    CGContextShowText(context, "hh", 2);
    
    
    // convert the context into a CGImageRef
    CGImageRef image = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    
    UIImage* image2 = [UIImage imageWithCGImage:image];
    [image2 drawInRect:CGRectMake(0, 0, 120, 160)];
    
    NSString* myStr = @"中文";
    UIFont* font = [UIFont systemFontOfSize:12.0];
    [myStr drawInRect: CGRectMake(160, 240, 100, 130) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
    
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值