CustomAlertView.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // CustomAlertView.m
  3. // JasonDevelop
  4. //
  5. // Created by Jason Lee on 3/6/14.
  6. // Copyright (c) jasondevelop. All rights reserved.
  7. //
  8. #import <objc/runtime.h>
  9. #import "NSString-Addtions.h"
  10. #import "CustomAlertView.h"
  11. #import "Masonry.h"
  12. //#import "AppDelegate.h"
  13. #define kfButtonSize 260.0f
  14. #define kfBoxCap 4.0f
  15. static NSString *CustomAlertViewHandlerRunTimeAccosiationKey = @"CustomAlertViewHandlerRunTimeAccosiationKey";
  16. @interface CustomAlertView () {
  17. }
  18. @end
  19. @implementation CustomAlertView
  20. //커스텀 얼럿 인스턴스를 생성함, 단순 형태
  21. - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate OKButtonTitle:(NSString *)okButtonTitle cancelButtonTitle:(NSString *)cancelButtonTitle {
  22. NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"CustomAlertView"
  23. owner:nil
  24. options:nil];
  25. self = (CustomAlertView*)[nibViews firstObject];
  26. CGRect screenRect = [UIScreen mainScreen].bounds;
  27. self.frame = screenRect;
  28. //메시지, 버튼 타이틀 설정
  29. _okButtonTitle = okButtonTitle;
  30. _cancelButtonTitle = cancelButtonTitle;
  31. _lblBoldMessage.hidden = YES;
  32. _lblMessage2.hidden = YES;
  33. CGRect m1Rect = _lblMessage1.frame;
  34. _lblTitle.text = title;
  35. //텍스트 사이즈 만큼 레이블사이즈를 늘림
  36. _lblMessage1.text = message;
  37. m1Rect.size.height = [CommonUtil getLabelRealHeight:_lblMessage1];
  38. _lblMessage1.frame = m1Rect;
  39. [_btnConfirm setTitle:okButtonTitle forState:UIControlStateNormal];
  40. if (_cancelButtonTitle && ![_cancelButtonTitle isEmptyString]) {
  41. [_btnCancel setTitle:_cancelButtonTitle forState:UIControlStateNormal];
  42. }
  43. return self;
  44. }
  45. //커스텀 얼럿 인스턴스를 생성함, 볼드메시지와 메시지1, 메시지2를 가지고 있음.
  46. - (id)initWithTitle:(NSString *)title delegate:(id)delegate OKButtonTitle:(NSString *)okButtonTitle cancelButtonTitle:(NSString *)cancelButtonTitle message:(NSString *)messages,... {
  47. NSString *boldMessage = messages;
  48. NSString *message1 = nil, *message2 = nil;
  49. va_list args;
  50. va_start(args, messages);
  51. NSInteger i = 0;
  52. while ((messages = va_arg(args, id)) != nil) {//루프 - 입력된 메시지를 받아옴.
  53. if (i == 0) {
  54. message1 = messages;
  55. } else if (i == 1) {
  56. message2 = messages;
  57. }
  58. i++;
  59. }
  60. va_end(args);
  61. self = [self initWithTitle:title message:message1 delegate:delegate OKButtonTitle:okButtonTitle cancelButtonTitle:cancelButtonTitle];
  62. _lblBoldMessage.hidden = [boldMessage isEmptyString];
  63. _lblMessage2.hidden = [message2 isEmptyString];
  64. CGRect m1Rect = _lblMessage1.frame;
  65. if (!_lblBoldMessage.hidden) {//볼드 메시지가 있을 경우,
  66. _lblBoldMessage.text = boldMessage;
  67. [_lblBoldMessage sizeToFit];
  68. [CommonUtil moveToCenterHorizon:_lblBoldMessage withContainer:_popUpView];
  69. CGRect bRect = _lblBoldMessage.frame;
  70. bRect.origin.y = m1Rect.origin.y;
  71. _lblBoldMessage.frame = bRect;
  72. m1Rect.origin.y = bRect.origin.y + bRect.size.height + kfLabelMargin;
  73. _lblMessage1.frame = m1Rect;
  74. }
  75. if (!_lblMessage2.hidden) {//2번 메시지가 있을 경우,
  76. _lblMessage2.text = message2;
  77. [_lblMessage2 sizeToFit];
  78. [CommonUtil moveToCenterHorizon:_lblMessage2 withContainer:_popUpView];
  79. CGRect m2Rect = _lblMessage2.frame;
  80. m2Rect.origin.y = m1Rect.origin.y + m1Rect.size.height + kfLabelMargin;
  81. _lblMessage2.frame = m2Rect;
  82. }
  83. return self;
  84. }
  85. - (void)didMoveToSuperview {
  86. if (!_cancelButtonTitle) {
  87. if (_btnCancel && [_btnCancel superview]) {
  88. [_containerView removeConstraint:_constraintBtnSameWidth];
  89. [_btnCancel removeFromSuperview];
  90. _constraintBtnConfirmWidth.constant = (IPHONE_WIDTH - 30);
  91. _btnCancel = nil;
  92. }
  93. }
  94. }
  95. - (IBAction)btnConfirmTouched:(id)sender {
  96. [self alertView:self clickedButtonAtIndex:0];
  97. [self removeFromSuperview];
  98. }
  99. - (IBAction)btnCancelTouched:(id)sender {
  100. [self alertView:self clickedButtonAtIndex:1];
  101. [self removeFromSuperview];
  102. }
  103. - (void)show {
  104. if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
  105. return;
  106. }
  107. //얼럿이 뜨는 경우, 로딩창을 종료함.
  108. UIView *topView = [CommonUtil topView];
  109. if (!topView) {
  110. [self performSelector:@selector(show) withObject:nil afterDelay:0.3f];
  111. return;
  112. }
  113. [topView addSubview:self];
  114. [topView bringSubviewToFront:self];
  115. // NSLog(@"%s\n %@, %zd : 0=active, 1=inactive, 2=background", __PRETTY_FUNCTION__, topView, [UIApplication sharedApplication].applicationState);
  116. }
  117. - (void)layoutSubviews {
  118. [super layoutSubviews];
  119. if (IOS_VERSION >= 8.0) {
  120. CGRect screenRect = [UIScreen mainScreen].bounds;
  121. self.frame = screenRect;
  122. if (_maskingView.superview) {
  123. [_maskingView mas_makeConstraints:^(MASConstraintMaker *make) {
  124. make.width.mas_equalTo(screenRect.size.width);
  125. make.height.mas_equalTo(screenRect.size.height);
  126. make.center.equalTo(self);
  127. }];
  128. }
  129. if (_popUpView.superview) {
  130. [_popUpView mas_makeConstraints:^(MASConstraintMaker *make) {
  131. make.left.equalTo(_maskingView).offset(15);
  132. make.right.equalTo(_maskingView).offset(-15);
  133. CGFloat margin = ceil(screenRect.size.height - _popUpView.frame.size.height) / 2;
  134. make.top.mas_equalTo(margin);
  135. make.bottom.mas_equalTo(-margin);
  136. }];
  137. }
  138. }
  139. }
  140. - (void)showWithCompletion:(CustomAlertViewCallBackHandler)completion {
  141. //set runtime accosiation of object
  142. //param - sourse object for association, association key, association value, policy of association
  143. objc_setAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey), completion, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  144. // objc_setAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey), completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
  145. [self show];
  146. }
  147. - (void)alertView:(CustomAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  148. CustomAlertViewCallBackHandler completionHandler = objc_getAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey));
  149. if (completionHandler != nil) {
  150. completionHandler(alertView, buttonIndex);
  151. }
  152. }
  153. - (void)alertView:(CustomAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
  154. CustomAlertViewCallBackHandler completionHandler = objc_getAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey));
  155. if (completionHandler != nil) {
  156. completionHandler(alertView, buttonIndex);
  157. }
  158. }
  159. @end