iOS Question how to change the background color in an html file

kisoft

Well-Known Member
Licensed User
Longtime User
Hi
how to change the background color in an html file. In b4a I could do it with webview extras and Javascript

code B4A:
B4X:
    Private WebView1 As WebView
    Private wve As WebViewExtras
    Dim JSClient As DefaultJavascriptInterface
  
    JSClient.Initialize
    wve.Initialize(WebView1)
    wve.AddJavascriptInterface(JSClient,"B4A")
    wve.LoadUrl(xui.FileUri(File.DirAssets,"myfile.html"))

Sub WebView1_PageFinished (Url As String)
  wve.ExecuteJavascript("document.body.style.backgroundColor = '#DDC8B0'")
End If
 

kisoft

Well-Known Member
Licensed User
Longtime User
hi
@Brandsum, Thank you for your help so far. Unfortunately, I can't check it out right now, but shouldn't it be like that?
B4X:
WebView1.LoadHtml(File.ReadString(File.DirAssets,"myfile.html"))
          WKWebView1.EvaluateJavaScript(WebView1,"document.body.style.backgroundColor ='#DDC8B0'")
 
Upvote 0

Brandsum

Well-Known Member
Licensed User
No. WkWebview is itself a webview. So load html file into WkWebview.

It's true that EvaluateJavascript takes two parameters as input, but the first one is a tag object which will be returned in JsComplete event so that you can track.
https://www.b4x.com/b4i/help/iui8.html#wkwebview_evaluatejavascript

So the correct code is,
B4X:
WKWebView1.EvaluateJavaScript("bgcolor","document.body.style.backgroundColor = '#DDC8B0'")

Sub WKWebView1_JSComplete (Success As Boolean, Tag As Object, Result As String)
    If Success And Tag = "bgcolor" Then Log("Background color has been changed")
End Sub
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
Thank you for your answers.
It does not work. Moreover, the font size of the text in the wkwebview view is much smaller than in the webview view.
 
Last edited:
Upvote 0

Brandsum

Well-Known Member
Licensed User
It does not work.

Maybe there is any specific CSS (background color) applied to another tag containing all the other tags. You need to find that tag and apply background color to that tag.

Moreover, the font size of the text in the wkwebview view is much smaller than in the webview view.
Add this following tag inside head tag of your html file.
HTML:
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
 
Upvote 0
Top