WentingLicenseViewController.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // WentingLicenseViewController.m
  3. // LBUIDemo
  4. //
  5. // Created by 吴博 on 2021/7/26.
  6. // Copyright © 2021 xunjiangtao. All rights reserved.
  7. //
  8. #import "WentingLicenseViewController.h"
  9. #import "WentingLicenseWebView.h"
  10. #define kSourceViewWidth self.view.frame.size.width
  11. #define kSourceViewHeight self.view.frame.size.height
  12. #define kViewWidth 280
  13. #define kViewHeight 300
  14. #define kHeaderViewHeight 40
  15. #define kCenterViewHeight 200
  16. #define kFooterViewHeight 50
  17. #define kTitleText @"用户协议和隐私政策"
  18. #define kContentText @"我们非常重视您的个人信息和隐私保护。为了更好地保障您的个人权益,请在享受我们的服务前,详细阅读并同意<a href=\"http://www.pkey.cn/pp/protocol.html\">《用户协议》</a>和<a href=\"http://www.pkey.cn/pp/privacy.html\">《隐私政策》</a>。如您同意,请点击“同意”"
  19. #define kCancelBtnText @"拒绝"
  20. #define kConfirmBtnText @"同意"
  21. @interface WentingLicenseViewController()
  22. @property (nonatomic, strong) UIWindow *window;
  23. @end
  24. @implementation WentingLicenseViewController
  25. - (id)init {
  26. id controller = [super init];
  27. if (self.window == nil) {
  28. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  29. }
  30. self.window.rootViewController = controller;
  31. self.window.backgroundColor = [UIColor blackColor];
  32. self.window.hidden = NO;
  33. [self.window makeKeyAndVisible];
  34. return controller;
  35. }
  36. - (void)remove {
  37. __weak WentingLicenseViewController *weakSelf = self;
  38. [self dismissViewControllerAnimated:NO completion:^{
  39. if (weakSelf.window != nil) {
  40. weakSelf.window.hidden = YES;
  41. weakSelf.window = nil;
  42. }
  43. }];
  44. }
  45. - (void)dealloc {
  46. if (self.window != nil) {
  47. self.window.hidden = YES;
  48. self.window = nil;
  49. }
  50. }
  51. - (void)viewDidLoad {
  52. [super viewDidLoad];
  53. self.view.backgroundColor = [UIColor blackColor];
  54. [self setupSubviews];
  55. }
  56. - (BOOL)prefersStatusBarHidden {
  57. return YES;
  58. }
  59. - (void)setupSubviews {
  60. [self setupHeaderViews];
  61. [self setupCenterViews];
  62. [self setupFooterViews];
  63. }
  64. - (void)setupHeaderViews {
  65. CGRect frame = CGRectMake((kSourceViewWidth - kViewWidth) / 2,
  66. (kSourceViewHeight - kViewHeight) / 2, kViewWidth, kHeaderViewHeight);
  67. UIImageView *imageView = [[UIImageView alloc] init];
  68. imageView.backgroundColor = [UIColor whiteColor];
  69. imageView.frame = frame;
  70. [self.view addSubview:imageView];
  71. UILabel *label = [[UILabel alloc] init];
  72. label.frame = frame;
  73. label.font = [UIFont systemFontOfSize:20];
  74. label.textColor = [UIColor blackColor];
  75. label.textAlignment = NSTextAlignmentCenter;
  76. label.text = kTitleText;
  77. [self.view addSubview:label];
  78. }
  79. - (void)setupCenterViews {
  80. CGRect frame = CGRectMake((kSourceViewWidth - kViewWidth) / 2,
  81. (kSourceViewHeight - kViewHeight) / 2 + kHeaderViewHeight, kViewWidth, kCenterViewHeight);
  82. UITextView *textView = [[UITextView alloc] init];
  83. textView.frame = frame;
  84. textView.backgroundColor = [UIColor whiteColor];
  85. textView.font = [UIFont systemFontOfSize:13];
  86. textView.textColor = [UIColor blackColor];
  87. textView.textAlignment = NSTextAlignmentLeft;
  88. NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[kContentText dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
  89. textView.attributedText = attrStr;
  90. textView.userInteractionEnabled = true;
  91. [textView setEditable:false];
  92. [textView setSelectable:false];
  93. [textView setScrollEnabled:true];
  94. UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(richTextClickSel:)];
  95. [textView addGestureRecognizer:recognizer];
  96. [self.view addSubview:textView];
  97. [self contentSizeToFit:textView fontSize:13];
  98. }
  99. - (void)setupFooterViews {
  100. CGRect frame1 = CGRectMake((kSourceViewWidth - kViewWidth) / 2,
  101. (kSourceViewHeight - kViewHeight) / 2 + kHeaderViewHeight + kCenterViewHeight, kViewWidth / 2, kFooterViewHeight);
  102. CGRect frame2 = CGRectMake(kSourceViewWidth / 2,
  103. (kSourceViewHeight - kViewHeight) / 2 + kHeaderViewHeight + kCenterViewHeight, kViewWidth / 2, kFooterViewHeight);
  104. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  105. button.backgroundColor = [UIColor grayColor];
  106. button.frame = frame1;
  107. button.titleLabel.font = [UIFont systemFontOfSize:14];
  108. [button setTitle:kCancelBtnText forState:UIControlStateNormal];
  109. [button setTitleColor:[UIColor colorWithRed:202 green:202 blue:202 alpha:255] forState:UIControlStateNormal];
  110. [button addTarget:self action:@selector(cancelBtnSel:) forControlEvents:UIControlEventTouchUpInside];
  111. [self.view addSubview:button];
  112. button = [UIButton buttonWithType:UIButtonTypeCustom];
  113. button.backgroundColor = [UIColor greenColor];
  114. button.frame = frame2;
  115. button.titleLabel.font = [UIFont systemFontOfSize:14];
  116. [button setTitle:kConfirmBtnText forState:UIControlStateNormal];
  117. [button setTitleColor:[UIColor colorWithRed:104 green:221 blue:228 alpha:255] forState:UIControlStateNormal];
  118. [button addTarget:self action:@selector(comfirmBtnSel:) forControlEvents:UIControlEventTouchUpInside];
  119. [self.view addSubview:button];
  120. }
  121. - (void)cancelBtnSel:(UIButton *)button {
  122. if (self.cancelAction) {
  123. self.cancelAction();
  124. }
  125. }
  126. - (void)comfirmBtnSel:(UIButton *)button {
  127. if (self.confirmAction) {
  128. self.confirmAction();
  129. }
  130. }
  131. - (void)richTextClickSel:(UIGestureRecognizer *)recognizer {
  132. UITextView *textView = (UITextView *)recognizer.view;
  133. CGPoint point = [recognizer locationInView:textView];
  134. point.x -= textView.textContainerInset.left;
  135. point.y -= textView.textContainerInset.top;
  136. NSUInteger val = [[textView layoutManager] glyphIndexForPoint:point inTextContainer:[textView textContainer]];
  137. NSAttributedString* attrTxt = textView.attributedText;
  138. NSDictionary<NSAttributedStringKey, id> *dic = [attrTxt attributesAtIndex:val effectiveRange:nil];
  139. for (id key in dic) {
  140. if (key == NSLinkAttributeName)
  141. {
  142. NSString *urlStr = [NSString stringWithFormat:@"%@", [dic objectForKey:key]];
  143. WentingLicenseWebView *webview = [[WentingLicenseWebView alloc] initWithUrl:urlStr];
  144. webview.modalPresentationStyle = UIModalPresentationFullScreen;
  145. webview.modalPresentationCapturesStatusBarAppearance = true;
  146. if (self.presentedViewController) {
  147. [self.presentedViewController dismissViewControllerAnimated:NO completion:^{
  148. [self presentViewController:webview animated:NO completion:nil];
  149. }];
  150. } else {
  151. [self presentViewController:webview animated:NO completion:nil];
  152. }
  153. }
  154. }
  155. }
  156. - (void)contentSizeToFit:(UITextView *)textView fontSize:(CGFloat)fontSize
  157. {
  158. //先判断一下有没有文字(没文字就没必要设置居中了)
  159. if([textView.text length]>0)
  160. {
  161. //textView的contentSize属性
  162. CGSize contentSize = textView.contentSize;
  163. //textView的内边距属性
  164. UIEdgeInsets offset;
  165. CGSize newSize = contentSize;
  166. //如果文字内容高度没有超过textView的高度
  167. if(contentSize.height <= textView.frame.size.height)
  168. {
  169. //textView的高度减去文字高度除以2就是Y方向的偏移量,也就是textView的上内边距
  170. CGFloat offsetY = (textView.frame.size.height - contentSize.height)/2;
  171. offset = UIEdgeInsetsMake(offsetY, 0, 0, 0);
  172. }
  173. else //如果文字高度超出textView的高度
  174. {
  175. newSize = textView.frame.size;
  176. offset = UIEdgeInsetsZero;
  177. //通过一个while循环,设置textView的文字大小,使内容不超过整个textView的高度(这个根据需要可以自己设置)
  178. while (contentSize.height > textView.frame.size.height)
  179. {
  180. [textView setFont:[UIFont systemFontOfSize:fontSize--]];
  181. contentSize = textView.contentSize;
  182. }
  183. newSize = contentSize;
  184. }
  185. //根据前面计算设置textView的ContentSize和Y方向偏移量
  186. [textView setContentSize:newSize];
  187. [textView setContentInset:offset];
  188. }
  189. }
  190. @end