MergeTipsAlert.m 2.7 KB

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