[原创] 自定义UIAlertView

//
//  MyAlertView.h
//  MyAlertIView
//
//  Created by 金春浩 on 13-5-31.
//  Copyright (c) 2013年 jinchunhao. All rights reserved.
//

#import <UIKit/UIKit.h>
@protocol MyAlertViewDelegate <NSObject>

@optional
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end
@interface MyAlertView : UIAlertView
@property(nonatomic, assign) id<MyAlertViewDelegate> myyAlertViewDelegate;
@property(nonatomic, retain) UIImage *backgroundImage;
@property(nonatomic, retain) UIImage *contentImage;
@property(nonatomic, retain) UIImage *btnImage;
@property (nonatomic,retain) NSString * cancelText;
@property (nonatomic,retain) NSString * ConfrimText;
@property(nonatomic,retain) NSString * titile;
@property(nonatomic,retain) NSString * detail;
@property(nonatomic,assign) int btncount;

- (void)show;
- (id)initWithImage:(UIImage *)image contentImage:(UIImage *)content;
- (id)initWithImage:(UIImage *)image contentImage:(UIImage *)content rect:(CGRect) frame;
- (id)initWithImage:(UIImage *) image
       contentImage:(UIImage *)
   content btnImage:(UIImage *) btnimg
              title:(NSString *) titlemsg
             detail:(NSString *)detailmsg
          delegate:(id) delegate
      cancelbtnText:(NSString *) cacelbtntext
     confirmbtnText:(NSString *) confirmbtntext
              frame:(CGRect) frame;
@end

 

//
//  MyAlertView.m
//  MyAlertIView
//
//  Created by 金春浩 on 13-5-31.
//  Copyright (c) 2013年 jinchunhao. All rights reserved.
//

#import "MyAlertView.h"

@implementation MyAlertView
@synthesize myyAlertViewDelegate;
@synthesize backgroundImage;
@synthesize contentImage;
@synthesize btnImage;
@synthesize cancelText;
@synthesize ConfrimText;
@synthesize btncount;
@synthesize titile;
@synthesize detail;
@synthesize ifLeftOrRight;


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

- (id)initWithImage:(UIImage *)image contentImage:(UIImage *)content{
    if (self == [super init]) {
        self.backgroundImage = image;
        self.contentImage = content;
    }
    return self;
}
- (id)initWithImage:(UIImage *)image contentImage:(UIImage *)content rect:(CGRect) frame{
 
       self = [super initWithFrame:frame];

    if (self) {
        self = [super initWithFrame:frame];
        if (self) {
            
        }
        
    }
   
       return  self;
}
- (id)initWithImage:(UIImage *) image
                     contentImage:(UIImage *)
                    content btnImage:(UIImage *) btnimg
                    title:(NSString *)titlemsg
                    detail:(NSString *)detailmsg
                    delegate:(id) delegate
                    cancelbtnText:(NSString *) cacelbtntext
                    confirmbtnText:(NSString *) confirmbtntext
                    frame:(CGRect) frame{
   
    self = [super init];
    [self sizeToFit];
    //默认是有两个按钮
    self.btncount=2;
    if (self){
    if(image){
           self.backgroundImage=image;
            
        }
        if(content){
           self.contentImage=content;
        }
         if(btnimg){
         
            self.btnImage=btnimg;
        }
        
        if(cacelbtntext){
            self.cancelText=cancelText;
        
        }
        if(confirmbtntext){
            self.ConfrimText=confirmbtntext;
        
        }else{
            //当没有右边的按钮的时候,表示只有一个按钮
            self.btncount=1;
        
        }
        if(titlemsg){
            self.titile=titlemsg;
        
        }
        if(detailmsg){
            self.detail=detailmsg;
        }
        self.myyAlertViewDelegate=delegate;
        
    }

    return  self;

}
- (void)drawRect:(CGRect)rect
{
   
    //self.backgroundColor=[UIColor blackColor];
    //根据方向的改变重绘背景图片时会正常显示
    self.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin;
        CGSize imageSize = self.backgroundImage.size;
    [self.backgroundImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
    //每次都要重新设置
    self.bounds = CGRectMake(0, 0, imageSize.width,imageSize.height);
     
}
- (void) layoutSubviews {

    for (UIView *v in [self subviews]) {
        if ([v class] == [UIImageView class]){
            [v setHidden:YES];
        }
        
        
        if ([v isKindOfClass:[UIButton class]] ||
            [v isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {
            [v setHidden:YES];
        }
    }
    if (contentImage) {
        UIImageView *contentview = [[UIImageView alloc] initWithImage:self.contentImage];
        contentview.frame = CGRectMake(0, 0, self.backgroundImage.size.width, self.backgroundImage.size.height-20);
        [self addSubview:contentview];
    }
    UILabel *titlelbl = [[UILabel alloc]  initWithFrame:CGRectMake(100, 15, 100, 25)];
    titlelbl.backgroundColor=[UIColor clearColor];
    titlelbl.textColor=[UIColor whiteColor];
    titlelbl.font = [UIFont boldSystemFontOfSize:14.0f];
    titlelbl.numberOfLines = 0;// 不可少Label属性之一
    titlelbl.textAlignment = NSTextAlignmentCenter;
    
    //自动折行设置
    titlelbl.lineBreakMode = NSLineBreakByCharWrapping;
    
     if(self.titile){
    
        titlelbl.text = self.titile;
     }
    [self addSubview:titlelbl];
    UIScrollView * lblview=[[UIScrollView alloc] initWithFrame:CGRectMake(40, 40, 230, 38)];
    lblview.scrollEnabled=YES;
    lblview.multipleTouchEnabled=NO;
    
    UILabel *detaillbl = [[UILabel alloc]  initWithFrame:CGRectMake(0, 0, 230, 38)];
    detaillbl.backgroundColor=[UIColor clearColor];
    detaillbl.textColor=[UIColor whiteColor];
    detaillbl.font = [UIFont boldSystemFontOfSize:15.0f];
    detaillbl.numberOfLines = 0;// 不可少Label属性之一
    //detaillbl.textAlignment = NSTextAlignmentLeft;
    //自动折行设置
    titlelbl.lineBreakMode = NSLineBreakByCharWrapping;
    if(self.detail){
        //计算该字符串所占用的高度,然后动态设置scriollview和label标签高度!
        UIFont *font = [UIFont systemFontOfSize:15];
        detaillbl.text = self.detail;
        CGSize size = [self.detail sizeWithFont:font constrainedToSize:CGSizeMake(lblview.frame.size.width ,1000) lineBreakMode:NSLineBreakByWordWrapping];
        lblview.contentSize=size;
        detaillbl.frame=CGRectMake(0, 0, 230, size.height);
    }
    
    [lblview addSubview:detaillbl];
    [self addSubview:lblview];
       
    //当只有一个按钮的时候
    if(self.btncount==1){
        CGRect rc= CGRectMake(60, 100, 160, 36);
        [self getCancelbtn:rc];
      //两个按钮都在
    }else if(self.btncount==2){
        CGRect rc= CGRectMake(30, 100, 90, 36);
        [self getCancelbtn:rc];
        [self getConfrimbtn];
        
    }

}
//右边确认按钮
-(void) getConfrimbtn{

    UIButton *confrimbtn = [[UIButton alloc] initWithFrame:CGRectMake(160, 100, 90, 36)];
    confrimbtn.tag = 1;
    [confrimbtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    confrimbtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [self addSubview:confrimbtn];
    
    if(self.ConfrimText){
        
        [confrimbtn setTitle:self.ConfrimText forState:UIControlStateNormal];
    }else{
        [confrimbtn setTitle:@"确定" forState:UIControlStateNormal];
        
        
    }
    if(self.btnImage){
        
        [confrimbtn setBackgroundImage:self.btnImage forState:UIControlStateNormal];
    }

}

//左边确认按钮
-(void)getCancelbtn:(CGRect) rect{
    UIButton *cancelbtn = [[UIButton alloc] initWithFrame:rect];
    cancelbtn.tag = 0;
    if(self.cancelText){
        [cancelbtn setTitle:self.cancelText forState:UIControlStateNormal];
    }else{
        
        [cancelbtn setTitle:@"取消" forState:UIControlStateNormal];
    }
    if(self.btnImage){
        
        [cancelbtn setBackgroundImage:self.btnImage forState:UIControlStateNormal];
    }
    [cancelbtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
     cancelbtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [self addSubview:cancelbtn];

}

-(void) buttonClicked:(id)sender
{
    UIButton *btn = (UIButton *) sender;
    
    if (myyAlertViewDelegate) {
        if ([myyAlertViewDelegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
        {
            [myyAlertViewDelegate alertView:self clickedButtonAtIndex:btn.tag];
        }
    }
    
    [self dismissWithClickedButtonIndex:0 animated:YES];
    
}

- (void) show {
   
    [super show];
    CGSize imageSize = self.backgroundImage.size;
    self.bounds = CGRectMake(0, 0, imageSize.width,imageSize.height);

}
-(void) dealloc{
    [backgroundImage release];
    [contentImage release];
    [detail release];
    [titile release];
    [btnImage release];
    [cancelText release];
    [ConfrimText release];
    [super dealloc];

}
@end

调用

 UIImage * bgimg=[UIImage imageNamed:@"alertviewbg2.png"];
 UIImage * btnimg=[UIImage imageNamed:@"btnbg"];

 MyAlertView * myalert=[[MyAlertView alloc] initWithImage:bgimg contentImage:bgimg btnImage:btnimg title:@"提示" detail:@"infsadaafasfasdfo" delegate:self cancelbtnText:@"取消" confirmbtnText:@"确定" frame:CGRectZero];

    [myalert show];
    [myalert release];

posted @ 2013-06-04 08:13  金建彤  阅读(254)  评论(0编辑  收藏  举报