// // RABackOrderSubmitAlertController.m // iSales-NPD // // Created by Jack on 2018/1/2. // Copyright © 2018年 United Software Applications, Inc. All rights reserved. // #import "RABackOrderSubmitAlertController.h" #define MARGIN 15 #define V_DISTANCE 15 @interface AlertButton : UIButton @end @implementation AlertButton @end @interface RABackOrderSubmitAlertController () { NSString *_title; NSString *_message; } @property (nonatomic,strong) UILabel *titleLabel; @property (nonatomic,strong) UILabel *messageLabel; @property (nonatomic,strong) AlertButton *confirmBtn; @property (nonatomic,strong) UIView *line; @end @implementation RABackOrderSubmitAlertController - (instancetype)initWithTitle:(NSString *)title Message:(NSString *)msg { if (self = [super init]) { _title = title; _message = msg; self.preferredContentSize = CGSizeMake(300, 200); self.modalPresentationStyle = UIModalPresentationFormSheet; [self initContentUI]; } return self; } - (UILabel *)titleLabel { if (_titleLabel == nil) { _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _titleLabel.font = [UIFont boldSystemFontOfSize:17.0f]; } return _titleLabel; } - (UILabel *)messageLabel { if (_messageLabel == nil) { _messageLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _messageLabel.numberOfLines = 0; } return _messageLabel; } - (AlertButton *)confirmBtn { if (_confirmBtn == nil) { _confirmBtn = [AlertButton buttonWithType:UIButtonTypeSystem]; [_confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside]; _confirmBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17.0f]; [_confirmBtn setTitle:@"Ok" forState:UIControlStateNormal]; } return _confirmBtn; } - (UIView *)line { if (_line == nil) { _line = [UIView new]; _line.backgroundColor = [UIColor lightGrayColor]; } return _line; } - (void)confirmBtnClick:(AlertButton *)sender { [self dismissViewControllerAnimated:YES completion:nil]; } - (void)initContentUI { CGRect bounds = CGRectZero; CGFloat width = CGRectGetWidth(self.view.bounds); CGFloat maxWidth = 0; self.titleLabel.text = _title; [self.titleLabel sizeToFit]; bounds = self.titleLabel.bounds; self.titleLabel.frame = CGRectMake((width - bounds.size.width) * 0.5, 10, bounds.size.width, bounds.size.height); maxWidth = MAX(maxWidth, bounds.size.width); self.messageLabel.attributedText = [self attributedStringWithHtml:_message]; [self.messageLabel sizeToFit]; bounds = self.messageLabel.bounds; self.messageLabel.frame = CGRectMake((width - bounds.size.width) * 0.5, V_DISTANCE + CGRectGetMaxY(self.titleLabel.frame), bounds.size.width, bounds.size.height); maxWidth = MAX(maxWidth, bounds.size.width); [self.confirmBtn sizeToFit]; bounds = self.confirmBtn.bounds; self.confirmBtn.frame = CGRectMake((width - bounds.size.width) * 0.5, V_DISTANCE + CGRectGetMaxY(self.messageLabel.frame), bounds.size.width, bounds.size.height); self.preferredContentSize = CGSizeMake(maxWidth + 2 * MARGIN, CGRectGetMaxY(self.confirmBtn.frame) + 5.0); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.view addSubview:self.titleLabel]; [self.view addSubview:self.messageLabel]; [self.view addSubview:self.line]; [self.view addSubview:self.confirmBtn]; self.view.backgroundColor = [UIColor whiteColor]; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; CGRect bounds = CGRectZero; CGFloat width = CGRectGetWidth(self.view.bounds); bounds = self.titleLabel.bounds; self.titleLabel.frame = CGRectMake((width - bounds.size.width) * 0.5, 10, bounds.size.width, bounds.size.height); bounds = self.messageLabel.bounds; self.messageLabel.frame = CGRectMake((width - bounds.size.width) * 0.5, V_DISTANCE + CGRectGetMaxY(self.titleLabel.frame), bounds.size.width, bounds.size.height); _line.frame = CGRectMake(0, CGRectGetMaxY(self.messageLabel.frame) + V_DISTANCE - 0.5, width, 0.5); bounds = self.confirmBtn.bounds; self.confirmBtn.frame = CGRectMake((width - self.messageLabel.bounds.size.width) * 0.5, V_DISTANCE + CGRectGetMaxY(self.messageLabel.frame), self.messageLabel.bounds.size.width, bounds.size.height); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSAttributedString *)attributedStringWithHtml:(NSString *)html { NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}; NSMutableAttributedString *attrString=[[NSMutableAttributedString alloc] initWithData:[html dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES] options:options documentAttributes:nil error:nil]; return attrString; } @end