I am updating my iOS app to replace UIWebView with WKWebView. However I don't understand how to achieve the same behavior with WKWebView. With UIWebView I used scalesPageToFit to ensure the web pag was displayed with the same size as the screen size (so as to appear full screen without scrolling).
I found that solution on the web however it does not work :
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
NSString *javascript = #"var meta = document.createElement('meta');meta.setAttribute('name', 'viewport');meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no');document.getElementsByTagName('head')[0].appendChild(meta);";
[webView evaluateJavaScript:javascript completionHandler:nil];
}
you can also try the WKUserScript.
here's my working config:
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;
wkWebV = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig];
you can add additional configuration to your WKWebViewConfiguration.
Echo of nferocious76's answer, in swift code:
Swift2.x version
let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
let userScript = WKUserScript(source: jscript, injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, forMainFrameOnly: true)
let wkUController = WKUserContentController()
wkUController.addUserScript(userScript)
let wkWebConfig = WKWebViewConfiguration()
wkWebConfig.userContentController = wkUController
let youWebView = WKWebView(frame: CGRectZero, configuration: wkWebConfig)
swift3 version
let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
let userScript = WKUserScript(source: jscript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let wkUController = WKUserContentController()
wkUController.addUserScript(userScript)
let wkWebConfig = WKWebViewConfiguration()
wkWebConfig.userContentController = wkUController
let yourWebView = WKWebView(frame: self.view.bounds, configuration: wkWebConfig)
Similar to #nferocious76's but in Swift language
var scriptContent = "var meta = document.createElement('meta');"
scriptContent += "meta.name='viewport';"
scriptContent += "meta.content='width=device-width';"
scriptContent += "document.getElementsByTagName('head')[0].appendChild(meta);"
webView.evaluateJavaScript(scriptContent, completionHandler: nil)
I figured out below soultion. For making the content to fit with the device size.
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { for making the content to fit with the device size
let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
webView.evaluateJavaScript(jscript)
}
C# version, for use in Xamarin Custom Renderer:
string jScript = #"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript wkUScript = new WKUserScript((NSString)jScript, WKUserScriptInjectionTime.AtDocumentEnd, true);
WKUserContentController wkUController = new WKUserContentController();
wkUController.AddUserScript(wkUScript);
WKWebViewConfiguration wkWebConfig = new WKWebViewConfiguration();
wkWebConfig.UserContentController = wkUController;
WKWebView webView=new WKWebView(Frame, wkWebConfig);
Adding the below to my HTML script helped me.
<meta name="viewport" content="width=device-width, shrink-to-fit=YES">
It does the same as scalePageToFit in UIWebView.
refer: How to set iOS WkWebview zoom scale
Swift 5 solution:
let javaScript = """
var meta = document.createElement('meta');
meta.setAttribute('name', 'viewport');
meta.setAttribute('content', 'width=device-width');
document.getElementsByTagName('head')[0].appendChild(meta);
"""
webView.evaluateJavaScript(javaScript)
This work for me, I added two attributes in meta: initial-scale=1.0, shrink-to-fit=yes, that help fit text with html have a big image in html.
let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width, initial-scale=1.0, shrink-to-fit=yes'); document.getElementsByTagName('head')[0].appendChild(meta);"
let userScript = WKUserScript(source: jscript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let wkUController = WKUserContentController()
wkUController.addUserScript(userScript)
let wkWebConfig = WKWebViewConfiguration()
wkWebConfig.userContentController = wkUController
self.webView = WKWebView(frame: self.view.bounds, configuration: wkWebConfig)
None of this answers were working for me. I was trying to open an Amazon URL for a particular artist. Turns out that Amazon has a mobile responsive URL and a desktop URL. On desktop Safari, I changed to iPhone as user-agent (Develop > User Agent > Safari - iOS 13.1.3 - iPhone) and got the appropriate URL for mobile.
This works for me to fit the image size within width of device for Swift 5
Helpful with dynamic html from server
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
let fontAdjust = "var style = document.createElement('style');style.innerHTML = 'body { -webkit-text-size-adjust: none; }';document.getElementsByTagName('head')[0].appendChild(style);"
let imgAdjust = "var style = document.createElement('style');style.innerHTML = 'img { display: inline;height: auto;max-width: 100%; }';document.getElementsByTagName('head')[0].appendChild(style);"
webKitView.evaluateJavaScript(imgAdjust)
webKitView.evaluateJavaScript(fontAdjust)
webKitView.evaluateJavaScript(imgAdjust)
}
Modern Swift code to use in an extension
extension WKWebView {
func addInitialScaleMetaTag() {
evaluateJavaScript(
"""
var meta = document.createElement("meta");
meta.name = "viewport";
meta.content = "initial-scale=1.0";
document.getElementsByTagName('head')[0].appendChild(meta);
""") { result, error in
// handle error
}
}
}
// 100% work for me
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
//print(#function)
let scriptFontSizeAdjustment = "var style = document.createElement('style');style.innerHTML = 'body { -webkit-text-size-adjust: none; }';document.getElementsByTagName('head')[0].appendChild(style);"
let scriptImgSizeAdjustment = "var style = document.createElement('style');style.innerHTML = 'img { display: inline;height: auto;max-width: 100%; }';document.getElementsByTagName('head')[0].appendChild(style);"
webKitView.evaluateJavaScript(scriptFontSizeAdjustment)
webKitView.evaluateJavaScript(scriptImgSizeAdjustment)
}
Related
I am using xcode 11.5 and ios 13.5.1 and I am trying to read "epub" files located locally in my project using MKWebView as follow :
let webConfiguration = WKWebViewConfiguration()
webConfiguration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
webConfiguration.allowsInlineMediaPlayback = true
webConfiguration.allowsPictureInPictureMediaPlayback = true
let webView = WKWebView(frame: vc.view.frame, configuration: webConfiguration)
if let url = Bundle.main.url(forResource: fileName, withExtension: "epub") {
if url.isFileURL {
webView.loadFileURL(url, allowingReadAccessTo: url)
vc.view.addSubview(webView)
} else {
let request = URLRequest(url: url)
webView.load(request)
}
}
However, the page does not load "epub" file and instead, it gives me the following error :
> WF: _userSettingsForUser mobile: {
filterBlacklist = (
);
filterWhitelist = (
);
restrictWeb = 1;
useContentFilter = 0;
useContentFilterOverrides = 0;
whitelistEnabled = 0;
}
>
> WF: _WebFilterIsActive returning: NO
What's the problem with my solution? Is there any alternative ways to read epub files ?
An epub file is a zip file with some html, styles, and metadata.
To make an app that can read an epub you should with an unzipping library which may require you to rename the file first.
Here is a thread that might be helpful
So I have a Nativescript App using Angular (NG 5.1.1 / Angular 7.x)
I have a view with a webview.
#ViewChild("myWebView") webViewRef: ElementRef;
<WebView class="webview" #myWebView [src]="myURL"></WebView>
Inside my webview.component.ts I have this.
ngAfterViewInit(): void {
const webview: WebView = this.webViewRef.nativeElement;
webview.on(WebView.loadFinishedEvent, (args: LoadEventData) => {
this.setIndicatorFalse();
if (webview.ios) {
webview.ios.scrollView.delegate = UIScrollViewDelegateZ.new();
webview.ios.scrollView.minimumZoomScale = 1;
webview.ios.scrollView.maximumZoomScale = 1;
}
});
webview.on(WebView.loadStartedEvent, (args: LoadEventData) => {
if (webview.android) {
webview.android.getSettings().setBuiltInZoomControls(false);
webview.android.getSettings().setDisplayZoomControls(false);
} else {
// #ts-ignore
webview.ios.multipleTouchEnabled = false;
webview.ios.scalesPageToFit = false;
webview.ios.scrollView.bounces = false;
webview.ios.scrollView.showsHorizontalScrollIndicator = true;
webview.ios.scrollView.showsVerticalScrollIndicator = true;
webview.ios.opaque = false;
webview.ios.scrollView.allowsInlineMediaPlayback = true;
webview.ios.scrollView.mediaPlaybackRequiresUserAction = false;
}
});
}
As you can see i've tried all sorts of stuff to get this webview to not pinch zoom.
I am overriding my ViewDelegate with
webview.ios.scrollView.delegate = UIScrollViewDelegateZ.new();
and that file is here
export class UIScrollViewDelegateZ extends NSObject implements UIScrollViewDelegate {
public static ObjCProtocols = [UIScrollViewDelegate];
static new(): UIScrollViewDelegateZ {
console.log("here we are");
return <UIScrollViewDelegateZ>super.new();
}
viewForZoomingInScrollView(scrollView: UIScrollView): UIView {
console.log("viewForZoomingInScrollView");
return null;
}
scrollViewDidScroll(scrollView: UIScrollView): void {
console.log("scrollViewDidZoom");
return null;
}
scrollViewWillBeginZoomingWithView(scrollView: UIScrollView, view: UIView): void {
console.log("scrollViewWillBeginZoomingWithView " + scrollView);
return null;
}
}
When I load my webview and pinch zoom my console log says this:
CONSOLE LOG file:///app/app/webview/scrollDelegate.js:9:20: here we are
CONSOLE LOG file:///app/app/webview/scrollDelegate.js:21:20: scrollViewWillBeginZoomingWithView <WKScrollView: 0x7fb140afe000; baseClass = UIScrollView; frame = (0 0; 375 603); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000ab2100>; layer = <CALayer: 0x600000537480>; contentOffset: {0, 0}; contentSize: {375, 1916}; adjustedContentInset: {0, 0, 0, 0}>
CONSOLE LOG file:///app/app/webview/scrollDelegate.js:17:20: scrollViewDidZoom
So I feel like I am close?? but I just can't get it to go? I thought I was suppose to return null / undefined when trying to pinch zoom? which is what a few other SO answers / the web say to do but nothing is working.
the HTML page also has the correct metatag headers...
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0, minimal-ui"/>
Any help would be appreciated! Thanks!!
You have to override the default meta data being set by tns-core-modules.
import { WebView } from 'tns-core-modules/ui/web-view';
declare var WKUserScript, WKUserScriptInjectionTime, WKUserContentController, WKWebViewConfiguration, WKWebView, CGRectZero;
(<any>WebView.prototype).createNativeView = function () {
const jScript = `var meta = document.createElement('meta');
meta.setAttribute('name', 'viewport');
meta.setAttribute('content', 'initial-scale=1.0 maximum-scale=1.0');
document.getElementsByTagName('head')[0].appendChild(meta);`;
const wkUScript = WKUserScript.alloc().initWithSourceInjectionTimeForMainFrameOnly(jScript, WKUserScriptInjectionTime.AtDocumentEnd, true);
const wkUController = WKUserContentController.new();
wkUController.addUserScript(wkUScript);
const configuration = WKWebViewConfiguration.new();
configuration.userContentController = wkUController;
configuration.preferences.setValueForKey(
true,
"allowFileAccessFromFileURLs"
);
return new WKWebView({
frame: CGRectZero,
configuration: configuration
});
};
Here is a Playground Sample, since you are using Angular you may add the above lines to your app.component.ts
I am trying to autoplay video of youtube using YTplayer and load the Youtube video using VideoID,
I had also tried using loadHTMLString in Webview.
is iOS not allow to autoplay youtube ?
here is my code: 1) using YTplayer
let fullNameArr = video_url!.componentsSeparatedByString("=")
let video: String = fullNameArr[1]
enYTplayer.webView?.mediaPlaybackRequiresUserAction = false
YTplayer.webView?.allowsInlineMediaPlayback = true
YTplayer.loadWithVideoId(video)
YTplayer.playVideo()
2)using webView (iframe)
let vidurl = "https://www.youtube.com/embed/LG-_Dz6nseQ"
yutub_webview.loadHTMLString("<iframe width=\"\(yutub_webview.frame.size.width)\" hight=\"\(yutub_webview.frame.size.height)\" src=\"\(vidurl)?playsinline=1\" frameborder=\"0\" allowfullscreen & autoplay=1 &showinfo=0 &controls=0 autohide=1></iframe>", baseURL: nil)
3) using HTML
let youTubeVideoHTML :NSString = "<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'200', height:'204', videoId:'8LVN7WVgx0c', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
yutub_webview.loadHTMLString(youTubeVideoHTML as String,baseURL:nil)
and also tried out this one:
yutub_webview.loadHTMLString("<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='300' height='200' src='http://www.youtube.com/embed/LG-_Dz6nseQ? enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></body></html>",baseURL: NSBundle.mainBundle().bundleURL)
but still default play button is there and video not autoplay.
can any one tell me what i am doing wrong with these?
after a too much surfing I found the solution :
just add the delegate of YTPlayerViewDelegate
and use its method -
func playerViewDidBecomeReady(playerView: YTPlayerView) {
YTplayer.playVideo()
}
I try to change the opacity of the actionbar. For test i try to change the color over native method but this won't work.
I don't understand why, other native functions like UIAlert works fine.
I have no error or anything else only that the actionbar looks like this:
My target is completely transparent because the background has a gradient.
This is my page loaded function:
loaded(args){
let page = <Page>args.object;
page.bindingContext = appViewModel;
if (page.ios) {
var controller = frameModule.topmost().ios.controller;
var navigationBar = controller.navigationBar;
navigationBar.barTintColor = UIColor.blueColor();
}
}
thanks for help
UPDATE
my ts code:
var frameModule = require("ui/frame");
import {topmost} from "ui/frame";
import {Page} from "ui/page";
import {Observable, EventData} from "data/observable";
import {View} from "ui/core/view";
let appViewModel = new Observable({selectedPage: "dashboard"});
export abstract class BasePage {
//implement this function in the inheriting pages to set their specific binding context
abstract mainContentLoaded(args:EventData);
loaded(args){
let page = <Page>args.object;
page.bindingContext = appViewModel;
if (page.ios) {
frameModule.topmost().ios.navBarVisibility = "always";
page.ios.title = "Test Application";
var controller = frameModule.topmost().ios.controller;
var navBar = controller.navigationBar;
navBar.barTintColor = UIColor.redColor();
navBar.navigationBarHidden = false;
navBar.barStyle = UIBarStyle.UIBarStyleBlackTranslucent;
}
}
toggleDrawer(){
let page = <Page>topmost().currentPage;
let drawer = <any>page.getViewById("sidebar");
drawer.toggleDrawerState();
}
navigate(args){
let pageName = args.view.text.toLowerCase();
appViewModel.set("selectedPage", pageName);
topmost().navigate("modules/" + pageName + "/" + pageName);
}
}
my xml:
<page
xmlns="http://schemas.nativescript.org/tns.xsd"
xmlns:drawer="nativescript-telerik-ui/sidedrawer"
xmlns:widgets="shared/widgets"
loaded="loaded">
<page.actionBar>
<action-bar>
<navigation-button icon="res://ic_menu" tap="toggleDrawer" ios:visibility="collapsed" />
<action-bar.actionItems>
<ios>
<action-item icon="res://ic_menu" ios.position="left" tap="toggleDrawer" />
</ios>
</action-bar.actionItems>
</action-bar>
</page.actionBar>
<drawer:rad-side-drawer id="sidebar">
<drawer:rad-side-drawer.mainContent>
</drawer:rad-side-drawer.mainContent>
<drawer:rad-side-drawer.drawerContent>
<widgets:drawer-content />
</drawer:rad-side-drawer.drawerContent>
</drawer:rad-side-drawer>
</page>
The following solution worked out for me (iOS 9.3 with NativeScript 2.0.1)
if (page.ios) {
frameModule.topmost().ios.navBarVisibility = "always";
page.ios.title = "Test Application";
var controller = frameModule.topmost().ios.controller;
var navBar = controller.navigationBar;
navBar.barTintColor = UIColor.redColor();
navBar.navigationBarHidden = false;
navBar.barStyle = UIBarStyle.UIBarStyleBlackTranslucent;
}
I am successfully able to get HTML content and display into my UIWebView.
But want to customize the content by adding an external CSS file. I can only change the size of text and font. I tried every possible solution to make changes but it does not work - it shows no changes.
Below is my code
HTMLNode* body = [parser body];
HTMLNode* mainContentNode = [body findChildWithAttribute:#"id" matchingName:#"main_content" allowPartial:NO];
NSString *pageContent = [NSString stringWithFormat:#"%#%#", cssString, contentHtml];
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#"http://www.example.org"]];
-(void)webViewDidFinishLoad:(UIWebView *)webView1{
int fontSize = 50;
NSString *font = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
NSString *fontString = [[NSString alloc] initWithFormat:#"document.getElementById('body').style.fontFamily=\"helvetica\""];
[webView1 stringByEvaluatingJavaScriptFromString:fontString];
[webView1 stringByEvaluatingJavaScriptFromString:font];
}
Please help me get the css stylesheet in my view.
You can do it like this:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *cssString = #"body { font-family: Helvetica; font-size: 50px }"; // 1
NSString *javascriptString = #"var style = document.createElement('style'); style.innerHTML = '%#'; document.head.appendChild(style)"; // 2
NSString *javascriptWithCSSString = [NSString stringWithFormat:javascriptString, cssString]; // 3
[webView stringByEvaluatingJavaScriptFromString:javascriptWithCSSString]; // 4
}
What this code does:
// 1 : Define a string that contains all the CSS declarations
// 2 : Define a javascript string that creates a new <style> HTML DOM element and inserts the CSS declarations into it. Actually the inserting is done in the next step, right now there is only the %# placeholder. I did this to prevent the line from becoming too long, but step 2 and 3 could be done together.
// 3 : Combine the 2 strings
// 4 : Execute the javascript in the UIWebView
For this to work, your HTML has to have a <head></head> element.
EDIT:
You can also load the css string from a local css file (named "styles.css" in this case). Just replace step //1 with the following:
NSString *path = [[NSBundle mainBundle] pathForResource:#"styles" ofType:#"css"];
NSString *cssString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
As another option you can just inject a <link> element to the <head> that loads the CSS file:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *path = [[NSBundle mainBundle] pathForResource:#"styles" ofType:#"css"];
NSString *javascriptString = #"var link = document.createElement('link'); link.href = '%#'; link.rel = 'stylesheet'; document.head.appendChild(link)";
NSString *javascriptWithPathString = [NSString stringWithFormat:javascriptString, path];
[webView stringByEvaluatingJavaScriptFromString:javascriptWithPathString];
}
This solution works best for large CSS files. Unfortunately it does not work with remote HTML files. You can only use this when you want to insert CSS into HTML that you have downloaded to your app.
UPDATE: WKWebView / Swift 3.x
When you are working with a WKWebView injecting a <link> element does not work because of WKWebView's security settings.
You can still inject the css as a string. Either create the CSS string in your code //1 or put it in a local file //2. Just be aware that with WKWebView you have to do the injection in WKNavigationDelegate's webView(_:didFinish:) method:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
insertCSSString(into: webView) // 1
// OR
insertContentsOfCSSFile(into: webView) // 2
}
func insertCSSString(into webView: WKWebView) {
let cssString = "body { font-size: 50px; color: #f00 }"
let jsString = "var style = document.createElement('style'); style.innerHTML = '\(cssString)'; document.head.appendChild(style);"
webView.evaluateJavaScript(jsString, completionHandler: nil)
}
func insertContentsOfCSSFile(into webView: WKWebView) {
guard let path = Bundle.main.path(forResource: "styles", ofType: "css") else { return }
let cssString = try! String(contentsOfFile: path).trimmingCharacters(in: .whitespacesAndNewlines)
let jsString = "var style = document.createElement('style'); style.innerHTML = '\(cssString)'; document.head.appendChild(style);"
webView.evaluateJavaScript(jsString, completionHandler: nil)
}
Since UIWebView is deprecated in iOS 12, I'll only answer for WKWebView.
I've implemented CSS loading like it was described in the accepted answer. The problem was that sometimes the transition from HTML with no CSS applied to HTML with CSS was visible.
I think a better approach is to use the WKUserScript to inject the CSS like this:
lazy var webView: WKWebView = {
guard let path = Bundle.main.path(forResource: "style", ofType: "css") else {
return WKWebView()
}
let cssString = try! String(contentsOfFile: path).components(separatedBy: .newlines).joined()
let source = """
var style = document.createElement('style');
style.innerHTML = '\(cssString)';
document.head.appendChild(style);
"""
let userScript = WKUserScript(source: source,
injectionTime: .atDocumentEnd,
forMainFrameOnly: true)
let userContentController = WKUserContentController()
userContentController.addUserScript(userScript)
let configuration = WKWebViewConfiguration()
configuration.userContentController = userContentController
let webView = WKWebView(frame: .zero,
configuration: configuration)
return webView
}()
You can read more about this approach in this blog post.
Instead of applying css with style tag it's better to apply it with the link tag:
func insertContentsOfCSSFile2(into webView: WKWebView) {
guard let path = Bundle.main.path(forResource: "resource", ofType: "css") else { return }
let csFile = "var head = document.getElementsByTagName('head')[0];var link = document.createElement('link'); link.rel = 'stylesheet';link.type = 'text/css';link.href = '\(path)';link.media = 'all';head.appendChild(link);"
webView.evaluateJavaScript(csFile) {(result, error) in
if let error = error {
print(error)
}
}
}
I have tested it.
it is working fine.
You need to add this header before apple style to HTML
let fontName = UIFont.systemFont(ofSize: 17.0).fontName
let htmlContent = """
<header>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'>
</header>
<style type='text/css'>
img{max-height: 100%; min-height: 100%; height:auto; max-width: 100%; width:auto;margin-bottom:5px;}
p{text-align:left|right|center; line-height: 180%; font-family: '\(fontName)'; font-size: 17px;}
iframe{width:100%; height:250px;}
</style> \(html)
"""
webView.loadHTMLString(htmlContent, baseURL: Bundle.main.bundleURL)
I tried Amer Hukic answer. But did not work just the way it is. I added below code between my html head tags.
<link rel="stylesheet" href="style.css">
See #joern's accepted answer for more complete details. I'm adding this answer because I ran into a weird edge case. My particular use case needed to add styling to a div with a class="dialog". For some reason styling using .dialog and div weren't working though other types of styling were working. In the end I used the following to set the width of the dialog
let width = Int(webView.bounds.width)
let script = "document.getElementsByClassName(\"dialog\")[0].style.width = \"\(width)px\""
webView.evaluateJavaScript(script)