iOS:一個簡單的提示框

效果圖:


代碼:.h中

typedef void(^SBBlock)(NSInteger num);

@interface SBAlertView : UIView

/**

*? 蒙版

*/

@property (nonatomic,strong)UIView *blackView;

/**

*? 提示框

*/

@property (strong,nonatomic)UIView * alertview;

/**

*? 標題

*/

@property (strong,nonatomic)NSString * title;

/**

*? 內容

*/

@property (nonatomic,copy)NSString *contentStr;

-(void)initWithTitle:(NSString *)title contentStr:(NSString *)content BtnArray:(NSArray *)array andBlock:(SBBlock)block;


--------------------------------分割線--------------------------------

.m中


@interface SBAlertView ()

/**

*? 創建block對象

*/

@property (nonatomic, copy) SBBlock block;

/**

*? 標題label

*/

@property(strong,nonatomic)UILabel *isCreate;

/**

*? 內容

*/

@property(strong,nonatomic)UILabel *attenL;

/**

*? 取消按鈕

*/

@property(strong,nonatomic)UIButton *btn1;

/**

*? 確定按鈕

*/

@property(strong,nonatomic)UIButton *btn2;

/**

*? 橫線

*/

@property(strong,nonatomic)UILabel *label1;

/**

*? 豎線

*/

@property(strong,nonatomic)UILabel *label2;

@end

@implementation SBAlertView

- (instancetype)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

//創建遮罩

self.blackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

self.blackView.backgroundColor = [UIColor blackColor];

self.blackView.alpha = 0.5;

UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];

tap1.cancelsTouchesInView = NO;

[self.blackView addGestureRecognizer:tap1];

[self addSubview:_blackView];

//創建alert

self.alertview = [[UIView alloc]initWithFrame:CGRectMake(30, HEIGHT/2-120-64, WIDTH-60, 240)];

//? ? ? ? self.alertview.center = self.center;

self.alertview.layer.cornerRadius = 5;

self.alertview.clipsToBounds = YES;

self.alertview.backgroundColor = [UIColor whiteColor];

[self addSubview:_alertview];

[self createAliertView];

}

return self;

}

-(void)viewTapped:(UITapGestureRecognizer*)tap1

{

self.alpha = 0.0;

[self removeFromSuperview];

self.alertview = nil;

}

//創建View

-(void)createAliertView{

self.isCreate = [[UILabel alloc]initWithFrame:CGRectMake( 20,10, self.alertview.frame.size.width-40, 43)];

self.isCreate.font = [UIFont boldSystemFontOfSize:18];

self.isCreate.textAlignment=NSTextAlignmentCenter;

self.attenL = [[UILabel alloc]initWithFrame:CGRectMake(self.isCreate.frame.origin.x, self.isCreate.frame.origin.y+20, self.alertview.frame.size.width-40, 100)];

self.attenL.font = [UIFont systemFontOfSize:15];

self.attenL.numberOfLines = 0;

self.attenL.font = [UIFont systemFontOfSize:14];

self.attenL.textColor = [UIColor blackColor];

[self.alertview addSubview:self.attenL];

[self.alertview addSubview:self.isCreate];

[self createBtnTitleWithCancel];

}

//創建button

- (void)createBtnTitleWithCancel

{

CGFloat m = self.alertview.frame.size.width;

//橫線和豎線

self.label1=[[UILabel alloc] initWithFrame:CGRectMake(0, self.alertview.height-50, self.alertview.width, 1)];

self.label1.backgroundColor=[UIColor lightGrayColor];

[self.alertview addSubview:self.label1];

self.label2=[[UILabel alloc] initWithFrame:CGRectMake(self.alertview.width/2, self.alertview.height-50, 1, 50)];

self.label2.backgroundColor=[UIColor lightGrayColor];

[self.alertview addSubview:self.label2];

//取消按鈕

self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

self.btn1.tag=1001;

self.btn1.frame = CGRectMake(0, self.alertview.height-50,m/2, 50);

[self.btn1 setTitleColor:TINTCOLOR forState:UIControlStateNormal];

[self.btn1 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

[self.btn1.titleLabel setFont:[UIFont systemFontOfSize:18]];

//確定按鈕

self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];

self.btn2.tag=1002;

self.btn2.frame = CGRectMake(m/2, self.alertview.height-50,m/2, 50);

[self.btn2 setTitleColor:TINTCOLOR forState:UIControlStateNormal];

[self.btn2 addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

[self.btn2.titleLabel setFont:[UIFont systemFontOfSize:18]];

[self.alertview addSubview:self.btn1];

[self.alertview addSubview:self.btn2];

}

- (void)clickBtn:(UIButton *)btn

{

self.block(btn.tag-1000);

[self.blackView removeFromSuperview];

}

//傳值,并且高度自適應

-(void)initWithTitle:(NSString *)title contentStr:(NSString *)content BtnArray:(NSArray *)array andBlock:(SBBlock)block

{

self.block=block;

CGFloat m = self.alertview.frame.size.width;

self.isCreate.text = title;

self.attenL.text= content;

if (array.count==1) {

[self.btn2 setTitle:[NSString stringWithFormat:@"%@", array[0]] forState:UIControlStateNormal];

}else{

[self.btn1 setTitle:[NSString stringWithFormat:@"%@", array[0]] forState:UIControlStateNormal];

[self.btn2 setTitle:[NSString stringWithFormat:@"%@", array[1]] forState:UIControlStateNormal];

}

CGRect r = [content boundingRectWithSize:CGSizeMake(self.alertview.width-40,1000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.f]} context:nil];

self.attenL.frame = CGRectMake(self.isCreate.frame.origin.x, CGRectGetMaxY(self.isCreate.frame), self.alertview.frame.size.width-40, r.size.height);

if (array.count==1) {

self.btn2.frame=CGRectMake(0, CGRectGetMaxY(self.attenL.frame)+15, m, 50);

[self.btn1 removeFromSuperview];

[self.label2 removeFromSuperview];

}else{

self.btn1.frame=CGRectMake(0, CGRectGetMaxY(self.attenL.frame)+15, m/2, 50);

self.btn2.frame=CGRectMake(m/2, CGRectGetMaxY(self.attenL.frame)+15,m/2, 50);

}

self.label1.frame=CGRectMake(0, CGRectGetMaxY(self.attenL.frame)+15, self.alertview.width, 1);

self.label2.frame=CGRectMake(self.alertview.width/2,CGRectGetMaxY(self.attenL.frame)+15, 1, 50);

self.alertview.frame=CGRectMake(30, HEIGHT/2-120-64,WIDTH-60 ,CGRectGetMaxY(self.btn2.frame)+1);

}

--------------------------------分割線--------------------------------

使用:


SBAlertView *alertView = [[SBAlertView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

NSArray *array=@[@"退出",@"重新登錄"];//也可以只寫一個,看需要

[alertView initWithTitle:@"下線通知" contentStr:@"你的賬號在另一臺設備登錄。如非本人操作,則密碼可能已泄露,建議立即修改密碼或凍結賬號。" BtnArray:array andBlock:^(NSInteger num){

if (num == 1)

{

// 退出

NSLog(@"點擊退出");

}

else

{

NSLog(@"點擊重新登錄");

}

}];



寫在后面:新手初試,不喜勿噴。感謝~(喜歡點??)

有不對的地方請告訴我,一起進步。再次感謝!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容