FDActionSheet.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // FDActionSheet.m
  3. // FDFoundationObjC
  4. //
  5. // Created by fandongtongxue on 2020/2/27.
  6. //
  7. #import "FDActionSheet.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 FDActionSheet ()
  15. @property(nonatomic, strong) UILabel *titleLabel;
  16. @property(nonatomic, strong) UILabel *contentLabel;
  17. @property(nonatomic, strong) UIView *bottomLineView;
  18. @property(nonatomic, assign) CGFloat bottomY;
  19. @property(nonatomic, strong) NSMutableArray *actionArray;
  20. @end
  21. @implementation FDActionSheet
  22. - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message{
  23. if (self = [super initWithFrame:CGRectMake(0, FD_ScreenHeight, FD_ScreenWidth, 1)]) {
  24. self.backgroundColor = FD_WhiteColor;
  25. self.layer.cornerRadius = 5;
  26. self.clipsToBounds = YES;
  27. if (title.length) {
  28. self.titleLabel.text = title;
  29. [self addSubview:self.titleLabel];
  30. self.titleLabel.frame = CGRectMake(10, 0, self.fd_width - 20, [title fd_textSizeIn:CGSizeMake(self.width - 20, MAXFLOAT) font:[UIFont systemFontOfSize:16]].height + 20);
  31. self.bottomLineView.fd_top = self.titleLabel.fd_bottom - 1;
  32. self.bottomY = self.titleLabel.fd_bottom;
  33. }
  34. if (message.length){
  35. self.contentLabel.text = message;
  36. [self addSubview:self.contentLabel];
  37. 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);
  38. self.bottomLineView.fd_top = self.contentLabel.fd_bottom - 1;
  39. self.bottomY = self.contentLabel.fd_bottom;
  40. }
  41. }
  42. return self;
  43. }
  44. - (void)addAction:(FDAction *)action{
  45. [self.actionArray addObject:action];
  46. }
  47. - (void)onClick:(UIButton *)sender{
  48. NSLog(@"FDActionSheet:点击了%@",sender.titleLabel.text);
  49. FDAction *action = self.actionArray[sender.tag -100];
  50. if (action.fd_handleAction) {
  51. action.fd_handleAction();
  52. }
  53. [self hide];
  54. }
  55. - (void)show:(UIView *)superView{
  56. for (NSInteger i = 0; i < self.actionArray.count; i++) {
  57. if (i == self.actionArray.count - 1) {
  58. UIView *spiltView = [[UIView alloc]initWithFrame:CGRectMake(0, self.bottomY, self.fd_width, 3)];
  59. spiltView.backgroundColor = self.backColor ? self.backColor : [UIColor colorWithHexString:@"#F1F1F1"];
  60. [self addSubview:spiltView];
  61. self.bottomY += 1.5;
  62. }
  63. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, self.bottomY, self.fd_width, 50)];
  64. FDAction *action = self.actionArray[i];
  65. [button setTitle:action.title forState:UIControlStateNormal];
  66. [button setTitleColor:self.titleColor ? self.titleColor : [UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
  67. [button setBackgroundColor:self.backColor ? self.backColor : FD_WhiteColor];
  68. [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
  69. button.tag = 100 + i;
  70. [button.titleLabel setFont:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium]];
  71. [self addSubview:button];
  72. self.bottomY += 50;
  73. }
  74. if (@available(iOS 11.0, *)) {
  75. self.bounds = CGRectMake(0, 0, self.fd_width, self.bottomY + FD_Bottom_SafeArea_Height);
  76. } else {
  77. // Fallback on earlier versions
  78. self.bounds = CGRectMake(0, 0, self.fd_width, self.bottomY);
  79. }
  80. [super show:superView type:FDPopTypeBottom];
  81. }
  82. - (NSMutableArray *)actionArray{
  83. if (!_actionArray) {
  84. _actionArray = [NSMutableArray array];
  85. }
  86. return _actionArray;
  87. }
  88. - (UILabel *)titleLabel{
  89. if (!_titleLabel) {
  90. _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.fd_width, 45)];
  91. _titleLabel.textAlignment = NSTextAlignmentCenter;
  92. _titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
  93. _titleLabel.textColor = [UIColor colorWithHexString:@"333333"];
  94. _titleLabel.backgroundColor = FD_WhiteColor;
  95. }
  96. return _titleLabel;
  97. }
  98. - (UILabel *)contentLabel{
  99. if (!_contentLabel) {
  100. _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, _titleLabel.fd_bottom, self.fd_width - 20, 20)];
  101. _contentLabel.textAlignment = NSTextAlignmentCenter;
  102. _contentLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
  103. _contentLabel.numberOfLines = 0;
  104. _contentLabel.textColor = [UIColor colorWithHexString:@"333333"];
  105. _contentLabel.backgroundColor = FD_WhiteColor;
  106. }
  107. return _contentLabel;
  108. }
  109. - (UIView *)bottomLineView{
  110. if (!_bottomLineView) {
  111. _bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.bottomY, self.fd_width, 0.5)];
  112. _bottomLineView.backgroundColor = [UIColor colorWithHexString:@"#CACACA"];
  113. }
  114. return _bottomLineView;
  115. }
  116. @end