// // MessageBox.m // PopupViewWrapper // // Created by Rui Zhang on 3/20/23. // Copyright © 2023 yangyi. All rights reserved. // #import "MessageBox.h" #import "RAUtils.h" @implementation MessageBox -(void)updateframe { int h = 0; h=self.labelTitle.frame.size.height+10+self.labelMessage.frame.size.height+10+_btnOK.frame.size.height+10+10; CGRect newframe = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width,h); self.frame = newframe; } //+ (instancetype)sharedInstance { // // static MessageBox *singleton = nil; // static dispatch_once_t onceToken; // dispatch_once(&onceToken, ^{ // // singleton = [[MessageBox alloc] init]; //// [[NSNotificationCenter defaultCenter] addObserver:singleton selector:@selector(handleLogin:) name:User_LoginOK_Notification object:nil]; // // // //// self addObserver:self forKeyPath:self.scan_cart options:<#(NSKeyValueObservingOptions)#> context:<#(nullable void *)#> // }); // return singleton; //} + (instancetype)Show:(NSString*) title message:(NSString*) message completion:(void (^ __nullable)(void))completion { MessageBox *mb =[[MessageBox alloc] init:completion]; // [mb setCompletion:completion]; [mb setCenter : [RAUtils keyWindow].center ]; [mb setTitle:title]; [mb setMessage:message]; RAPopviewContainer *wrapper = [[RAPopviewContainer alloc] initWithView:mb]; wrapper.modalView=true; wrapper.bgColor = [UIColor colorWithWhite:0 alpha:0.2]; [wrapper show:[RAUtils keyWindow]]; return mb; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self.labelTitle setNumberOfLines:2]; [self.labelTitle sizeToFit]; [self.labelMessage setNumberOfLines:4]; [self.labelMessage sizeToFit]; // self.layer.cornerRadius = 5.0f; // self.layer.masksToBounds = YES; // //// UILabel *clabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width-20, 100)]; //// clabel.textAlignment = NSTextAlignmentCenter; //// clabel.text = @"Title Title Title Title"; //// clabel.font=[UIFont boldSystemFontOfSize:17.0f]; //// [clabel setLineBreakMode:NSLineBreakByWordWrapping]; //// //// [clabel setNumberOfLines:2]; //// //// [clabel sizeToFit]; //// // // _labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, frame.size.width-60, 40)]; // _labelTitle.textAlignment = NSTextAlignmentCenter; // _labelTitle.text = @"Title Title Title Title"; // _labelTitle.font=[UIFont boldSystemFontOfSize:20.0f]; // [_labelTitle setLineBreakMode:NSLineBreakByWordWrapping]; // // // // [self addSubview:_labelTitle]; // // // _labelTitle.translatesAutoresizingMaskIntoConstraints = NO; // // NSLayoutConstraint *T_hl = [NSLayoutConstraint constraintWithItem:_labelTitle // attribute:NSLayoutAttributeLeading // relatedBy:NSLayoutRelationEqual // toItem:self // attribute:NSLayoutAttributeLeading // multiplier:1 // constant:30]; // // // NSLayoutConstraint *T_ht = [NSLayoutConstraint constraintWithItem:_labelTitle // attribute:NSLayoutAttributeTrailing // relatedBy:NSLayoutRelationEqual // toItem:self // attribute:NSLayoutAttributeTrailing // multiplier:1 // constant:-30]; // // NSLayoutConstraint *T_vt = [NSLayoutConstraint constraintWithItem:_labelTitle // attribute:NSLayoutAttributeTop // relatedBy:NSLayoutRelationEqual // toItem:self // attribute:NSLayoutAttributeTop // multiplier:1 // constant:10]; //// NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:hud //// attribute:NSLayoutAttributeWidth //// relatedBy:NSLayoutRelationEqual //// toItem:nil //// attribute:NSLayoutAttributeNotAnAttribute //// multiplier:0 //// constant:HUDSIZE]; //// NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:hud //// attribute:NSLayoutAttributeHeight //// relatedBy:NSLayoutRelationEqual //// toItem:nil //// attribute:NSLayoutAttributeNotAnAttribute //// multiplier:0 //// constant:HUDSIZE]; // [self addConstraints:@[T_hl,T_ht,T_vt]]; // // CGSize maxSizeTitle =[labelTitle sizeThatFits:labelTitle.frame.size]; // _labelMessage = [[UILabel alloc] initWithFrame:CGRectMake(30, 45, frame.size.width-60, 40)]; // _labelMessage.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter; // _labelMessage.text = @"Message"; // [_labelMessage setLineBreakMode:NSLineBreakByWordWrapping]; // // // [self addSubview:_labelMessage]; // // // _labelMessage.translatesAutoresizingMaskIntoConstraints = NO; // // NSLayoutConstraint *M_hl = [NSLayoutConstraint constraintWithItem:_labelMessage // attribute:NSLayoutAttributeLeading // relatedBy:NSLayoutRelationEqual // toItem:self // attribute:NSLayoutAttributeLeading // multiplier:1 // constant:30]; // // // NSLayoutConstraint *M_ht = [NSLayoutConstraint constraintWithItem:_labelMessage // attribute:NSLayoutAttributeTrailing // relatedBy:NSLayoutRelationEqual // toItem:self // attribute:NSLayoutAttributeTrailing // multiplier:1 // constant:-30]; // // NSLayoutConstraint *M_vt = [NSLayoutConstraint constraintWithItem:_labelMessage // attribute:NSLayoutAttributeTop // relatedBy:NSLayoutRelationEqual // toItem:_labelTitle // attribute:NSLayoutAttributeBottom // multiplier:1 // constant:10]; // // [self addConstraints:@[M_hl,M_ht,M_vt]]; _btnOK = [[UIButton alloc] initWithFrame:CGRectMake(30, 20, 80, 40)]; [_btnOK setTitle:@"OK" forState:UIControlStateNormal]; [_btnOK setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal]; [_btnOK addTarget:self action:@selector(onBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; [_btnOK sizeToFit]; [self addSubview:_btnOK]; _btnOK.translatesAutoresizingMaskIntoConstraints = NO; NSLayoutConstraint *B_h = [NSLayoutConstraint constraintWithItem:_btnOK attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]; NSLayoutConstraint *B_w = [NSLayoutConstraint constraintWithItem:_btnOK attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:60]; NSLayoutConstraint *B_vt = [NSLayoutConstraint constraintWithItem:_btnOK attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelMessage attribute:NSLayoutAttributeBottom multiplier:1 constant:10]; [self addConstraints:@[B_h,B_w,B_vt]]; // labelTitle.text = @"Title Title Title Title Title Title Title"; // [labelTitle sizeToFit]; [self updateframe]; // [self sizeToFit]; // self resize // int a=0; } return self; } - (void)onBtnClicked:(id)sender { // 可以调用基类的hide方法 [super hide]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end