// // HWAgreementViewController.m // Private-X // // Created by 余衡武 on 2022/3/24. // #import "HWAgreementViewController.h" #import @interface HWAgreementViewController () @property (weak, nonatomic) IBOutlet UIView *header; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; @property (nonatomic, strong) WKWebView *webView; @end @implementation HWAgreementViewController - (void)viewDidLoad { [super viewDidLoad]; [self drawView]; } - (void)drawView { [self.header mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(NAVIHEIGHT); }]; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"agreement"ofType:@"txt"]; NSError *error; NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; WKWebView *webView = [self webViewLoadHTMLString:htmlString]; self.webView = webView; [webView mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.header.mas_bottom).mas_offset(10); make.left.mas_equalTo(8); make.width.mas_equalTo(SCREEN_W-16); make.bottom.mas_equalTo(self.view.mas_bottom); }]; self.webView.backgroundColor = [UIColor hwColor:@"#101010"]; } - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{ [self.webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.background='#101010'"completionHandler:nil]; [self.webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#919191'"completionHandler:nil]; } - (WKWebView *)webViewLoadHTMLString:(NSString *)htmlStr { NSString *htmls = [NSString stringWithFormat:@" \n" " \n" " \n" " \n" " \n" "" "%@" "" "",htmlStr]; //js脚本 (脚本注入设置网页样式) NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; //注入 WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; WKUserContentController *wkUController = [[WKUserContentController alloc] init]; [wkUController addUserScript:wkUScript]; //配置对象 WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init]; wkWebConfig.userContentController = wkUController; //改变初始化方法 (这里一定要给个初始宽度,要不算的高度不对) WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W-30, 0) configuration:wkWebConfig]; webView.scrollView.bounces = YES; webView.scrollView.scrollEnabled = YES; webView.navigationDelegate = self; [self.view addSubview:webView]; [webView loadHTMLString:htmls baseURL:nil]; return webView; } - (IBAction)backBtnClick:(UIButton *)sender { [self.navigationController popViewControllerAnimated:YES]; } @end