FDAlertView.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // FDAlertView.m
  3. // FDUIKitObjC
  4. //
  5. // Created by fandongtongxue on 2020/2/27.
  6. //
  7. #import "FDAlertView.h"
  8. #import "FDAction.h"
  9. #import "FDUIDefine.h"
  10. #import "FDUIColorDefine.h"
  11. #import "UIView+FD.h"
  12. #import <YYKit/YYKit.h>
  13. #import <FDFoundationObjC/FDFoundationObjC.h>
  14. @interface FDAlertView ()
  15. @property(nonatomic, strong) UIImageView *imageView;
  16. @property(nonatomic, strong) UILabel *titleLabel;
  17. @property(nonatomic, strong) UILabel *contentLabel;
  18. @property(nonatomic, assign) CGFloat bottomY;
  19. @property(nonatomic, strong) NSMutableArray *actionArray;
  20. @property(nonatomic, strong) UIView *bottomLineView;
  21. @property(nonatomic, strong) UIView *marginView;
  22. @end
  23. @implementation FDAlertView
  24. - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message{
  25. if (self = [super initWithFrame:CGRectMake(40, FD_ScreenHeight, FD_ScreenWidth - 80, 1)]) {
  26. self.backgroundColor = FD_WhiteColor;
  27. self.layer.cornerRadius = 10;
  28. self.clipsToBounds = YES;
  29. self.titleLabel.text = title;
  30. [self.contentLabel setText:message];
  31. if (title.length) {
  32. [self addSubview:self.titleLabel];
  33. }
  34. if (message.length) {
  35. [self addSubview:self.contentLabel];
  36. }
  37. self.contentLabel.frame = CGRectMake(10, title.length ? _titleLabel.fd_bottom : 35, self.fd_width - 20, [message fd_textSizeIn:CGSizeMake(self.fd_width - 20, MAXFLOAT) font:[UIFont systemFontOfSize:14]].height + 20);
  38. self.bottomY = self.contentLabel.fd_bottom + 20;
  39. }
  40. return self;
  41. }
  42. - (instancetype)initWithImage:(UIImage *)image title:(NSString *)title message:(NSString *)message{
  43. if (self = [super initWithFrame:CGRectMake(40, FD_ScreenHeight, FD_ScreenWidth - 80, 1)]) {
  44. self.backgroundColor = FD_WhiteColor;
  45. self.layer.cornerRadius = 10;
  46. self.clipsToBounds = YES;
  47. [self.imageView setImage:image];
  48. [self addSubview:self.imageView];
  49. self.titleLabel.text = title;
  50. [self.contentLabel setText:message];
  51. [self addSubview:self.titleLabel];
  52. [self addSubview:self.contentLabel];
  53. self.contentLabel.frame = CGRectMake(10, _titleLabel.fd_bottom, self.fd_width - 20, [message fd_textSizeIn:CGSizeMake(self.fd_width - 20, MAXFLOAT) font:[UIFont systemFontOfSize:14]].height + 20);
  54. self.bottomY = self.contentLabel.fd_bottom + 20;
  55. }
  56. return self;
  57. }
  58. - (void)addAction:(FDAction *)action{
  59. [self.actionArray addObject:action];
  60. }
  61. - (void)onClick:(UIButton *)sender{
  62. NSLog(@"FDAlertView:点击了%@",sender.titleLabel.text);
  63. FDAction *action = self.actionArray[sender.tag -100];
  64. if (action.fd_handleAction) {
  65. action.fd_handleAction();
  66. }
  67. [self hide];
  68. }
  69. - (void)show:(UIView *)superView{
  70. for (NSInteger i = 0; i < self.actionArray.count; i++) {
  71. CGFloat buttonWidth = self.width / self.actionArray.count;
  72. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(buttonWidth * i, self.bottomY, buttonWidth, 40)];
  73. FDAction *action = self.actionArray[i];
  74. [button setTitle:action.title forState:UIControlStateNormal];
  75. if (action.type == FDActionTypeDefault) {
  76. [button setTitleColor:[UIColor colorWithHexString:@"#9152F8"] forState:UIControlStateNormal];
  77. }else{
  78. [button setTitleColor:[UIColor colorWithHexString:@"#777777"] forState:UIControlStateNormal];
  79. }
  80. [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
  81. button.tag = 100 + i;
  82. [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
  83. [self addSubview:button];
  84. }
  85. self.bounds = CGRectMake(0, 0, self.fd_width, self.bottomY + 40);
  86. [self addSubview:self.bottomLineView];
  87. if (self.actionArray.count > 1) {
  88. [self addSubview:self.marginView];
  89. }
  90. [super show:superView type:FDPopTypeCenter];
  91. }
  92. - (NSMutableArray *)actionArray{
  93. if (!_actionArray) {
  94. _actionArray = [NSMutableArray array];
  95. }
  96. return _actionArray;
  97. }
  98. - (UIImageView *)imageView{
  99. if (!_imageView) {
  100. _imageView = [[UIImageView alloc]initWithFrame:CGRectMake((FD_ScreenWidth - 80 - 40 ) / 2, 25, 40, 40)];
  101. }
  102. return _imageView;
  103. }
  104. - (UILabel *)titleLabel{
  105. if (!_titleLabel) {
  106. _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.imageView.image ? self.imageView.bottom + 10 : 20, self.fd_width, 22)];
  107. _titleLabel.textAlignment = NSTextAlignmentCenter;
  108. _titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
  109. _titleLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  110. }
  111. return _titleLabel;
  112. }
  113. - (UILabel *)contentLabel{
  114. if (!_contentLabel) {
  115. _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, _titleLabel.fd_bottom + 20, self.fd_width - 20, 20)];
  116. _contentLabel.textAlignment = NSTextAlignmentCenter;
  117. _contentLabel.font = [UIFont systemFontOfSize:14];
  118. _contentLabel.numberOfLines = 0;
  119. _contentLabel.textColor = [UIColor colorWithHexString:@"333333"];
  120. }
  121. return _contentLabel;
  122. }
  123. - (UIView *)bottomLineView{
  124. if (!_bottomLineView) {
  125. _bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height - 40, self.width, 0.5)];
  126. _bottomLineView.backgroundColor = [UIColor colorWithHexString:@"#E6E6E6"];
  127. }
  128. return _bottomLineView;
  129. }
  130. - (UIView *)marginView{
  131. if (!_marginView) {
  132. _marginView = [[UIView alloc]initWithFrame:CGRectMake(self.width / 2 - 0.5, self.height - 40, 0.5, self.width / 2)];
  133. _marginView.backgroundColor = [UIColor colorWithHexString:@"#E6E6E6"];
  134. }
  135. return _marginView;
  136. }
  137. @end