12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- //
- // HWAgreementViewController.m
- // Private-X
- //
- // Created by 余衡武 on 2022/3/24.
- //
- #import "HWAgreementViewController.h"
- #import <WebKit/WebKit.h>
- @interface HWAgreementViewController () <WKNavigationDelegate>
- @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:@"<html> \n" "<head> \n" "<meta name=\"viewport\" content=\"initial-scale=1.0, maximum-scale=1.0, user-scalable=no\" /> \n" "<style type=\"text/css\"> \n" "body {font-size:16px;}\n" "</style> \n" "</head> \n" "<body>" "<script type='text/javascript'>" "window.onload = function(){\n" "var $img = document.getElementsByTagName('img');\n" "for(var p in $img){\n" " $img[p].style.width = '100%%';\n" "$img[p].style.height ='auto'\n" "}\n" "}" "</script>%@" "</body>" "</html>",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
|