//
// EGviewAlert.h--------------------------
#import <UIKit/UIKit.h>
@interface EGviewAlert : UIView
//alert view with heading message and ok button
typedef void (^closeAction)(id);
typedef void (^cancelAction)(id);
-(void)Alert:(NSString*)heading withMsg:(NSString*)message withOkBtn:(NSString*)okBtnMsg closeAction:(void (^)(id))completeBlock;
//alert view with heading message and ok button and cancel button
-(void)Alert:(NSString*)heading withMsg:(NSString*)message withOkBtn:(NSString*)okBtnMsg withCancelBtn:(NSString*)cancelBtnMsg closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction;
//for custom layout
-(void)customAlert:(NSDictionary *)messages closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction;
//heading view
@property (nonatomic,strong) UIView *HeadingView;
//heading text
@property (nonatomic,strong) UILabel *headingText;
//heading message
@property (nonatomic,strong) UITextView *msgText;
@property (nonatomic,strong) UILabel *subHeadingText;
@property (nonatomic,strong) UITextField *answerTextField;
//clsoe btn
@property (nonatomic,strong) UIButton *closeBtn;
@property (nonatomic,strong) UIButton *cancelBtn;
@property (copy, nonatomic) closeAction CloseAction;
@property (copy, nonatomic) cancelAction CancelAction;
@end
//
// EGviewAlert.m-----------------------------------------------------
#import "EGviewAlert.h"
#define _width 480.0f
#define _height 320.0f
@implementation EGviewAlert
{
CGSize updateSize;
CGPoint updatedFrame;
CGFloat headingFont;
CGFloat msgFont;
CGFloat space;
CGFloat button_width;
CGFloat button_height;
id data;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
space=20;
headingFont=21;
msgFont=17;
button_width=125;
button_height=50;
self.frame=CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
[self addHeadingView:CGSizeMake(frame.size.width, frame.size.height/5)];
}
return self;
}
- (instancetype)init
{
self = [super init];
if (self) {
headingFont=21;
msgFont=17;
space=20;
self.frame=CGRectMake(([UIScreen mainScreen].bounds.size.width/2-_width/2), ([UIScreen mainScreen].bounds.size.height/2-_height/2), _width, _height);
[self addHeadingView:CGSizeMake(self.frame.size.width, self.frame.size.height/5)];
}
return self;
}
//@add heading view with size
-(void)addHeadingView:(CGSize)size
{
_HeadingView=[[UIView alloc] init];
_HeadingView.frame=CGRectMake(0, 0, size.width, size.height);
_HeadingView.backgroundColor=[UIColor grayColor];
[self addSubview:_HeadingView];
//add uilabel to heading text
_headingText=[[UILabel alloc] init];
_headingText.frame=CGRectMake(0, 0, size.width, size.height);
[_headingText setFont:[UIFont boldSystemFontOfSize:headingFont]];
_headingText.textAlignment=NSTextAlignmentCenter;
_headingText.text=@"";
[_HeadingView addSubview:_headingText];
///add msgText
_msgText=[[UITextView alloc] init];
_msgText.frame=CGRectMake(0, ((_HeadingView.frame.origin.y+_headingText.frame.size.height)+space), size.width, _height/2);
_msgText.backgroundColor=[UIColor clearColor];
[_msgText setFont:[UIFont boldSystemFontOfSize:headingFont]];
_msgText.textAlignment=NSTextAlignmentCenter;\
_msgText.editable=false;
_msgText.scrollEnabled=false;
_msgText.text=@"";
[self addSubview:_msgText];
//add close btn
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.bounds];
self.layer.masksToBounds = NO;
self.layer.cornerRadius=10;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
self.layer.shadowOpacity = 0.5f;
self.layer.shadowPath = shadowPath.CGPath;
}
-(void)ClickEvent:(id)object
{
self.CloseAction(data);
[self removeFromSuperview];
}
-(void)CancelEvent:(id)object
{
self.CancelAction(nil);
[self removeFromSuperview];
}
//add shadow
-(void)addShadow
{
}
//update layout
-(void)updateLayout
{
}
//alert with ok btn
-(void)Alert:(NSString *)heading withMsg:(NSString *)message withOkBtn:(NSString *)okBtnMsg closeAction:(void (^)(id))completeBlock
{
_headingText.text=heading;
_msgText.text=message;
_closeBtn=[UIButton buttonWithType:UIButtonTypeSystem];
_closeBtn.frame=CGRectMake((_width/2-button_width/2), _height-75, button_width, button_height);
_closeBtn.backgroundColor=[UIColor grayColor];
[_closeBtn setTitle:okBtnMsg forState:UIControlStateNormal];
[self addSubview:_closeBtn];
[_closeBtn addTarget:self action:@selector(ClickEvent:) forControlEvents:UIControlEventTouchUpInside];
self.CloseAction = completeBlock;
}
//alert with ok and cancel button
-(void)Alert:(NSString *)heading withMsg:(NSString *)message withOkBtn:(NSString *)okBtnMsg withCancelBtn:(NSString *)cancelBtnMsg closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction
{
_closeBtn=[UIButton buttonWithType:UIButtonTypeSystem];
_closeBtn.frame=CGRectMake(50, _height-75, button_width, button_height);
_closeBtn.backgroundColor=[UIColor grayColor];
[_closeBtn setTitle:okBtnMsg forState:UIControlStateNormal];
[self addSubview:_closeBtn];
[_closeBtn addTarget:self action:@selector(ClickEvent:) forControlEvents:UIControlEventTouchUpInside];
_headingText.text=heading;
_msgText.text=message;
_cancelBtn=[UIButton buttonWithType:UIButtonTypeSystem];
_cancelBtn.frame=CGRectMake(480-175, _height-75, button_width, button_height);
_cancelBtn.backgroundColor=[UIColor grayColor];
[_cancelBtn setTitle:cancelBtnMsg forState:UIControlStateNormal];
[self addSubview:_cancelBtn];
[_cancelBtn addTarget:self action:@selector(CancelEvent:) forControlEvents:UIControlEventTouchUpInside];
self.CloseAction = completeBlock;
self.CancelAction =cancelAction;
}
//custom alert add any object label textfield button segment etc
-(void)customAlert:(id)messages closeAction:(void (^)(id))completeBlock cancelAction:(void (^)(id))cancelAction
{
//heading msg
//other msgs
_headingText.text=[messages objectForKey:@"heading"];
_msgText.hidden=true;
_closeBtn=[UIButton buttonWithType:UIButtonTypeSystem];
_closeBtn.frame=CGRectMake(50, _height-75, button_width, button_height);
_closeBtn.backgroundColor=[UIColor grayColor];
[_closeBtn setTitle:[messages objectForKey:@"closeText"] forState:UIControlStateNormal];
[self addSubview:_closeBtn];
[_closeBtn addTarget:self action:@selector(ClickEvent:) forControlEvents:UIControlEventTouchUpInside];
//sub heading text
_subHeadingText=[[UILabel alloc] init];
_subHeadingText.frame=CGRectMake(0, 100, self.frame.size.width, 50);
[_subHeadingText setFont:[UIFont boldSystemFontOfSize:headingFont]];
_subHeadingText.textAlignment=NSTextAlignmentCenter;
_subHeadingText.text=[messages objectForKey:@"msg"];
[self addSubview:_subHeadingText];
//adding answer text filed
_answerTextField=[[UITextField alloc] init];
_answerTextField.textAlignment=NSTextAlignmentCenter;
_answerTextField.keyboardType=UIKeyboardTypeASCIICapableNumberPad;
_answerTextField.frame=CGRectMake(25, 150, 400, 50);
_answerTextField.borderStyle=UITextBorderStyleLine;
[self addSubview:_answerTextField];
_cancelBtn=[UIButton buttonWithType:UIButtonTypeSystem];
_cancelBtn.frame=CGRectMake(480-175, _height-75, button_width, button_height);
_cancelBtn.backgroundColor=[UIColor grayColor];
[_cancelBtn setTitle:[messages objectForKey:@"cancelText"] forState:UIControlStateNormal];
[self addSubview:_cancelBtn];
[_cancelBtn addTarget:self action:@selector(CancelEvent:) forControlEvents:UIControlEventTouchUpInside];
self.CloseAction = completeBlock;
self.CancelAction =cancelAction;
}
-(void)dealloc
{
NSLog(@"all relased");
}
@end
importing and added file to your view controller---------------------------------------------------------------------
#import "ViewController.h"
#import "EGviewAlert.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
EGviewAlert *alert=[[EGviewAlert alloc] init];
alert.backgroundColor=[UIColor whiteColor];
[self.view addSubview:alert];
[alert customAlert:nil closeAction:^(id data) {
//
} cancelAction:^(id data) {
//
}]; // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Comments
Post a Comment