CSAlertView.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // CSAlertView.m
  3. // zhuxun
  4. //
  5. // Created by JonathanH on 2017/7/20.
  6. //
  7. //
  8. #import "CSAlertView.h"
  9. ///alertView 宽
  10. #define AlertW 280
  11. ///各个栏目之间的距离
  12. #define XLSpace 10.0
  13. @interface CSAlertView()
  14. //弹窗
  15. @property (nonatomic,weak) UIView *alertView;
  16. //title
  17. @property (nonatomic,weak) UILabel *titleLbl;
  18. //内容
  19. @property (nonatomic,weak) UILabel *msgLbl;
  20. //输入文本框
  21. @property (nonatomic,weak) UITextField *textField;
  22. //文本框内菊花
  23. @property (nonatomic,weak) UIActivityIndicatorView *actityindicatorview;
  24. //确认按钮
  25. @property (nonatomic,weak) UIButton *sureBtn;
  26. //取消按钮
  27. @property (nonatomic,weak) UIButton *cancleBtn;
  28. //横线线
  29. @property (nonatomic,weak) UIView *lineView;
  30. //竖线
  31. @property (nonatomic,weak) UIView *verLineView;
  32. @end
  33. @implementation CSAlertView
  34. - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message inputMode:(BOOL)inputmode sureBtn:(NSString *)sureTitle cancleBtn:(NSString *)cancleTitle
  35. {
  36. if (self == [super init]) {
  37. self.frame = [UIScreen mainScreen].bounds;
  38. self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
  39. UIView *alertView = [[UIView alloc] init];
  40. alertView.backgroundColor = [UIColor whiteColor];
  41. alertView.layer.cornerRadius = 5.0;
  42. alertView.frame = CGRectMake(0, 0, AlertW, 100);
  43. alertView.layer.position = self.center;
  44. [self addSubview:alertView];
  45. self.alertView = alertView;
  46. CGFloat nextX =0;
  47. CGFloat nextY =0;
  48. if (title) {
  49. UILabel *titleLbl = [self GetAdaptiveLable:CGRectMake(2*XLSpace, 2*XLSpace, AlertW-4*XLSpace, 20) AndText:title andIsTitle:YES];
  50. titleLbl.textAlignment = NSTextAlignmentCenter;
  51. [self.alertView addSubview:titleLbl];
  52. self.titleLbl = titleLbl;
  53. CGFloat titleW = self.titleLbl.bounds.size.width;
  54. CGFloat titleH = self.titleLbl.bounds.size.height;
  55. self.titleLbl.frame = CGRectMake((AlertW-titleW)/2, 2*XLSpace, titleW, titleH);
  56. //有标题下一个控件是 subtitle 最多2行Nextpoint 更改为(XLSpace,CG)
  57. nextX = XLSpace;
  58. nextY = CGRectGetMaxY(_titleLbl.frame)+XLSpace;
  59. }
  60. if (message) {
  61. UILabel *msgLbl = [self GetAdaptiveLable:CGRectMake(nextX, nextY, AlertW-2*XLSpace, 20) AndText:message andIsTitle:NO];
  62. msgLbl.textAlignment = NSTextAlignmentCenter;
  63. msgLbl.textColor = [UIColor redColor];
  64. CGFloat msgW = msgLbl.bounds.size.width;
  65. CGFloat msgH = msgLbl.bounds.size.height;
  66. msgLbl.frame = _titleLbl?CGRectMake((AlertW-msgW)/2, CGRectGetMaxY(_titleLbl.frame)+XLSpace, msgW, msgH):CGRectMake((AlertW-msgW)/2, 2*XLSpace, msgW, msgH);
  67. [self.alertView addSubview:msgLbl];
  68. self.msgLbl = msgLbl;
  69. nextY = CGRectGetMaxY(msgLbl.frame)+XLSpace;
  70. }
  71. if(inputmode){
  72. UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(nextX, nextY, AlertW -2*XLSpace, 22)];
  73. textField.borderStyle = UITextBorderStyleRoundedRect;
  74. textField.backgroundColor = [UIColor lightGrayColor];
  75. textField.clearButtonMode = UITextFieldViewModeWhileEditing;
  76. UIActivityIndicatorView *actityindicatorview = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
  77. textField.rightView = actityindicatorview;
  78. self.actityindicatorview = actityindicatorview;
  79. textField.rightViewMode = UITextFieldViewModeUnlessEditing;
  80. textField.text = @"新建文件夹";
  81. [self.alertView addSubview:textField];
  82. self.textField = textField;
  83. nextY = CGRectGetMaxY(_textField.frame)+2*XLSpace;
  84. }
  85. UIView *lineView = [[UIView alloc] init];
  86. lineView.frame = CGRectMake(0, nextY, AlertW, 1);
  87. lineView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
  88. [alertView addSubview:lineView];
  89. self.lineView = lineView;
  90. //两个按钮
  91. if (cancleTitle && sureTitle) {
  92. UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  93. cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(_lineView.frame), (AlertW-1)/2, 40);
  94. [cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
  95. [cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
  96. [cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
  97. cancleBtn.tag = 1;
  98. [cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
  99. // UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cancleBtn.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(5.0, 5.0)];
  100. // CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  101. // maskLayer.frame = cancleBtn.bounds;
  102. // maskLayer.path = maskPath.CGPath;
  103. // cancleBtn.layer.mask = maskLayer;
  104. //cancleBtn.layer.masksToBounds = YES;
  105. [alertView addSubview:cancleBtn];
  106. self.cancleBtn = cancleBtn;
  107. UIView *verLineView = [[UIView alloc] init];
  108. verLineView.frame = CGRectMake(CGRectGetMaxX(cancleBtn.frame), CGRectGetMaxY(lineView.frame), 1, 40);
  109. verLineView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.6];
  110. [alertView addSubview:verLineView];
  111. self.verLineView = verLineView;
  112. UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  113. sureBtn.frame = CGRectMake(CGRectGetMaxX(verLineView.frame), CGRectGetMaxY(lineView.frame), (AlertW-1)/2+1, 40);
  114. [sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
  115. [sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
  116. [sureBtn setTitle:sureTitle forState:UIControlStateNormal];
  117. sureBtn.tag = 2;
  118. [sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
  119. // UIBezierPath *bmaskPath = [UIBezierPath bezierPathWithRoundedRect:sureBtn.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
  120. // CAShapeLayer *bmaskLayer = [[CAShapeLayer alloc] init];
  121. // maskLayer.frame = sureBtn.bounds;
  122. // maskLayer.path = bmaskPath.CGPath;
  123. // sureBtn.layer.mask = bmaskLayer;
  124. //sureBtn.layer.masksToBounds = YES;
  125. [alertView addSubview:sureBtn];
  126. self.sureBtn = sureBtn;
  127. }
  128. /*if (cancleTitle && sureTitle) {
  129. }*/
  130. //只有取消按钮
  131. if (cancleTitle && !sureTitle) {
  132. UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  133. cancleBtn.frame = CGRectMake(0, CGRectGetMaxY(lineView.frame), AlertW, 40);
  134. [cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
  135. [cancleBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
  136. [cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
  137. //[self.cancleBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  138. cancleBtn.tag = 1;
  139. [cancleBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
  140. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cancleBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
  141. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  142. maskLayer.frame = cancleBtn.bounds;
  143. maskLayer.path = maskPath.CGPath;
  144. cancleBtn.layer.mask = maskLayer;
  145. [self.alertView addSubview:cancleBtn];
  146. self.cancleBtn = cancleBtn;
  147. }
  148. //只有确定按钮
  149. if(sureTitle && !cancleTitle){
  150. UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  151. sureBtn.frame = CGRectMake(0, CGRectGetMaxY(lineView.frame), AlertW, 40);
  152. [sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateNormal];
  153. [sureBtn setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.2]] forState:UIControlStateSelected];
  154. [sureBtn setTitle:sureTitle forState:UIControlStateNormal];
  155. //[self.sureBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  156. sureBtn.tag = 2;
  157. [sureBtn addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
  158. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.sureBtn.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5.0, 5.0)];
  159. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  160. maskLayer.frame = self.sureBtn.bounds;
  161. maskLayer.path = maskPath.CGPath;
  162. self.sureBtn.layer.mask = maskLayer;
  163. [self.alertView addSubview:sureBtn];
  164. self.sureBtn = sureBtn;
  165. }
  166. //计算高度
  167. CGFloat alertHeight = cancleTitle?CGRectGetMaxY(self.cancleBtn.frame):CGRectGetMaxY(self.sureBtn.frame);
  168. alertView.frame = CGRectMake(0, 0, AlertW, alertHeight);
  169. alertView.layer.position = self.center;
  170. [self registerForKeyboardNotifications];
  171. }
  172. return self;
  173. }
  174. #pragma mark - 弹出 -
  175. - (void)showXBAlertView
  176. {
  177. UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
  178. [rootWindow addSubview:self];
  179. [self creatShowAnimation];
  180. }
  181. - (void)creatShowAnimation
  182. {
  183. self.alertView.layer.position = self.center;
  184. self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
  185. [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
  186. self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
  187. } completion:^(BOOL finished) {
  188. }];
  189. }
  190. #pragma mark - 回调 -设置只有2 -- > 确定才回调
  191. - (void)buttonEvent:(UIButton *)sender
  192. {
  193. if (sender.tag == 2) {
  194. if (self.alertresult) {
  195. [self.textField resignFirstResponder];
  196. [self.actityindicatorview startAnimating];
  197. sender.enabled = NO;
  198. self.alertresult(self.textField.text);
  199. }
  200. }
  201. else{
  202. [self removeFromSuperview];
  203. }
  204. //
  205. }
  206. -(void)showErrorMsg:(NSString *)errormsg{
  207. self.msgLbl.text = errormsg;
  208. }
  209. -(UILabel *)GetAdaptiveLable:(CGRect)rect AndText:(NSString *)contentStr andIsTitle:(BOOL)isTitle
  210. {
  211. UILabel *contentLbl = [[UILabel alloc] initWithFrame:rect];
  212. contentLbl.numberOfLines = 0;
  213. contentLbl.text = contentStr;
  214. contentLbl.textAlignment = NSTextAlignmentCenter;
  215. if (isTitle) {
  216. contentLbl.font = [UIFont boldSystemFontOfSize:16.0];
  217. }else{
  218. contentLbl.font = [UIFont systemFontOfSize:14.0];
  219. }
  220. NSMutableAttributedString *mAttrStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
  221. NSMutableParagraphStyle *mParaStyle = [[NSMutableParagraphStyle alloc] init];
  222. mParaStyle.lineBreakMode = NSLineBreakByCharWrapping;
  223. [mParaStyle setLineSpacing:3.0];
  224. [mAttrStr addAttribute:NSParagraphStyleAttributeName value:mParaStyle range:NSMakeRange(0,[contentStr length])];
  225. [contentLbl setAttributedText:mAttrStr];
  226. [contentLbl sizeToFit];
  227. return contentLbl;
  228. }
  229. -(UIImage *)imageWithColor:(UIColor *)color
  230. {
  231. CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  232. UIGraphicsBeginImageContext(rect.size);
  233. CGContextRef context = UIGraphicsGetCurrentContext();
  234. CGContextSetFillColorWithColor(context, [color CGColor]);
  235. CGContextFillRect(context, rect);
  236. UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
  237. UIGraphicsEndImageContext();
  238. return theImage;
  239. }
  240. -(void)startActity{
  241. [self.actityindicatorview startAnimating];
  242. }
  243. -(void)stopActityAndRemoveAlertView{
  244. [self.actityindicatorview stopAnimating];
  245. self.sureBtn.enabled = YES;
  246. [self removeFromSuperview];
  247. }
  248. //针对键盘 frame调整
  249. - (void)registerForKeyboardNotifications
  250. {
  251. //使用NSNotificationCenter 鍵盤出現時
  252. [[NSNotificationCenter defaultCenter] addObserver:self
  253. selector:@selector(keyboardWasShown:)
  254. name:UIKeyboardWillShowNotification object:nil];
  255. //使用NSNotificationCenter 鍵盤隐藏時
  256. [[NSNotificationCenter defaultCenter] addObserver:self
  257. selector:@selector(keyboardWillBeHidden:)
  258. name:UIKeyboardWillHideNotification object:nil];
  259. }
  260. - (void)keyboardWasShown:(NSNotification*)aNotification
  261. {
  262. NSDictionary* ainfo = [aNotification userInfo];
  263. CGSize akbSize = [[ainfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
  264. NSNumber *aduration = [ainfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
  265. __weak typeof(self.alertView)weakalertview = self.alertView;
  266. __weak typeof(self)weakSelf = self;
  267. [UIView animateWithDuration:aduration.doubleValue animations:^{
  268. //判断键盘是退出还是出现.
  269. weakalertview.layer.position = CGPointMake(weakalertview.layer.position.x,(weakSelf.bounds.size.height- akbSize.height)/2);
  270. }];
  271. }
  272. - (void)keyboardWillBeHidden:(NSNotification*)aNotification
  273. {
  274. NSDictionary* ainfo = [aNotification userInfo];
  275. NSNumber *aduration = [ainfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
  276. __weak typeof(self.alertView)weakalertview = self.alertView;
  277. __weak typeof(self)weakSelf = self;
  278. [UIView animateWithDuration:aduration.doubleValue animations:^{
  279. weakalertview.layer.position = weakSelf.center;
  280. }];
  281. }
  282. -(void)dealloc{
  283. [[NSNotificationCenter defaultCenter]removeObserver:self];
  284. NSLog(@"delloc xbalertview");
  285. }
  286. @end