0

I am getting a content like this from my web service

<Strong>some unicode characters</Strong>
<iframe src="https://www.youtube.com/embed/<some_ID>" width="640" height="480" frameborder="0" allowfullscreen="allowfullscreen"></iframe>

When I load this string directly to the UIWebView it is black. But I want to make it in white color.

  1. How to change the color of this in swift
  2. Also need to change the width of this video to current UIView width

How can I do this?

Code Different
  • 90,614
  • 16
  • 144
  • 163
user1960169
  • 3,533
  • 12
  • 39
  • 61
  • Seems duplicate, refer this - http://stackoverflow.com/questions/25607247/how-do-i-decode-html-entities-in-swift – Santosh Jun 24 '16 at 02:01

1 Answers1

0

You can use Javascript to add a bit of style and resize the video:

override func viewDidLoad() {
    super.viewDidLoad()

    let html = "..."
    webView.delegate = self
    webView.loadHTMLString(html, baseURL: nil)
}

func webViewDidFinishLoad(webView: UIWebView) {
    let url = NSBundle.mainBundle().URLForResource("script", withExtension: "js")!
    let javascript = try! String(contentsOfURL: url)

    webView.stringByEvaluatingJavaScriptFromString(javascript)
}

And the content of script.js:

var title = document.body.getElementsByTagName('strong')[0]
title.style.color = 'blue'
title.style.display = 'block'

var iframe = document.body.getElementsByTagName('iframe')[0]
iframe.style.width = '100%'
iframe.height = document.body.offsetHeight - title.offsetHeight
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • Thanks :) work perfectly for the color. But my video dissappear when web view finished loading? What would be the reason for this? – user1960169 Jun 24 '16 at 02:31