YBAlertView.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // YBAlertView.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2019/11/15.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "YBAlertView.h"
  9. #import "YBInvitationView.h"
  10. @interface YBAlertView ()
  11. {
  12. NSDictionary *_contentDic;
  13. }
  14. @end
  15. @implementation YBAlertView
  16. - (void)awakeFromNib {
  17. [super awakeFromNib];
  18. //有标题默认值
  19. // _titleHeight.constant = 25;
  20. // _contentTop.constant = 25;
  21. //无标题默认
  22. _titleHeight.constant = 0;
  23. _contentTop.constant = 0;
  24. //默认色值
  25. _titleL.textColor = _contentL.textColor = RGB_COLOR(@"#323232", 1);
  26. _contentL.font = SYS_Font(14);
  27. _contentL.numberOfLines = 0;
  28. _contentL.textAlignment = NSTextAlignmentCenter;
  29. [_cancleBtn setTitleColor:RGB_COLOR(@"#969696", 1) forState:0];//c9c9c9
  30. [_sureBtn setTitleColor:Pink_Cor forState:0];//RGB_COLOR(@"#fb483a", 1)
  31. }
  32. +(instancetype)showAlertView:(NSDictionary *)contentDic complete:(YBAlertBlock)complete;{
  33. YBAlertView *aletView = [[[NSBundle mainBundle]loadNibNamed:@"YBAlertView" owner:nil options:nil]objectAtIndex:0];
  34. if (complete) {
  35. aletView.ybAlertEvent = ^(int eventType) {
  36. complete(eventType);
  37. };
  38. }
  39. [aletView setContent:contentDic];
  40. return aletView;
  41. }
  42. -(void)setContent:(NSDictionary *)contentDic{
  43. self.frame = [UIScreen mainScreen].bounds;
  44. _contentDic = contentDic;
  45. _titleHeight.constant = 0;
  46. _contentTop.constant = 0;
  47. if (![PublicObj checkNull:minstr([_contentDic valueForKey:@"title"])]) {
  48. _titleHeight.constant = 25;
  49. _contentTop.constant = 15;
  50. _titleL.text = minstr([_contentDic valueForKey:@"title"]);
  51. }
  52. _contentL.text = minstr([_contentDic valueForKey:@"msg"]);
  53. NSMutableAttributedString *introText = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",_contentL.text]];
  54. if ([minstr([_contentDic valueForKey:@"msg"]) containsString:@"[rich]"]) {
  55. //说明是富文本
  56. NSRange richRange = [_contentL.text rangeOfString:@"[rich]"];
  57. UIImage *image = [UIImage imageNamed:minstr([_contentDic valueForKey:@"richImg"])];
  58. NSMutableAttributedString *attachment = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:CGSizeMake(13, 13) alignToFont:SYS_Font(14) alignment:(YYTextVerticalAlignment)YYTextVerticalAlignmentCenter];
  59. //'richImg'替换[rich]
  60. [introText replaceCharactersInRange:richRange withAttributedString:attachment];
  61. }
  62. introText.yy_font = SYS_Font(14);
  63. introText.yy_lineSpacing = 8;
  64. introText.yy_alignment = NSTextAlignmentCenter;
  65. _contentL.attributedText = introText;
  66. //计算content高度
  67. CGSize introSize = CGSizeMake(_bgView.width*0.8, CGFLOAT_MAX);
  68. YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:introSize text:introText];
  69. _contentL.textLayout = layout;
  70. CGFloat introHeight = layout.textBoundingSize.height;
  71. _contentHeight.constant = introHeight;
  72. if (![PublicObj checkNull:minstr([_contentDic valueForKey:@"left"])]) {
  73. _vLineL.hidden = NO;
  74. _cancleBtn.hidden = NO;
  75. [_cancleBtn setTitle:minstr([_contentDic valueForKey:@"left"]) forState:0];
  76. }else {
  77. _vLineL.hidden = YES;
  78. _cancleBtn.hidden = YES;
  79. [_sureBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  80. make.width.centerX.bottom.equalTo(_bgView);
  81. make.height.mas_equalTo(40);
  82. }];
  83. }
  84. [_sureBtn setTitle:minstr([_contentDic valueForKey:@"right"]) forState:0];
  85. dispatch_async(dispatch_get_main_queue(), ^{
  86. [self show];
  87. });
  88. }
  89. -(void)show {
  90. /*
  91. for (UIView *view in [UIApplication sharedApplication].delegate.window.subviews) {
  92. if ([view isKindOfClass:[YBAlertView class]]) {
  93. [view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  94. [view removeFromSuperview];
  95. }
  96. }*/
  97. [[UIApplication sharedApplication].delegate.window addSubview:self];
  98. [self changeLayer];
  99. [UIView animateWithDuration:0.3 animations:^{
  100. self.backgroundColor = RGB_COLOR(@"#000000", 0.2);
  101. } completion:^(BOOL finished) {
  102. }];
  103. }
  104. - (void)setAlertFrom:(AlertFrom)alertFrom{
  105. _alertFrom = alertFrom;
  106. [self changeLayer];
  107. }
  108. -(void)changeLayer {
  109. [PublicObj layoutWindowPopLayer];
  110. }
  111. -(void)dismiss {
  112. [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  113. [self removeFromSuperview];
  114. }
  115. //左边
  116. - (IBAction)clikcCancelBtn:(UIButton *)sender {
  117. if (!_forbidSureDismiss) {
  118. [self dismiss];
  119. }
  120. if (self.ybAlertEvent) {
  121. self.ybAlertEvent(0);
  122. }
  123. }
  124. //右边
  125. - (IBAction)clikcSureBtn:(UIButton *)sender {
  126. [self dismiss];
  127. if (self.ybAlertEvent) {
  128. self.ybAlertEvent(1);
  129. }
  130. }
  131. @end