// // WentingLicenseViewController.m // LBUIDemo // // Created by 吴博 on 2021/7/26. // Copyright © 2021 xunjiangtao. All rights reserved. // #import "WentingLicenseViewController.h" #import "WentingLicenseWebView.h" #define kSourceViewWidth self.view.frame.size.width #define kSourceViewHeight self.view.frame.size.height #define kViewWidth 280 #define kViewHeight 300 #define kHeaderViewHeight 40 #define kCenterViewHeight 200 #define kFooterViewHeight 50 #define kTitleText @"用户协议和隐私政策" #define kContentText @"我们非常重视您的个人信息和隐私保护。为了更好地保障您的个人权益,请在享受我们的服务前,详细阅读并同意《用户协议》《隐私政策》。如您同意,请点击“同意”" #define kCancelBtnText @"拒绝" #define kConfirmBtnText @"同意" @interface WentingLicenseViewController() @property (nonatomic, strong) UIWindow *window; @end @implementation WentingLicenseViewController - (id)init { id controller = [super init]; if (self.window == nil) { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; } self.window.rootViewController = controller; self.window.backgroundColor = [UIColor blackColor]; self.window.hidden = NO; [self.window makeKeyAndVisible]; return controller; } - (void)remove { __weak WentingLicenseViewController *weakSelf = self; [self dismissViewControllerAnimated:NO completion:^{ if (weakSelf.window != nil) { weakSelf.window.hidden = YES; weakSelf.window = nil; } }]; } - (void)dealloc { if (self.window != nil) { self.window.hidden = YES; self.window = nil; } } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; [self setupSubviews]; } - (BOOL)prefersStatusBarHidden { return YES; } - (void)setupSubviews { [self setupHeaderViews]; [self setupCenterViews]; [self setupFooterViews]; } - (void)setupHeaderViews { CGRect frame = CGRectMake((kSourceViewWidth - kViewWidth) / 2, (kSourceViewHeight - kViewHeight) / 2, kViewWidth, kHeaderViewHeight); UIImageView *imageView = [[UIImageView alloc] init]; imageView.backgroundColor = [UIColor whiteColor]; imageView.frame = frame; [self.view addSubview:imageView]; UILabel *label = [[UILabel alloc] init]; label.frame = frame; label.font = [UIFont systemFontOfSize:20]; label.textColor = [UIColor blackColor]; label.textAlignment = NSTextAlignmentCenter; label.text = kTitleText; [self.view addSubview:label]; } - (void)setupCenterViews { CGRect frame = CGRectMake((kSourceViewWidth - kViewWidth) / 2, (kSourceViewHeight - kViewHeight) / 2 + kHeaderViewHeight, kViewWidth, kCenterViewHeight); UITextView *textView = [[UITextView alloc] init]; textView.frame = frame; textView.backgroundColor = [UIColor whiteColor]; textView.font = [UIFont systemFontOfSize:13]; textView.textColor = [UIColor blackColor]; textView.textAlignment = NSTextAlignmentLeft; NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[kContentText dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil]; textView.attributedText = attrStr; textView.userInteractionEnabled = true; [textView setEditable:false]; [textView setSelectable:false]; [textView setScrollEnabled:true]; UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(richTextClickSel:)]; [textView addGestureRecognizer:recognizer]; [self.view addSubview:textView]; [self contentSizeToFit:textView fontSize:13]; } - (void)setupFooterViews { CGRect frame1 = CGRectMake((kSourceViewWidth - kViewWidth) / 2, (kSourceViewHeight - kViewHeight) / 2 + kHeaderViewHeight + kCenterViewHeight, kViewWidth / 2, kFooterViewHeight); CGRect frame2 = CGRectMake(kSourceViewWidth / 2, (kSourceViewHeight - kViewHeight) / 2 + kHeaderViewHeight + kCenterViewHeight, kViewWidth / 2, kFooterViewHeight); UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.backgroundColor = [UIColor grayColor]; button.frame = frame1; button.titleLabel.font = [UIFont systemFontOfSize:14]; [button setTitle:kCancelBtnText forState:UIControlStateNormal]; [button setTitleColor:[UIColor colorWithRed:202 green:202 blue:202 alpha:255] forState:UIControlStateNormal]; [button addTarget:self action:@selector(cancelBtnSel:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; button = [UIButton buttonWithType:UIButtonTypeCustom]; button.backgroundColor = [UIColor greenColor]; button.frame = frame2; button.titleLabel.font = [UIFont systemFontOfSize:14]; [button setTitle:kConfirmBtnText forState:UIControlStateNormal]; [button setTitleColor:[UIColor colorWithRed:104 green:221 blue:228 alpha:255] forState:UIControlStateNormal]; [button addTarget:self action:@selector(comfirmBtnSel:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)cancelBtnSel:(UIButton *)button { if (self.cancelAction) { self.cancelAction(); } } - (void)comfirmBtnSel:(UIButton *)button { if (self.confirmAction) { self.confirmAction(); } } - (void)richTextClickSel:(UIGestureRecognizer *)recognizer { UITextView *textView = (UITextView *)recognizer.view; CGPoint point = [recognizer locationInView:textView]; point.x -= textView.textContainerInset.left; point.y -= textView.textContainerInset.top; NSUInteger val = [[textView layoutManager] glyphIndexForPoint:point inTextContainer:[textView textContainer]]; NSAttributedString* attrTxt = textView.attributedText; NSDictionary *dic = [attrTxt attributesAtIndex:val effectiveRange:nil]; for (id key in dic) { if (key == NSLinkAttributeName) { NSString *urlStr = [NSString stringWithFormat:@"%@", [dic objectForKey:key]]; WentingLicenseWebView *webview = [[WentingLicenseWebView alloc] initWithUrl:urlStr]; webview.modalPresentationStyle = UIModalPresentationFullScreen; webview.modalPresentationCapturesStatusBarAppearance = true; if (self.presentedViewController) { [self.presentedViewController dismissViewControllerAnimated:NO completion:^{ [self presentViewController:webview animated:NO completion:nil]; }]; } else { [self presentViewController:webview animated:NO completion:nil]; } } } } - (void)contentSizeToFit:(UITextView *)textView fontSize:(CGFloat)fontSize { //先判断一下有没有文字(没文字就没必要设置居中了) if([textView.text length]>0) { //textView的contentSize属性 CGSize contentSize = textView.contentSize; //textView的内边距属性 UIEdgeInsets offset; CGSize newSize = contentSize; //如果文字内容高度没有超过textView的高度 if(contentSize.height <= textView.frame.size.height) { //textView的高度减去文字高度除以2就是Y方向的偏移量,也就是textView的上内边距 CGFloat offsetY = (textView.frame.size.height - contentSize.height)/2; offset = UIEdgeInsetsMake(offsetY, 0, 0, 0); } else //如果文字高度超出textView的高度 { newSize = textView.frame.size; offset = UIEdgeInsetsZero; //通过一个while循环,设置textView的文字大小,使内容不超过整个textView的高度(这个根据需要可以自己设置) while (contentSize.height > textView.frame.size.height) { [textView setFont:[UIFont systemFontOfSize:fontSize--]]; contentSize = textView.contentSize; } newSize = contentSize; } //根据前面计算设置textView的ContentSize和Y方向偏移量 [textView setContentSize:newSize]; [textView setContentInset:offset]; } } @end