HWAgreementViewController.m 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // HWAgreementViewController.m
  3. // Private-X
  4. //
  5. // Created by 余衡武 on 2022/3/24.
  6. //
  7. #import "HWAgreementViewController.h"
  8. #import <WebKit/WebKit.h>
  9. @interface HWAgreementViewController () <WKNavigationDelegate>
  10. @property (weak, nonatomic) IBOutlet UIView *header;
  11. @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
  12. @property (nonatomic, strong) WKWebView *webView;
  13. @end
  14. @implementation HWAgreementViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. [self drawView];
  18. }
  19. - (void)drawView {
  20. [self.header mas_updateConstraints:^(MASConstraintMaker *make) {
  21. make.height.mas_equalTo(NAVIHEIGHT);
  22. }];
  23. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"agreement"ofType:@"txt"];
  24. NSError *error;
  25. NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
  26. WKWebView *webView = [self webViewLoadHTMLString:htmlString];
  27. self.webView = webView;
  28. [webView mas_updateConstraints:^(MASConstraintMaker *make) {
  29. make.top.mas_equalTo(self.header.mas_bottom).mas_offset(10);
  30. make.left.mas_equalTo(8);
  31. make.width.mas_equalTo(SCREEN_W-16);
  32. make.bottom.mas_equalTo(self.view.mas_bottom);
  33. }];
  34. self.webView.backgroundColor = [UIColor hwColor:@"#101010"];
  35. }
  36. - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  37. [self.webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.background='#101010'"completionHandler:nil];
  38. [self.webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#919191'"completionHandler:nil];
  39. }
  40. - (WKWebView *)webViewLoadHTMLString:(NSString *)htmlStr {
  41. 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];
  42. //js脚本 (脚本注入设置网页样式)
  43. NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
  44. //注入
  45. WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
  46. WKUserContentController *wkUController = [[WKUserContentController alloc] init];
  47. [wkUController addUserScript:wkUScript];
  48. //配置对象
  49. WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
  50. wkWebConfig.userContentController = wkUController;
  51. //改变初始化方法 (这里一定要给个初始宽度,要不算的高度不对)
  52. WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W-30, 0) configuration:wkWebConfig];
  53. webView.scrollView.bounces = YES;
  54. webView.scrollView.scrollEnabled = YES;
  55. webView.navigationDelegate = self;
  56. [self.view addSubview:webView];
  57. [webView loadHTMLString:htmls baseURL:nil];
  58. return webView;
  59. }
  60. - (IBAction)backBtnClick:(UIButton *)sender {
  61. [self.navigationController popViewControllerAnimated:YES];
  62. }
  63. @end