MessageBox.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // MessageBox.m
  3. // PopupViewWrapper
  4. //
  5. // Created by Rui Zhang on 3/20/23.
  6. // Copyright © 2023 yangyi. All rights reserved.
  7. //
  8. #import "MessageBox.h"
  9. #import "RAUtils.h"
  10. @implementation MessageBox
  11. -(void)updateframe
  12. {
  13. int h = 0;
  14. h=self.labelTitle.frame.size.height+10+self.labelMessage.frame.size.height+10+_btnOK.frame.size.height+10+10;
  15. CGRect newframe = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width,h);
  16. self.frame = newframe;
  17. }
  18. //+ (instancetype)sharedInstance {
  19. //
  20. // static MessageBox *singleton = nil;
  21. // static dispatch_once_t onceToken;
  22. // dispatch_once(&onceToken, ^{
  23. //
  24. // singleton = [[MessageBox alloc] init];
  25. //// [[NSNotificationCenter defaultCenter] addObserver:singleton selector:@selector(handleLogin:) name:User_LoginOK_Notification object:nil];
  26. //
  27. //
  28. //
  29. //// self addObserver:self forKeyPath:self.scan_cart options:<#(NSKeyValueObservingOptions)#> context:<#(nullable void *)#>
  30. // });
  31. // return singleton;
  32. //}
  33. + (instancetype)Show:(NSString*) title message:(NSString*) message completion:(void (^ __nullable)(void))completion
  34. {
  35. MessageBox *mb =[[MessageBox alloc] init:completion];
  36. // [mb setCompletion:completion];
  37. [mb setCenter : [RAUtils keyWindow].center ];
  38. [mb setTitle:title];
  39. [mb setMessage:message];
  40. RAPopviewContainer *wrapper = [[RAPopviewContainer alloc] initWithView:mb];
  41. wrapper.modalView=true;
  42. wrapper.bgColor = [UIColor colorWithWhite:0 alpha:0.2];
  43. [wrapper show:[RAUtils keyWindow]];
  44. return mb;
  45. }
  46. - (instancetype)initWithFrame:(CGRect)frame {
  47. if (self = [super initWithFrame:frame]) {
  48. [self.labelTitle setNumberOfLines:2];
  49. [self.labelTitle sizeToFit];
  50. [self.labelMessage setNumberOfLines:4];
  51. [self.labelMessage sizeToFit];
  52. // self.layer.cornerRadius = 5.0f;
  53. // self.layer.masksToBounds = YES;
  54. //
  55. //// UILabel *clabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width-20, 100)];
  56. //// clabel.textAlignment = NSTextAlignmentCenter;
  57. //// clabel.text = @"Title Title Title Title";
  58. //// clabel.font=[UIFont boldSystemFontOfSize:17.0f];
  59. //// [clabel setLineBreakMode:NSLineBreakByWordWrapping];
  60. ////
  61. //// [clabel setNumberOfLines:2];
  62. ////
  63. //// [clabel sizeToFit];
  64. ////
  65. //
  66. // _labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(30, 10, frame.size.width-60, 40)];
  67. // _labelTitle.textAlignment = NSTextAlignmentCenter;
  68. // _labelTitle.text = @"Title Title Title Title";
  69. // _labelTitle.font=[UIFont boldSystemFontOfSize:20.0f];
  70. // [_labelTitle setLineBreakMode:NSLineBreakByWordWrapping];
  71. //
  72. //
  73. //
  74. // [self addSubview:_labelTitle];
  75. //
  76. //
  77. // _labelTitle.translatesAutoresizingMaskIntoConstraints = NO;
  78. //
  79. // NSLayoutConstraint *T_hl = [NSLayoutConstraint constraintWithItem:_labelTitle
  80. // attribute:NSLayoutAttributeLeading
  81. // relatedBy:NSLayoutRelationEqual
  82. // toItem:self
  83. // attribute:NSLayoutAttributeLeading
  84. // multiplier:1
  85. // constant:30];
  86. //
  87. //
  88. // NSLayoutConstraint *T_ht = [NSLayoutConstraint constraintWithItem:_labelTitle
  89. // attribute:NSLayoutAttributeTrailing
  90. // relatedBy:NSLayoutRelationEqual
  91. // toItem:self
  92. // attribute:NSLayoutAttributeTrailing
  93. // multiplier:1
  94. // constant:-30];
  95. //
  96. // NSLayoutConstraint *T_vt = [NSLayoutConstraint constraintWithItem:_labelTitle
  97. // attribute:NSLayoutAttributeTop
  98. // relatedBy:NSLayoutRelationEqual
  99. // toItem:self
  100. // attribute:NSLayoutAttributeTop
  101. // multiplier:1
  102. // constant:10];
  103. //// NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:hud
  104. //// attribute:NSLayoutAttributeWidth
  105. //// relatedBy:NSLayoutRelationEqual
  106. //// toItem:nil
  107. //// attribute:NSLayoutAttributeNotAnAttribute
  108. //// multiplier:0
  109. //// constant:HUDSIZE];
  110. //// NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:hud
  111. //// attribute:NSLayoutAttributeHeight
  112. //// relatedBy:NSLayoutRelationEqual
  113. //// toItem:nil
  114. //// attribute:NSLayoutAttributeNotAnAttribute
  115. //// multiplier:0
  116. //// constant:HUDSIZE];
  117. // [self addConstraints:@[T_hl,T_ht,T_vt]];
  118. //
  119. // CGSize maxSizeTitle =[labelTitle sizeThatFits:labelTitle.frame.size];
  120. // _labelMessage = [[UILabel alloc] initWithFrame:CGRectMake(30, 45, frame.size.width-60, 40)];
  121. // _labelMessage.textAlignment = NSTextAlignmentLeft;//NSTextAlignmentCenter;
  122. // _labelMessage.text = @"Message";
  123. // [_labelMessage setLineBreakMode:NSLineBreakByWordWrapping];
  124. //
  125. //
  126. // [self addSubview:_labelMessage];
  127. //
  128. //
  129. // _labelMessage.translatesAutoresizingMaskIntoConstraints = NO;
  130. //
  131. // NSLayoutConstraint *M_hl = [NSLayoutConstraint constraintWithItem:_labelMessage
  132. // attribute:NSLayoutAttributeLeading
  133. // relatedBy:NSLayoutRelationEqual
  134. // toItem:self
  135. // attribute:NSLayoutAttributeLeading
  136. // multiplier:1
  137. // constant:30];
  138. //
  139. //
  140. // NSLayoutConstraint *M_ht = [NSLayoutConstraint constraintWithItem:_labelMessage
  141. // attribute:NSLayoutAttributeTrailing
  142. // relatedBy:NSLayoutRelationEqual
  143. // toItem:self
  144. // attribute:NSLayoutAttributeTrailing
  145. // multiplier:1
  146. // constant:-30];
  147. //
  148. // NSLayoutConstraint *M_vt = [NSLayoutConstraint constraintWithItem:_labelMessage
  149. // attribute:NSLayoutAttributeTop
  150. // relatedBy:NSLayoutRelationEqual
  151. // toItem:_labelTitle
  152. // attribute:NSLayoutAttributeBottom
  153. // multiplier:1
  154. // constant:10];
  155. //
  156. // [self addConstraints:@[M_hl,M_ht,M_vt]];
  157. _btnOK = [[UIButton alloc] initWithFrame:CGRectMake(30, 20, 80, 40)];
  158. [_btnOK setTitle:@"OK" forState:UIControlStateNormal];
  159. [_btnOK setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];
  160. [_btnOK addTarget:self action:@selector(onBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  161. [_btnOK sizeToFit];
  162. [self addSubview:_btnOK];
  163. _btnOK.translatesAutoresizingMaskIntoConstraints = NO;
  164. NSLayoutConstraint *B_h = [NSLayoutConstraint constraintWithItem:_btnOK
  165. attribute:NSLayoutAttributeCenterX
  166. relatedBy:NSLayoutRelationEqual
  167. toItem:self
  168. attribute:NSLayoutAttributeCenterX
  169. multiplier:1
  170. constant:0];
  171. NSLayoutConstraint *B_w = [NSLayoutConstraint constraintWithItem:_btnOK
  172. attribute:NSLayoutAttributeWidth
  173. relatedBy:NSLayoutRelationEqual
  174. toItem:nil
  175. attribute:NSLayoutAttributeNotAnAttribute
  176. multiplier:0
  177. constant:60];
  178. NSLayoutConstraint *B_vt = [NSLayoutConstraint constraintWithItem:_btnOK
  179. attribute:NSLayoutAttributeTop
  180. relatedBy:NSLayoutRelationEqual
  181. toItem:self.labelMessage
  182. attribute:NSLayoutAttributeBottom
  183. multiplier:1
  184. constant:10];
  185. [self addConstraints:@[B_h,B_w,B_vt]];
  186. // labelTitle.text = @"Title Title Title Title Title Title Title";
  187. // [labelTitle sizeToFit];
  188. [self updateframe];
  189. // [self sizeToFit];
  190. // self resize
  191. // int a=0;
  192. }
  193. return self;
  194. }
  195. - (void)onBtnClicked:(id)sender {
  196. // 可以调用基类的hide方法
  197. [super hide];
  198. }
  199. /*
  200. // Only override drawRect: if you perform custom drawing.
  201. // An empty implementation adversely affects performance during animation.
  202. - (void)drawRect:(CGRect)rect {
  203. // Drawing code
  204. }
  205. */
  206. @end