RABackOrderSubmitAlertController.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // RABackOrderSubmitAlertController.m
  3. // iSales-NPD
  4. //
  5. // Created by Jack on 2018/1/2.
  6. // Copyright © 2018年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RABackOrderSubmitAlertController.h"
  9. #define MARGIN 15
  10. #define V_DISTANCE 15
  11. @interface AlertButton : UIButton
  12. @end
  13. @implementation AlertButton
  14. @end
  15. @interface RABackOrderSubmitAlertController ()
  16. {
  17. NSString *_title;
  18. NSString *_message;
  19. }
  20. @property (nonatomic,strong) UILabel *titleLabel;
  21. @property (nonatomic,strong) UILabel *messageLabel;
  22. @property (nonatomic,strong) AlertButton *confirmBtn;
  23. @property (nonatomic,strong) UIView *line;
  24. @end
  25. @implementation RABackOrderSubmitAlertController
  26. - (instancetype)initWithTitle:(NSString *)title Message:(NSString *)msg {
  27. if (self = [super init]) {
  28. _title = title;
  29. _message = msg;
  30. self.preferredContentSize = CGSizeMake(300, 200);
  31. self.modalPresentationStyle = UIModalPresentationFormSheet;
  32. [self initContentUI];
  33. }
  34. return self;
  35. }
  36. - (UILabel *)titleLabel {
  37. if (_titleLabel == nil) {
  38. _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  39. _titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
  40. }
  41. return _titleLabel;
  42. }
  43. - (UILabel *)messageLabel {
  44. if (_messageLabel == nil) {
  45. _messageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  46. _messageLabel.numberOfLines = 0;
  47. }
  48. return _messageLabel;
  49. }
  50. - (AlertButton *)confirmBtn {
  51. if (_confirmBtn == nil) {
  52. _confirmBtn = [AlertButton buttonWithType:UIButtonTypeSystem];
  53. [_confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  54. _confirmBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
  55. [_confirmBtn setTitle:@"Ok" forState:UIControlStateNormal];
  56. }
  57. return _confirmBtn;
  58. }
  59. - (UIView *)line {
  60. if (_line == nil) {
  61. _line = [UIView new];
  62. _line.backgroundColor = [UIColor lightGrayColor];
  63. }
  64. return _line;
  65. }
  66. - (void)confirmBtnClick:(AlertButton *)sender {
  67. [self dismissViewControllerAnimated:YES completion:nil];
  68. }
  69. - (void)initContentUI {
  70. CGRect bounds = CGRectZero;
  71. CGFloat width = CGRectGetWidth(self.view.bounds);
  72. CGFloat maxWidth = 0;
  73. self.titleLabel.text = _title;
  74. [self.titleLabel sizeToFit];
  75. bounds = self.titleLabel.bounds;
  76. self.titleLabel.frame = CGRectMake((width - bounds.size.width) * 0.5, 10, bounds.size.width, bounds.size.height);
  77. maxWidth = MAX(maxWidth, bounds.size.width);
  78. self.messageLabel.attributedText = [self attributedStringWithHtml:_message];
  79. [self.messageLabel sizeToFit];
  80. bounds = self.messageLabel.bounds;
  81. self.messageLabel.frame = CGRectMake((width - bounds.size.width) * 0.5, V_DISTANCE + CGRectGetMaxY(self.titleLabel.frame), bounds.size.width, bounds.size.height);
  82. maxWidth = MAX(maxWidth, bounds.size.width);
  83. [self.confirmBtn sizeToFit];
  84. bounds = self.confirmBtn.bounds;
  85. self.confirmBtn.frame = CGRectMake((width - bounds.size.width) * 0.5, V_DISTANCE + CGRectGetMaxY(self.messageLabel.frame), bounds.size.width, bounds.size.height);
  86. self.preferredContentSize = CGSizeMake(maxWidth + 2 * MARGIN, CGRectGetMaxY(self.confirmBtn.frame) + 5.0);
  87. }
  88. - (void)viewDidLoad {
  89. [super viewDidLoad];
  90. // Do any additional setup after loading the view.
  91. [self.view addSubview:self.titleLabel];
  92. [self.view addSubview:self.messageLabel];
  93. [self.view addSubview:self.line];
  94. [self.view addSubview:self.confirmBtn];
  95. self.view.backgroundColor = [UIColor whiteColor];
  96. }
  97. - (void)viewDidLayoutSubviews {
  98. [super viewDidLayoutSubviews];
  99. CGRect bounds = CGRectZero;
  100. CGFloat width = CGRectGetWidth(self.view.bounds);
  101. bounds = self.titleLabel.bounds;
  102. self.titleLabel.frame = CGRectMake((width - bounds.size.width) * 0.5, 10, bounds.size.width, bounds.size.height);
  103. bounds = self.messageLabel.bounds;
  104. self.messageLabel.frame = CGRectMake((width - bounds.size.width) * 0.5, V_DISTANCE + CGRectGetMaxY(self.titleLabel.frame), bounds.size.width, bounds.size.height);
  105. _line.frame = CGRectMake(0, CGRectGetMaxY(self.messageLabel.frame) + V_DISTANCE - 0.5, width, 0.5);
  106. bounds = self.confirmBtn.bounds;
  107. self.confirmBtn.frame = CGRectMake((width - bounds.size.width) * 0.5, V_DISTANCE + CGRectGetMaxY(self.messageLabel.frame), bounds.size.width, bounds.size.height);
  108. }
  109. - (void)didReceiveMemoryWarning {
  110. [super didReceiveMemoryWarning];
  111. // Dispose of any resources that can be recreated.
  112. }
  113. - (NSAttributedString *)attributedStringWithHtml:(NSString *)html
  114. {
  115. NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
  116. NSMutableAttributedString *attrString=[[NSMutableAttributedString alloc] initWithData:[html dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES] options:options documentAttributes:nil error:nil];
  117. return attrString;
  118. }
  119. @end