0

Swift - How to set the UIWebView's background color to be the same as the html page loaded in this UIWebView? Note: the background of html is loaded from style.css like:

body {
 background: -webkit-linear-gradient(left, #fff, #F7F6FF);
}

Note2: there is many of html pages with different styles.css are loaded in the same UIWebView . So because that when I scroll down I want to see the same color behind the html page . thx :)

Hassan Taleb
  • 2,350
  • 2
  • 19
  • 24
  • You cannot set UIWebView background color directly, it does not work. Instead use this approach: http://stackoverflow.com/questions/1547102/cant-change-background-for-uiwebview-in-iphone-sdk – emrys57 Mar 30 '15 at 14:40

1 Answers1

0

go plain, use UIColor and set background.

here some utilities ported to swift:

go plain, use UIColor, setting background...

Here some utilities ported to swift:

func colorFromHTML(HEXColor: String?)->UIColor?{
    if HEXColor == nil{
        return UIColor.grayColor()
    }

    var rgbValue: UInt32 = 0
    let scanner = NSScanner(string: HEXColor!)
    scanner.scanLocation = 1    // bypass '#' character
    scanner.scanHexInt(&rgbValue)

    let c : UIColor = UIColorFromRGB(rgbValue)
    return c
}


func UIColorFromRGB(rgbValue: UInt32)-> UIColor{
    let red : CGFloat = CGFloat( (rgbValue & 0xFF0000) >> 16 )
    let green : CGFloat = CGFloat( (rgbValue & 0xFF00) >> 8 )
    let blue : CGFloat = CGFloat(rgbValue & 0xFF)

    let c = UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: 1)

    return c
}
ingconti
  • 10,876
  • 3
  • 61
  • 48