CustomAlertView.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. return self;
  40. }
  41. //커스텀 얼럿 인스턴스를 생성함, 볼드메시지와 메시지1, 메시지2를 가지고 있음.
  42. - (id)initWithTitle:(NSString *)title delegate:(id)delegate OKButtonTitle:(NSString *)okButtonTitle cancelButtonTitle:(NSString *)cancelButtonTitle message:(NSString *)messages,... {
  43. NSString *boldMessage = messages;
  44. NSString *message1 = nil, *message2 = nil;
  45. va_list args;
  46. va_start(args, messages);
  47. NSInteger i = 0;
  48. while ((messages = va_arg(args, id)) != nil) {//루프 - 입력된 메시지를 받아옴.
  49. if (i == 0) {
  50. message1 = messages;
  51. } else if (i == 1) {
  52. message2 = messages;
  53. }
  54. i++;
  55. }
  56. va_end(args);
  57. self = [self initWithTitle:title message:message1 delegate:delegate OKButtonTitle:okButtonTitle cancelButtonTitle:cancelButtonTitle];
  58. _lblBoldMessage.hidden = [boldMessage isEmptyString];
  59. _lblMessage2.hidden = [message2 isEmptyString];
  60. CGRect m1Rect = _lblMessage1.frame;
  61. if (!_lblBoldMessage.hidden) {//볼드 메시지가 있을 경우,
  62. _lblBoldMessage.text = boldMessage;
  63. [_lblBoldMessage sizeToFit];
  64. [CommonUtil moveToCenterHorizon:_lblBoldMessage withContainer:_popUpView];
  65. CGRect bRect = _lblBoldMessage.frame;
  66. bRect.origin.y = m1Rect.origin.y;
  67. _lblBoldMessage.frame = bRect;
  68. m1Rect.origin.y = bRect.origin.y + bRect.size.height + kfLabelMargin;
  69. _lblMessage1.frame = m1Rect;
  70. }
  71. if (!_lblMessage2.hidden) {//2번 메시지가 있을 경우,
  72. _lblMessage2.text = message2;
  73. [_lblMessage2 sizeToFit];
  74. [CommonUtil moveToCenterHorizon:_lblMessage2 withContainer:_popUpView];
  75. CGRect m2Rect = _lblMessage2.frame;
  76. m2Rect.origin.y = m1Rect.origin.y + m1Rect.size.height + kfLabelMargin;
  77. _lblMessage2.frame = m2Rect;
  78. }
  79. return self;
  80. }
  81. - (void)didMoveToSuperview {
  82. if (!_cancelButtonTitle) {
  83. if (_btnCancel && [_btnCancel superview]) {
  84. [_containerView removeConstraint:_constraintBtnSameWidth];
  85. [_btnCancel removeFromSuperview];
  86. _constraintBtnConfirmWidth.constant = (IPHONE_WIDTH - 20) - 2.0f;
  87. _btnCancel = nil;
  88. }
  89. }
  90. }
  91. - (IBAction)btnConfirmTouched:(id)sender {
  92. [self alertView:self clickedButtonAtIndex:0];
  93. [self removeFromSuperview];
  94. }
  95. - (IBAction)btnCancelTouched:(id)sender {
  96. [self alertView:self clickedButtonAtIndex:1];
  97. [self removeFromSuperview];
  98. }
  99. - (void)show {
  100. if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
  101. return;
  102. }
  103. //얼럿이 뜨는 경우, 로딩창을 종료함.
  104. UIView *topView = [CommonUtil topView];
  105. if (!topView) {
  106. [self performSelector:@selector(show) withObject:nil afterDelay:0.3f];
  107. return;
  108. }
  109. [topView addSubview:self];
  110. [topView bringSubviewToFront:self];
  111. NSLog(@"%s\n %@, %zd : 0=active, 1=inactive, 2=background", __PRETTY_FUNCTION__, topView, [UIApplication sharedApplication].applicationState);
  112. }
  113. - (void)layoutSubviews {
  114. [super layoutSubviews];
  115. if (IOS_VERSION >= 8.0) {
  116. CGRect screenRect = [UIScreen mainScreen].bounds;
  117. self.frame = screenRect;
  118. if (_maskingView.superview) {
  119. [_maskingView mas_makeConstraints:^(MASConstraintMaker *make) {
  120. make.width.mas_equalTo(screenRect.size.width);
  121. make.height.mas_equalTo(screenRect.size.height);
  122. make.center.equalTo(self);
  123. }];
  124. }
  125. if (_popUpView.superview) {
  126. [_popUpView mas_makeConstraints:^(MASConstraintMaker *make) {
  127. make.left.equalTo(_maskingView).offset(10);
  128. make.right.equalTo(_maskingView).offset(-10);
  129. CGFloat margin = (screenRect.size.height - _popUpView.frame.size.height) / 2;
  130. make.top.mas_equalTo(margin);
  131. make.bottom.mas_equalTo(-margin);
  132. }];
  133. }
  134. }
  135. }
  136. - (void)showWithCompletion:(CustomAlertViewCallBackHandler)completion {
  137. //set runtime accosiation of object
  138. //param - sourse object for association, association key, association value, policy of association
  139. objc_setAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey), completion, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  140. // objc_setAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey), completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
  141. [self show];
  142. }
  143. - (void)alertView:(CustomAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  144. CustomAlertViewCallBackHandler completionHandler = objc_getAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey));
  145. if (completionHandler != nil) {
  146. completionHandler(alertView, buttonIndex);
  147. }
  148. }
  149. - (void)alertView:(CustomAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
  150. CustomAlertViewCallBackHandler completionHandler = objc_getAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey));
  151. if (completionHandler != nil) {
  152. completionHandler(alertView, buttonIndex);
  153. }
  154. }
  155. @end