口袋优选

TYAlertView.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //
  2. // TYAlertView.m
  3. // TYAlertControllerDemo
  4. //
  5. // Created by tanyang on 15/9/7.
  6. // Copyright (c) 2015年 tanyang. All rights reserved.
  7. //
  8. #import "TYAlertView.h"
  9. #import "UIView+TYAlertView.h"
  10. #import "UIView+TYAutoLayout.h"
  11. @interface TYAlertAction ()
  12. @property (nonatomic, strong) NSString *title;
  13. @property (nonatomic, assign) TYAlertActionStyle style;
  14. @property (nonatomic, copy) void (^handler)(TYAlertAction *);
  15. @end
  16. @implementation TYAlertAction
  17. + (instancetype)actionWithTitle:(NSString *)title style:(TYAlertActionStyle)style handler:(void (^)(TYAlertAction *))handler
  18. {
  19. return [[self alloc]initWithTitle:title style:style handler:handler];
  20. }
  21. - (instancetype)initWithTitle:(NSString *)title style:(TYAlertActionStyle)style handler:(void (^)(TYAlertAction *))handler
  22. {
  23. if (self = [super init]) {
  24. _title = title;
  25. _style = style;
  26. _handler = handler;
  27. _enabled = YES;
  28. }
  29. return self;
  30. }
  31. - (id)copyWithZone:(NSZone *)zone
  32. {
  33. TYAlertAction *action = [[self class]allocWithZone:zone];
  34. action.title = self.title;
  35. action.style = self.style;
  36. return action;
  37. }
  38. @end
  39. @interface TYAlertView ()
  40. // text content View
  41. @property (nonatomic, weak) UIView *textContentView;
  42. @property (nonatomic, weak) UILabel *titleLable;
  43. @property (nonatomic, weak) UILabel *messageLabel;
  44. @property (nonatomic, weak) UIView *textFieldContentView;
  45. @property (nonatomic, weak) NSLayoutConstraint *textFieldTopConstraint;
  46. @property (nonatomic, strong) NSMutableArray *textFields;
  47. @property (nonatomic, strong) NSMutableArray *textFieldSeparateViews;
  48. // button content View
  49. @property (nonatomic, weak) UIView *buttonContentView;
  50. @property (nonatomic, weak) NSLayoutConstraint *buttonTopConstraint;
  51. @property (nonatomic, strong) NSMutableArray *buttons;
  52. @property (nonatomic, strong) NSMutableArray *actions;
  53. @end
  54. #define kAlertViewWidth 280
  55. #define kContentViewEdge 15
  56. #define kContentViewSpace 15
  57. #define kTextLabelSpace 6
  58. #define kButtonTagOffset 1000
  59. #define kButtonSpace 6
  60. #define KButtonHeight 44
  61. #define kTextFieldOffset 10000
  62. #define kTextFieldHeight 29
  63. #define kTextFieldEdge 8
  64. #define KTextFieldBorderWidth 0.5
  65. @implementation TYAlertView
  66. #pragma mark - init
  67. - (instancetype)initWithFrame:(CGRect)frame
  68. {
  69. if (self = [super initWithFrame:frame]) {
  70. [self configureProperty];
  71. [self addContentViews];
  72. [self addTextLabels];
  73. }
  74. return self;
  75. }
  76. - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message
  77. {
  78. if (self = [self init]) {
  79. _titleLable.text = title;
  80. _messageLabel.text = message;
  81. }
  82. return self;
  83. }
  84. + (instancetype)alertViewWithTitle:(NSString *)title message:(NSString *)message
  85. {
  86. return [[self alloc]initWithTitle:title message:message];
  87. }
  88. #pragma mark - configure
  89. - (void)configureProperty
  90. {
  91. _clickedAutoHide = YES;
  92. self.backgroundColor = [UIColor whiteColor];
  93. _alertViewWidth = kAlertViewWidth;
  94. _contentViewSpace = kContentViewSpace;
  95. _textLabelSpace = kTextLabelSpace;
  96. _textLabelContentViewEdge = kContentViewEdge;
  97. _buttonHeight = KButtonHeight;
  98. _buttonSpace = kButtonSpace;
  99. _buttonContentViewEdge = kContentViewEdge;
  100. _buttonContentViewTop = kContentViewSpace;
  101. _buttonCornerRadius = 4.0;
  102. _buttonFont = [UIFont fontWithName:@"HelveticaNeue" size:18];
  103. _buttonDefaultBgColor = [UIColor colorWithRed:52/255.0 green:152/255.0 blue:219/255.0 alpha:1];
  104. _buttonCancelBgColor = [UIColor colorWithRed:127/255.0 green:140/255.0 blue:141/255.0 alpha:1];
  105. _buttonDestructiveBgColor = [UIColor colorWithRed:231/255.0 green:76/255.0 blue:60/255.0 alpha:1];
  106. _textFieldHeight = kTextFieldHeight;
  107. _textFieldEdge = kTextFieldEdge;
  108. _textFieldBorderWidth = KTextFieldBorderWidth;
  109. _textFieldContentViewEdge = kContentViewEdge;
  110. _textFieldBorderColor = [UIColor colorWithRed:203/255.0 green:203/255.0 blue:203/255.0 alpha:1];
  111. _textFieldBackgroudColor = [UIColor whiteColor];
  112. _textFieldFont = [UIFont systemFontOfSize:14];
  113. _buttons = [NSMutableArray array];
  114. _actions = [NSMutableArray array];
  115. }
  116. - (UIColor *)buttonBgColorWithStyle:(TYAlertActionStyle)style
  117. {
  118. switch (style) {
  119. case TYAlertActionStyleDefault:
  120. return _buttonDefaultBgColor;
  121. case TYAlertActionStyleCancel:
  122. return _buttonCancelBgColor;
  123. case TYAlertActionStyleDestructive:
  124. return _buttonDestructiveBgColor;
  125. default:
  126. return nil;
  127. }
  128. }
  129. #pragma mark - add contentview
  130. - (void)addContentViews
  131. {
  132. UIView *textContentView = [[UIView alloc]init];
  133. [self addSubview:textContentView];
  134. _textContentView = textContentView;
  135. UIView *textFieldContentView = [[UIView alloc]init];
  136. [self addSubview:textFieldContentView];
  137. _textFieldContentView = textFieldContentView;
  138. UIView *buttonContentView = [[UIView alloc]init];
  139. buttonContentView.userInteractionEnabled = YES;
  140. [self addSubview:buttonContentView];
  141. _buttonContentView = buttonContentView;
  142. }
  143. - (void)addTextLabels
  144. {
  145. UILabel *titleLabel = [[UILabel alloc]init];
  146. titleLabel.textAlignment = NSTextAlignmentCenter;
  147. titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
  148. titleLabel.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
  149. [_textContentView addSubview:titleLabel];
  150. _titleLable = titleLabel;
  151. UILabel *messageLabel = [[UILabel alloc]init];
  152. messageLabel.numberOfLines = 0;
  153. messageLabel.textAlignment = NSTextAlignmentCenter;
  154. messageLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
  155. messageLabel.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
  156. [_textContentView addSubview:messageLabel];
  157. _messageLabel = messageLabel;
  158. }
  159. - (void)didMoveToSuperview
  160. {
  161. if (self.superview) {
  162. [self layoutContentViews];
  163. [self layoutTextLabels];
  164. }
  165. }
  166. - (void)addAction:(TYAlertAction *)action
  167. {
  168. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  169. button.clipsToBounds = YES;
  170. button.layer.cornerRadius = _buttonCornerRadius;
  171. [button setTitle:action.title forState:UIControlStateNormal];
  172. button.titleLabel.font = _buttonFont;
  173. button.backgroundColor = [self buttonBgColorWithStyle:action.style];
  174. button.enabled = action.enabled;
  175. button.tag = kButtonTagOffset + _buttons.count;
  176. button.translatesAutoresizingMaskIntoConstraints = NO;
  177. [button addTarget:self action:@selector(actionButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  178. [_buttonContentView addSubview:button];
  179. [_buttons addObject:button];
  180. [_actions addObject:action];
  181. if (_buttons.count == 1) {
  182. [self layoutContentViews];
  183. [self layoutTextLabels];
  184. }
  185. [self layoutButtons];
  186. }
  187. - (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler
  188. {
  189. if (_textFields == nil) {
  190. _textFields = [NSMutableArray array];
  191. }
  192. UITextField *textField = [[UITextField alloc]init];
  193. textField.tag = kTextFieldOffset + _textFields.count;
  194. textField.font = _textFieldFont;
  195. textField.translatesAutoresizingMaskIntoConstraints = NO;
  196. if (configurationHandler) {
  197. configurationHandler(textField);
  198. }
  199. [_textFieldContentView addSubview:textField];
  200. [_textFields addObject:textField];
  201. if (_textFields.count > 1) {
  202. if (_textFieldSeparateViews == nil) {
  203. _textFieldSeparateViews = [NSMutableArray array];
  204. }
  205. UIView *separateView = [[UIView alloc]init];
  206. separateView.backgroundColor = _textFieldBorderColor;
  207. separateView.translatesAutoresizingMaskIntoConstraints = NO;
  208. [_textFieldContentView addSubview:separateView];
  209. [_textFieldSeparateViews addObject:separateView];
  210. }
  211. [self layoutTextFields];
  212. }
  213. - (NSArray *)textFieldArray
  214. {
  215. return _textFields;
  216. }
  217. #pragma mark - layout contenview
  218. - (void)layoutContentViews
  219. {
  220. if (!_textContentView.translatesAutoresizingMaskIntoConstraints) {
  221. // layout done
  222. return;
  223. }
  224. if (_alertViewWidth) {
  225. [self addConstraintWidth:_alertViewWidth height:0];
  226. }
  227. // textContentView
  228. _textContentView.translatesAutoresizingMaskIntoConstraints = NO;
  229. [self addConstraintWithView:_textContentView topView:self leftView:self bottomView:nil rightView:self edgeInset:UIEdgeInsetsMake(_contentViewSpace, _textLabelContentViewEdge, 0, -_textLabelContentViewEdge)];
  230. // textFieldContentView
  231. _textFieldContentView.translatesAutoresizingMaskIntoConstraints = NO;
  232. _textFieldTopConstraint = [self addConstraintWithTopView:_textContentView toBottomView:_textFieldContentView constant:0];
  233. [self addConstraintWithView:_textFieldContentView topView:nil leftView:self bottomView:nil rightView:self edgeInset:UIEdgeInsetsMake(0, _textFieldContentViewEdge, 0, -_textFieldContentViewEdge)];
  234. // buttonContentView
  235. _buttonContentView.translatesAutoresizingMaskIntoConstraints = NO;
  236. _buttonTopConstraint = [self addConstraintWithTopView:_textFieldContentView toBottomView:_buttonContentView constant:_buttonContentViewTop];
  237. [self addConstraintWithView:_buttonContentView topView:nil leftView:self bottomView:self rightView:self edgeInset:UIEdgeInsetsMake(0, _buttonContentViewEdge, -_contentViewSpace, -_buttonContentViewEdge)];
  238. }
  239. - (void)layoutTextLabels
  240. {
  241. if (!_titleLable.translatesAutoresizingMaskIntoConstraints && !_messageLabel.translatesAutoresizingMaskIntoConstraints) {
  242. // layout done
  243. return;
  244. }
  245. // title
  246. _titleLable.translatesAutoresizingMaskIntoConstraints = NO;
  247. [_textContentView addConstraintWithView:_titleLable topView:_textContentView leftView:_textContentView bottomView:nil rightView:_textContentView edgeInset:UIEdgeInsetsZero];
  248. // message
  249. _messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
  250. [_textContentView addConstraintWithTopView:_titleLable toBottomView:_messageLabel constant:_textLabelSpace];
  251. [_textContentView addConstraintWithView:_messageLabel topView:nil leftView:_textContentView bottomView:_textContentView rightView:_textContentView edgeInset:UIEdgeInsetsZero];
  252. }
  253. - (void)layoutButtons
  254. {
  255. UIButton *button = _buttons.lastObject;
  256. if (_buttons.count == 1) {
  257. _buttonTopConstraint.constant = -_buttonContentViewTop;
  258. [_buttonContentView addConstraintToView:button edgeInset:UIEdgeInsetsZero];
  259. [button addConstraintWidth:0 height:_buttonHeight];
  260. }else if (_buttons.count == 2) {
  261. UIButton *firstButton = _buttons.firstObject;
  262. [_buttonContentView removeConstraintWithView:firstButton attribute:NSLayoutAttributeRight];
  263. [_buttonContentView addConstraintWithView:button topView:_buttonContentView leftView:nil bottomView:nil rightView:_buttonContentView edgeInset:UIEdgeInsetsZero];
  264. [_buttonContentView addConstraintWithLeftView:firstButton toRightView:button constant:_buttonSpace];
  265. [_buttonContentView addConstraintEqualWithView:button widthToView:firstButton heightToView:firstButton];
  266. }else {
  267. if (_buttons.count == 3) {
  268. UIButton *firstBtn = _buttons[0];
  269. UIButton *secondBtn = _buttons[1];
  270. [_buttonContentView removeConstraintWithView:firstBtn attribute:NSLayoutAttributeRight];
  271. [_buttonContentView removeConstraintWithView:firstBtn attribute:NSLayoutAttributeBottom];
  272. [_buttonContentView removeConstraintWithView:secondBtn attribute:NSLayoutAttributeTop];
  273. [_buttonContentView addConstraintWithView:firstBtn topView:nil leftView:nil bottomView:nil rightView:_buttonContentView edgeInset:UIEdgeInsetsZero];
  274. [_buttonContentView addConstraintWithTopView:firstBtn toBottomView:secondBtn constant:_buttonSpace];
  275. }
  276. UIButton *lastSecondBtn = _buttons[_buttons.count - 2];
  277. [_buttonContentView removeConstraintWithView:lastSecondBtn attribute:NSLayoutAttributeBottom];
  278. [_buttonContentView addConstraintWithTopView:lastSecondBtn toBottomView:button constant:_buttonSpace];
  279. [_buttonContentView addConstraintWithView:button topView:nil leftView:_buttonContentView bottomView:_buttonContentView rightView:_buttonContentView edgeInset:UIEdgeInsetsZero];
  280. [_buttonContentView addConstraintEqualWithView:button widthToView:nil heightToView:lastSecondBtn];
  281. }
  282. }
  283. - (void)layoutTextFields
  284. {
  285. UITextField *textField = _textFields.lastObject;
  286. if (_textFields.count == 1) {
  287. // setup textFieldContentView
  288. _textFieldContentView.backgroundColor = _textFieldBackgroudColor;
  289. _textFieldContentView.layer.masksToBounds = YES;
  290. _textFieldContentView.layer.cornerRadius = 4;
  291. _textFieldContentView.layer.borderWidth = _textFieldBorderWidth;
  292. _textFieldContentView.layer.borderColor = _textFieldBorderColor.CGColor;
  293. _textFieldTopConstraint.constant = -_contentViewSpace;
  294. [_textFieldContentView addConstraintToView:textField edgeInset:UIEdgeInsetsMake(_textFieldBorderWidth, _textFieldEdge, -_textFieldBorderWidth, -_textFieldEdge)];
  295. [textField addConstraintWidth:0 height:_textFieldHeight];
  296. }else {
  297. // textField
  298. UITextField *lastSecondTextField = _textFields[_textFields.count - 2];
  299. [_textFieldContentView removeConstraintWithView:lastSecondTextField attribute:NSLayoutAttributeBottom];
  300. [_textFieldContentView addConstraintWithTopView:lastSecondTextField toBottomView:textField constant:_textFieldBorderWidth];
  301. [_textFieldContentView addConstraintWithView:textField topView:nil leftView:_textFieldContentView bottomView:_textFieldContentView rightView:_textFieldContentView edgeInset:UIEdgeInsetsMake(0, _textFieldEdge, -_textFieldBorderWidth, -_textFieldEdge)];
  302. [_textFieldContentView addConstraintEqualWithView:textField widthToView:nil heightToView:lastSecondTextField];
  303. // separateview
  304. UIView *separateView = _textFieldSeparateViews[_textFields.count - 2];
  305. [_textFieldContentView addConstraintWithView:separateView topView:nil leftView:_textFieldContentView bottomView:nil rightView:_textFieldContentView edgeInset:UIEdgeInsetsZero];
  306. [_textFieldContentView addConstraintWithTopView:separateView toBottomView:textField constant:0];
  307. [separateView addConstraintWidth:0 height:_textFieldBorderWidth];
  308. }
  309. }
  310. #pragma mark - action
  311. - (void)actionButtonClicked:(UIButton *)button
  312. {
  313. TYAlertAction *action = _actions[button.tag - kButtonTagOffset];
  314. if (_clickedAutoHide) {
  315. [self hideView];
  316. }
  317. if (action.handler) {
  318. action.handler(action);
  319. }
  320. }
  321. //- (void)dealloc
  322. //{
  323. // NSLog(@"%@ dealloc",NSStringFromClass([self class]));
  324. //}
  325. @end