ios - How to vertically center the text in WKWebView? -


i using wkwebview in ios (swift) app , using loadhtmlstring(string, baseurl: ). problem want html vertically centered in frame. text variable in length. how can that?

this screenshot of html xcode:

figure 1

this image of output. top html on upper side, want centrally aligned in view (the view 300 points top):

figure 2

try using html inline css, can replace stuff in between divs want centered:

  <div style="display:flex;align-items:center;height:100%;justify-content:center;">     <p>you content here</p>   </div> 

this code works me using swift 3.0 on iphone 6 simulator

import uikit import webkit  class viewcontroller: uiviewcontroller {      var webview:wkwebview?      override func viewdidload() {         super.viewdidload()     }      override func viewdidappear(_ animated: bool) {         webview = wkwebview(frame: self.view.frame)         self.view.addsubview(webview!);         addhtml()     }      func addhtml() {         let html = "<html><body><div style='height:100%;background-color:black;display:flex;align-items:center;justify-content:center;'><p style='color:white;font-size:100px;'>asleepace</p></div></body></html>";         webview!.loadhtmlstring(html, baseurl: nil)     } } 

example output

enter image description here


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -