#import "ViewController.h" #import <WebKit/WebKit.h> @interface ViewController () <WKNavigationDelegate> @property (nonatomic, strong) WKWebView *webView; @property UIContentSizeCategory currentContentSizeCategory; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. WKUserContentController *wkUController = [[WKUserContentController alloc] init]; WKWebViewConfiguration *webConfiguration = [[WKWebViewConfiguration alloc]init]; webConfiguration.userContentController = wkUController; self.webView = [[WKWebView alloc]initWithFrame:self.view.frame configuration:webConfiguration]; [self.view addSubview:self.webView]; self.webView.navigationDelegate = self; NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"home" ofType:@"html"]; NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; [self.webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]]; // Large Text Size changes observer [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentSizeCategoryDidChangeNotification:) name:UIContentSizeCategoryDidChangeNotification object:nil]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (@available(iOS 11.0, *)) { if (UIContentSizeCategoryIsAccessibilityCategory(self.traitCollection.preferredContentSizeCategory)) { self.currentContentSizeCategory = self.traitCollection.preferredContentSizeCategory; } } [self performSelector:@selector(updateAccessibilityFontSize) withObject:nil afterDelay:0.2f]; } #pragma mark - Accessibility Font Size - (void)contentSizeCategoryDidChangeNotification:(NSNotification *)notification { if ([notification.userInfo objectForKey:UIContentSizeCategoryNewValueKey]) { self.currentContentSizeCategory = [notification.userInfo objectForKey:UIContentSizeCategoryNewValueKey]; if (@available(iOS 11.0, *)) { if (!UIContentSizeCategoryIsAccessibilityCategory(self.currentContentSizeCategory)) { self.currentContentSizeCategory = nil; } } [self updateAccessibilityFontSize]; } } -(void)updateAccessibilityFontSize { CGFloat pointSize = [UIFontDescriptor preferredFontDescriptorWithTextStyle: UIFontTextStyleBody].pointSize; if (self.currentContentSizeCategory) { pointSize = 100 + pointSize; } else { pointSize = 100; } ViewController * __weak weakSelf = self; NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%ld%@'",(long)pointSize, @"%"]; [self.webView evaluateJavaScript:jsString completionHandler:^(id _Nullable result, NSError * _Nullable error) { if ( !error ) { [weakSelf.webView reload]; } }]; } - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation { [self performSelector:@selector(updateAccessibilityFontSize) withObject:nil afterDelay:0.5f]; } @end