Powered By Blogger

Thursday, April 7, 2011

Custom Alert View (UIAlertView)

  ************************** Starting Of Header File Part ***************************


 #import <UIKit/UIKit.h>
#define OVALSCALEX 1.0
#define OVALSCALEY 70.0/425.0

@interface CustomAleartView : UIAlertView <UIAlertViewDelegate> {

}
- (void) drawRoundedRect:(CGRect) rect
          withBackground:(UIColor *) backgroundColor
               andBorder:(UIColor *) borderColor
               andRadius:(CGFloat) radius
              andContext:(CGContextRef)cntxt;

@end

 ************************** Starting Of Implementation Part ***************************

#import "CustomAleartView.h"


@implementation CustomAleartView


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}
- (void)willPresentAlertView:(UIAlertView *)alertView{
   
    self.frame = CGRectMake(10, 20, 420, 300);
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext ();
    [self drawRoundedRect:rect withBackground:[UIColor blackColor] andBorder:[UIColor whiteColor] andRadius:10.0  andContext:context];
}

- (void) drawRoundedRect:(CGRect) rect
          withBackground:(UIColor *) backgroundColor
               andBorder:(UIColor *) borderColor
               andRadius:(CGFloat) radius
              andContext:(CGContextRef)cntxt
{
    CGContextRef context = cntxt;
   
    CGContextSaveGState (context);
   
    //Calculate rounded form
    CGMutablePathRef     roundedPath = CGPathCreateMutable ();
   
    CGPathMoveToPoint (roundedPath, NULL, CGRectGetMinX (rect) + radius, CGRectGetMinY (rect));
    CGPathAddArc (roundedPath, NULL, CGRectGetMaxX (rect)-radius, CGRectGetMinY (rect)+radius, radius, 3*M_PI/2, 0, 0);
    CGPathAddArc (roundedPath, NULL, CGRectGetMaxX (rect)-radius, CGRectGetMaxY (rect)-radius, radius, 0, M_PI/2, 0);
    CGPathAddArc (roundedPath, NULL, CGRectGetMinX (rect)+radius, CGRectGetMaxY (rect)-radius, radius, M_PI/2, M_PI, 0);
    CGPathAddArc (roundedPath, NULL, CGRectGetMinX (rect)+radius, CGRectGetMinY (rect)+radius, radius, M_PI, 3*M_PI/2, 0);    
    CGPathCloseSubpath (roundedPath);
   
   
   
    //If background defined, fill roundedPath and add some light effect from the top
   
    if (backgroundColor)
    {
        //Fill background
        CGContextSetFillColorWithColor (context, [backgroundColor CGColor]);
        CGContextAddPath (context, roundedPath);
        CGContextFillPath (context);
       
        //Clip area
        CGContextAddPath (context, roundedPath);         
        CGContextClip (context);
       
       
        //     Draw elipse
        CGContextSetFillColorWithColor (context, [[[UIColor whiteColor] colorWithAlphaComponent:40.0/255.0] CGColor]);
       
       
       
        CGContextScaleCTM (context, OVALSCALEX, OVALSCALEY);
        CGContextAddArc (context,
                         (rect.origin.x+rect.size.width/2.0)/OVALSCALEX,
                         (rect.origin.y+0.0)/OVALSCALEY,
                         rect.size.width/2.0*425.0/270.0,
                         0.0, 2*M_PI, 1);
        CGContextFillPath (context);
       
        CGContextRestoreGState (context);     // Reset CTM
        CGContextSaveGState (context);         
    }
   
    if (borderColor)
    {
        CGContextSetStrokeColorWithColor (context, [borderColor CGColor]);
        CGContextSetLineWidth (context, 3.0);
        CGContextAddPath (context, roundedPath);
        CGContextStrokePath (context);
    }
   
    CFRelease (roundedPath);
   
    CGContextRestoreGState (context);
}


- (void)dealloc {
    [super dealloc];
}

@end

No comments:

Post a Comment