CustomAlertView.m 6.3 KB

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