MessageAlert.m.svn-base 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // MessageAlert.m
  3. // XFilesPro
  4. //
  5. // Created by 青秀斌 on 14-8-15.
  6. // Copyright (c) 2014年 深圳元度科技有限公司. All rights reserved.
  7. //
  8. #if ! __has_feature(objc_arc)
  9. #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  10. #endif
  11. #import "MessageAlert.h"
  12. @interface MessageAlert ()<UITextFieldDelegate>
  13. @property (nonatomic, strong) UIScrollView *messageView;
  14. @property (nonatomic, strong) NSMutableArray *textFields;
  15. @property (nonatomic, strong) NSMutableArray *buttons;
  16. @property (nonatomic, strong) NSMutableArray *actions;
  17. @end
  18. @implementation MessageAlert
  19. @dynamic setTitle;
  20. @dynamic setMessage;
  21. @dynamic addTextField;
  22. @dynamic addBlueButton;
  23. @dynamic addRedButton;
  24. @dynamic showAlert;
  25. @dynamic dismissAlert;
  26. @dynamic addTextFieldTwo;
  27. /**********************************************************************/
  28. #pragma mark - OverWrite Methods
  29. /**********************************************************************/
  30. - (void)awakeFromNib{
  31. self.textFields = [NSMutableArray array];
  32. self.buttons = [NSMutableArray array];
  33. self.actions = [NSMutableArray array];
  34. [super awakeFromNib];
  35. }
  36. - (void)layoutSubviews{
  37. CGFloat localtionY = 20;
  38. if (self.titleLabel != nil) {
  39. CGRect frame = self.titleLabel.frame;
  40. frame.origin.y = localtionY;
  41. [self.titleLabel setFrame:frame];
  42. localtionY += frame.size.height + 10;
  43. }
  44. if (self.messageView != nil) {
  45. CGRect frame = self.messageView.frame;
  46. frame.origin.y = localtionY;
  47. [self.messageView setFrame:frame];
  48. localtionY += frame.size.height + 15;
  49. }
  50. if (self.textFields.count > 0) {
  51. CGRect frame = CGRectInset(self.bounds, 20, 20);frame.size.height = 30;
  52. for (UITextField *textField in self.textFields) {
  53. frame.origin.y = localtionY;
  54. [textField setFrame:frame];
  55. localtionY += frame.size.height + 5;
  56. }
  57. localtionY += 10;
  58. }
  59. if (self.buttons.count > 0) {
  60. CGRect frame = CGRectInset(self.bounds, 20, 20);frame.size.height = 35;
  61. if (self.buttons.count == 2) {
  62. frame.origin.y = localtionY;
  63. frame.size.width = (frame.size.width-8)/2;
  64. UIButton *button = [self.buttons objectAtIndex:0];
  65. [button setFrame:frame];
  66. button = [self.buttons objectAtIndex:1];
  67. frame.origin.x = frame.origin.x + frame.size.width + 8;
  68. [button setFrame:frame];
  69. localtionY += frame.size.height + 20;
  70. } else {
  71. for (UIButton *button in self.buttons) {
  72. frame.origin.y = localtionY;
  73. [button setFrame:frame];
  74. localtionY += frame.size.height + 5;
  75. }
  76. localtionY += 15;
  77. }
  78. }
  79. CGRect alertFrame = self.frame;
  80. alertFrame.size.height = localtionY;
  81. [self setFrame:alertFrame];
  82. [self setCenter:[self centerWithAnimation:kAlertAnimationFade]];
  83. }
  84. /**********************************************************************/
  85. #pragma mark - Private Methods
  86. /**********************************************************************/
  87. - (AlertBlockF)setTitle{
  88. return ^MessageAlert *(NSString *format,...) {
  89. if (format) {
  90. va_list args;
  91. va_start(args, format);
  92. NSString *title = [[NSString alloc] initWithFormat:format arguments:args];
  93. UIFont *font = [UIFont boldSystemFontOfSize:17];
  94. CGRect frame = CGRectInset(self.bounds, 20, 20);
  95. CGSize size = [title sizeWithFont:font byWidth:frame.size.width];
  96. frame.size.height = size.height<20?20:size.height;
  97. if (_titleLabel == nil) {
  98. _titleLabel = [[UILabel alloc] init];
  99. _titleLabel.numberOfLines = 99;
  100. _titleLabel.font = font;
  101. _titleLabel.textColor = [UIColor blackColor];
  102. _titleLabel.textAlignment = NSTextAlignmentCenter;
  103. _titleLabel.backgroundColor = [UIColor clearColor];
  104. }
  105. _titleLabel.text = title;
  106. _titleLabel.frame = frame;
  107. [self addSubview:_titleLabel];
  108. } else {
  109. [_titleLabel removeFromSuperview];
  110. _titleLabel = nil;
  111. }
  112. return self;
  113. };
  114. }
  115. - (AlertBlockF)setMessage{
  116. return ^MessageAlert *(NSString *format,...){
  117. if (format) {
  118. va_list args;
  119. va_start(args, format);
  120. NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
  121. UIFont *font = [UIFont systemFontOfSize:15];
  122. CGRect frame = CGRectInset(self.bounds, 20, 20);
  123. CGSize size = [message sizeWithFont:font byWidth:frame.size.width];
  124. frame.size.height = MAX(size.height, 20);
  125. if (_messageView == nil) {
  126. _messageView = [[UIScrollView alloc] init];
  127. [_messageView addSubview:_messageLabel];
  128. }
  129. _messageView.contentSize = CGSizeMake(frame.size.width, frame.size.height);
  130. _messageView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, MIN(frame.size.height, 120));
  131. if (_messageLabel == nil) {
  132. _messageLabel = [[UILabel alloc] init];
  133. _messageLabel.numberOfLines = 99;
  134. _messageLabel.font = font;
  135. _messageLabel.textColor = [UIColor blackColor];
  136. _messageLabel.textAlignment = NSTextAlignmentCenter;
  137. _messageLabel.backgroundColor = [UIColor clearColor];
  138. }
  139. _messageLabel.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
  140. _messageLabel.text = message;
  141. [_messageView addSubview:_messageLabel];
  142. [self addSubview:_messageView];
  143. va_end(args);
  144. } else {
  145. [_messageLabel removeFromSuperview];
  146. _messageLabel = nil;
  147. [_messageView removeFromSuperview];
  148. _messageView = nil;
  149. }
  150. return self;
  151. };
  152. }
  153. - (AlertBlockT)addTextField{
  154. return ^MessageAlert *(NSString *placeholder, NSString *text){
  155. CGRect frame = CGRectInset(self.bounds, 20, 20);
  156. UITextField *textField = [[UITextField alloc] initWithFrame:frame];
  157. textField.backgroundColor = RGB(237, 237, 237);
  158. textField.font = [UIFont systemFontOfSize:15];
  159. textField.rightViewMode=UITextFieldViewModeAlways;
  160. textField.textAlignment = NSTextAlignmentLeft;
  161. textField.borderStyle = UITextBorderStyleNone;
  162. textField.placeholder = placeholder;
  163. textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  164. textField.text = text;
  165. textField.delegate = self;
  166. [self addSubview:textField];
  167. [self.textFields addObject:textField];
  168. return self;
  169. };
  170. }
  171. - (AlertBlockH)addTextFieldTwo{
  172. return ^MessageAlert *(NSString *placeholder, NSString *text, NSString *unit){
  173. CGRect frame = CGRectInset(self.bounds, 20, 20);
  174. UITextField *textField = [[UITextField alloc] initWithFrame:frame];
  175. textField.backgroundColor = RGB(237, 237, 237);
  176. textField.font = [UIFont systemFontOfSize:15];
  177. textField.rightViewMode=UITextFieldViewModeAlways;
  178. textField.textAlignment = NSTextAlignmentLeft;
  179. textField.borderStyle = UITextBorderStyleNone;
  180. textField.placeholder = placeholder;
  181. textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  182. textField.text = text;
  183. textField.delegate = self;
  184. UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
  185. [rightLabel setTextColor:[UIColor darkGrayColor]];
  186. [rightLabel setBackgroundColor:textField.backgroundColor];
  187. [rightLabel setText:unit];
  188. [textField setRightView:rightLabel];
  189. [self addSubview:textField];
  190. [self.textFields addObject:textField];
  191. return self;
  192. };
  193. }
  194. - (AlertBlockB)addRedButton{
  195. return ^MessageAlert *(NSString *title, void (^action)(MessageAlert *)) {
  196. CGRect frame = CGRectInset(self.bounds, 20, 20);
  197. UIButton *button = [[UIButton alloc] initWithFrame:frame];
  198. button.backgroundColor = [UIColor clearColor];
  199. button.titleLabel.textColor = [UIColor whiteColor];
  200. button.titleLabel.font = [UIFont boldSystemFontOfSize:15];
  201. [button setTitle:title forState:UIControlStateNormal];
  202. [button setBackgroundImage:[UIImage rButtonNormalImage] forState:UIControlStateNormal];
  203. [button setBackgroundImage:[UIImage rButtonHighlightImage] forState:UIControlStateHighlighted];
  204. [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  205. [self addSubview:button];
  206. [self.buttons addObject:button];
  207. if (action != nil) {
  208. [self.actions addObject:action];
  209. } else {
  210. [self.actions addObject:^(MessageAlert *alert){
  211. [alert dismiss];
  212. }];
  213. }
  214. return self;
  215. };
  216. }
  217. - (AlertBlockB)addBlueButton{
  218. return ^MessageAlert *(NSString *title, void (^action)(MessageAlert *)) {
  219. CGRect frame = CGRectInset(self.bounds, 20, 20);
  220. UIButton *button = [[UIButton alloc] initWithFrame:frame];
  221. button.backgroundColor = [UIColor clearColor];
  222. button.titleLabel.textColor = [UIColor whiteColor];
  223. button.titleLabel.font = [UIFont boldSystemFontOfSize:15];
  224. [button setTitle:title forState:UIControlStateNormal];
  225. [button setBackgroundImage:[UIImage bButtonNormalImage] forState:UIControlStateNormal];
  226. [button setBackgroundImage:[UIImage bButtonHighlightImage] forState:UIControlStateHighlighted];
  227. [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  228. [self addSubview:button];
  229. [self.buttons addObject:button];
  230. if (action != nil) {
  231. [self.actions addObject:action];
  232. } else {
  233. [self.actions addObject:^(MessageAlert *alert){
  234. [alert dismiss];
  235. }];
  236. }
  237. return self;
  238. };
  239. }
  240. - (AlertBlockA)showAlert{
  241. return ^(AlertAnimation animation){
  242. [self showWithAnimation:animation];
  243. return self;
  244. };
  245. }
  246. - (AlertBlockA)dismissAlert{
  247. return ^(AlertAnimation animation){
  248. [self dismissWithAnimation:animation];
  249. return self;
  250. };
  251. }
  252. /**********************************************************************/
  253. #pragma mark - Public Methods
  254. /**********************************************************************/
  255. + (instancetype)alert{
  256. return [[MessageAlert alloc] initWithFrame:CGRectMake(0, 0, 270, 75)];
  257. }
  258. - (UITextField *)textFieldAtIndex:(NSUInteger)index{
  259. if (index<self.textFields.count) {
  260. return [self.textFields objectAtIndex:index];
  261. }
  262. return nil;
  263. }
  264. - (UIButton *)buttonAtIndex:(NSUInteger)index{
  265. if (index<self.buttons.count) {
  266. return [self.buttons objectAtIndex:index];
  267. }
  268. return nil;
  269. }
  270. /**********************************************************************/
  271. #pragma mark - UIButton Action
  272. /**********************************************************************/
  273. - (void)buttonAction:(UIButton *)button {
  274. NSInteger index = [self.buttons indexOfObject:button];
  275. void (^action)(MessageAlert *) = [self.actions objectAtIndex:index];
  276. action(self);
  277. }
  278. /**********************************************************************/
  279. #pragma mark - UITextFieldDelegate
  280. /**********************************************************************/
  281. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  282. if ([string isEqualToString:@"\n"]) {
  283. [textField resignFirstResponder];
  284. return YES;
  285. }
  286. if ([string isEqualToString:@""]) {
  287. return YES;
  288. }
  289. return YES;
  290. }
  291. @end