KSPublicAlertView.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // KSPublicAlertView.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/6/28.
  6. //
  7. #import "KSPublicAlertView.h"
  8. #import <KSToolLibrary/UIView+Animation.h>
  9. @interface KSPublicAlertView ()
  10. @property (weak, nonatomic) IBOutlet UILabel *topTitle;
  11. @property (weak, nonatomic) IBOutlet UILabel *descLabel;
  12. @property (weak, nonatomic) IBOutlet UIButton *cancleButton;
  13. @property (weak, nonatomic) IBOutlet UIButton *sureButton;
  14. @property (nonatomic, copy) AlertCallback cancelCallback;
  15. @property (nonatomic, copy) AlertCallback sureCallback;
  16. @end
  17. @implementation KSPublicAlertView
  18. - (void)awakeFromNib {
  19. [super awakeFromNib];
  20. [self.cancleButton setTitleColor:CLIENT_THEMECOLOR forState:UIControlStateNormal];
  21. self.cancleButton.layer.borderColor = CLIENT_THEMECOLOR.CGColor;
  22. [self.sureButton setBackgroundColor:CLIENT_THEMECOLOR];
  23. }
  24. + (instancetype)shareInstanceWithTitle:(NSString *)title descMessage:(NSString *)descMsg leftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle cancelAction:(AlertCallback)cancelCallback sureAction:(AlertCallback)sureCallback {
  25. KSPublicAlertView *alertView = [[[NSBundle mainBundle] loadNibNamed:@"KSPublicAlertView" owner:nil options:nil] firstObject];
  26. alertView.topTitle.text = title;
  27. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  28. [paragraphStyle setLineSpacing:4];//调整行间距
  29. paragraphStyle.alignment = NSTextAlignmentCenter;
  30. NSMutableAttributedString *attrs = [[NSMutableAttributedString alloc] initWithString:descMsg attributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:[UIFont systemFontOfSize:16.0f],NSForegroundColorAttributeName:HexRGB(0x666666)}];
  31. alertView.descLabel.attributedText = attrs;
  32. [alertView.cancleButton setTitle:leftTitle forState:UIControlStateNormal];
  33. [alertView.sureButton setTitle:rightTitle forState:UIControlStateNormal];
  34. if (cancelCallback) {
  35. alertView.cancelCallback = cancelCallback;
  36. }
  37. if (sureCallback) {
  38. alertView.sureCallback = sureCallback;
  39. }
  40. [alertView showAlert];
  41. return alertView;
  42. }
  43. - (void)showAlert {
  44. [[NSObject getKeyWindow] addSubview:self];
  45. [self mas_makeConstraints:^(MASConstraintMaker *make) {
  46. make.left.top.bottom.right.mas_equalTo([NSObject getKeyWindow]);
  47. }];
  48. [self setPopAnimation];
  49. }
  50. - (void)hiddenAction {
  51. [self removeFromSuperview];
  52. }
  53. - (IBAction)cancleAction:(id)sender {
  54. if (self.cancelCallback) {
  55. self.cancelCallback();
  56. }
  57. [self hiddenAction];
  58. }
  59. - (IBAction)sureAction:(id)sender {
  60. if (self.sureCallback) {
  61. self.sureCallback();
  62. }
  63. [self hiddenAction];
  64. }
  65. /*
  66. // Only override drawRect: if you perform custom drawing.
  67. // An empty implementation adversely affects performance during animation.
  68. - (void)drawRect:(CGRect)rect {
  69. // Drawing code
  70. }
  71. */
  72. @end