MergeTipsAlert.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // MergeTipsAlert.m
  3. // KulexiuSchoolStudent
  4. //
  5. // Created by 王智 on 2024/7/15.
  6. //
  7. #import "MergeTipsAlert.h"
  8. @interface MergeTipsAlert ()
  9. @property (weak, nonatomic) IBOutlet UIView *colorView;
  10. @property (weak, nonatomic) IBOutlet UILabel *tipsLabel;
  11. @property (weak, nonatomic) IBOutlet UIButton *leftButton;
  12. @property (weak, nonatomic) IBOutlet UIButton *rightButton;
  13. @property (nonatomic, copy) MergeTipsCallback cancelCallback;
  14. @property (nonatomic, copy) MergeTipsCallback sureCallback;
  15. @end
  16. @implementation MergeTipsAlert
  17. - (void)awakeFromNib {
  18. [super awakeFromNib];
  19. }
  20. + (instancetype)shareInstance {
  21. MergeTipsAlert *view = [[[NSBundle mainBundle] loadNibNamed:@"MergeTipsAlert" owner:nil options:nil] firstObject];
  22. return view;
  23. }
  24. - (void)configWithDesc:(NSString *)desc leftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle {
  25. if ([NSString isEmptyString:desc]) {
  26. desc = @"";
  27. }
  28. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  29. [paragraphStyle setLineSpacing:4];//调整行间距
  30. paragraphStyle.alignment = NSTextAlignmentCenter;
  31. paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
  32. NSMutableAttributedString *tipsAttrs = [[NSMutableAttributedString alloc] initWithString:desc attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f weight:UIFontWeightRegular], NSForegroundColorAttributeName:HexRGB(0x777777),NSParagraphStyleAttributeName:paragraphStyle}];
  33. self.tipsLabel.attributedText = tipsAttrs;
  34. if (![NSString isEmptyString:leftTitle]) {
  35. [self.leftButton setTitle:leftTitle forState:UIControlStateNormal];
  36. }
  37. if (![NSString isEmptyString:rightTitle]) {
  38. [self.rightButton setTitle:rightTitle forState:UIControlStateNormal];
  39. }
  40. [self layoutIfNeeded];
  41. }
  42. - (void)actionCallbackCancel:(MergeTipsCallback)cancel sure:(MergeTipsCallback)sure {
  43. if (cancel) {
  44. self.cancelCallback = cancel;
  45. }
  46. if (sure) {
  47. self.sureCallback = sure;
  48. }
  49. }
  50. - (void)layoutSubviews {
  51. [super layoutSubviews];
  52. }
  53. - (void)showAlert {
  54. UIView *displayView = [NSObject getKeyWindow];
  55. [displayView addSubview:self];
  56. [self mas_makeConstraints:^(MASConstraintMaker *make) {
  57. make.left.right.top.bottom.mas_equalTo(displayView);
  58. }];
  59. }
  60. - (IBAction)cancelAction:(id)sender {
  61. [self removeFromSuperview];
  62. if (self.cancelCallback) {
  63. self.cancelCallback();
  64. }
  65. }
  66. - (IBAction)sureAction:(id)sender {
  67. [self removeFromSuperview];
  68. if (self.sureCallback) {
  69. self.sureCallback();
  70. }
  71. }
  72. /*
  73. // Only override drawRect: if you perform custom drawing.
  74. // An empty implementation adversely affects performance during animation.
  75. - (void)drawRect:(CGRect)rect {
  76. // Drawing code
  77. }
  78. */
  79. @end