我如何使用CGContext在UIView中设置背景颜色? [英] How i set the background color in UIView using CGContext?

查看:605
本文介绍了我如何使用CGContext在UIView中设置背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个应用程序,我想在UIViewController上设置已经设置的UIView的背景颜色。代码如下:

I have developed the application in which i want to set the background color of UIView which is already set on UIViewController.The code is below,

@implementation frmGraphView


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) 
    {
        // Initialization code
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{   
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextClearRect(ctx, rect);
    //CGContextSetCMYKFillColor(ctx, 35.0, 56.0, 34.0, 30.0, 1.0);
    //CGContextSetRGBFillColor(ctx, 92.0f, 95.0f, 97.0f, 1.0f);
    //CGContextFillRect(ctx, CGRectMake(0, 0, 300, 280));



    CGContextSetRGBStrokeColor(ctx, 2.0, 2.0, 2.0, 1.0);
    CGContextSetLineWidth(ctx, 2.0);
    float fltX1,fltX2,fltY1,fltY2=0;
    NSArray *hoursInDays = [[NSArray alloc] initWithObjects:@"0",@"1" ,@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12", nil];
    fltX1 = 30;
    fltY1 = 5;
    fltX2 = fltX1;
    fltY2 = 270;

    //Dividing the Y-axis   
    CGContextMoveToPoint(ctx, fltX1, fltY1);
    CGContextAddLineToPoint(ctx, fltX2, fltY2);
    //float y = 275;
    for(int intIndex = 0  ; intIndex < [hoursInDays count] ; fltY2-=20, intIndex++)
    {   
        CGContextSetRGBStrokeColor(ctx, 2.0, 2.0, 2.0, 1.0);   
        CGContextMoveToPoint(ctx, fltX1-3 , fltY2);
        CGContextAddLineToPoint(ctx, fltX1+3, fltY2);
        CGContextSelectFont(ctx, "Helvetica", 14.0, kCGEncodingMacRoman);
        CGContextSetTextDrawingMode(ctx, kCGTextFill);
        CGContextSetRGBFillColor(ctx, 0, 255, 255, 1);
        CGAffineTransform xform = CGAffineTransformMake(
                                                        1.0,  0.0,
                                                        0.0, -1.0,
                                                        0.0,  0.0);
        CGContextSetTextMatrix(ctx, xform);
        const char *arrayDataForYAxis = [[hoursInDays objectAtIndex:intIndex] UTF8String];
        CGContextShowTextAtPoint(ctx, fltX1-18, fltY2-18 , arrayDataForYAxis, strlen(arrayDataForYAxis));
        CGContextStrokePath(ctx);
    }
    CGContextSetRGBStrokeColor(ctx, 2.0, 2.0, 2.0, 1.0);
    CGContextSetLineWidth(ctx, 2.0);

    fltX1 = 5;
    fltY1 = 250;
    fltX2 = 270;
    fltY2 = fltY1;

    NSArray *weekDays =[[NSArray alloc] initWithObjects:@"Sun", @"Mon", @"Tus", @"Wed", @"Thu", @"Fri", @"Sat", nil];
    //Dividing the X-axis   
    CGContextMoveToPoint(ctx, fltX1, fltY1);
    CGContextAddLineToPoint(ctx, fltX2, fltY2);
    //float y = 275;
    for(int intIndex = 0  ; intIndex < [weekDays count] ; fltX1+=33, intIndex++)
    {   
        CGContextSetRGBStrokeColor(ctx, 2.0, 2.0, 2.0, 1.0);   
        CGContextMoveToPoint(ctx, fltX1+52 , fltY2-3);
        CGContextAddLineToPoint(ctx, fltX1+52, fltY2+3);
        CGContextSelectFont(ctx, "Arial", 15.0, kCGEncodingMacRoman);
        CGContextSetTextDrawingMode(ctx, kCGTextFill);
        CGContextSetRGBFillColor(ctx, 0, 255, 255, 1);
        CGAffineTransform xform = CGAffineTransformMake(
                                                        1.0,  0.0,
                                                        0.0, -1.0,
                                                        0.0,  0.0);
        CGContextSetTextMatrix(ctx, xform);
        const char *arrayDataForXAxis = [[weekDays objectAtIndex:intIndex] UTF8String];
        CGContextShowTextAtPoint(ctx, fltX1+37, fltY2+18 , arrayDataForXAxis, strlen(arrayDataForXAxis));
        CGContextStrokePath(ctx);

        }
}

@end


推荐答案

这段代码怎么样?

- (void)drawRect:(CGRect)rect {

    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGRect drawRect = CGRectMake(rect.origin.x, rect.origin.y,rect.size.width, rect.size.height);

    CGContextSetRGBFillColor(context, 100.0f/255.0f, 100.0f/255.0f, 100.0f/255.0f, 1.0f);
    CGContextFillRect(context, drawRect);
}

这篇关于我如何使用CGContext在UIView中设置背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆