Showing posts with label UIWebView. Show all posts
Showing posts with label UIWebView. Show all posts

Friday, March 4, 2011

How to Center Image when Rotating UIWebView

What happens, when you rotate iPhone while your application shows embedded webpage with custom background graphics? Hopefully the application rotates, too, and hopefully the background image has been designed well (*).

Here is an example how to top center backgroung image while rotating. Please note that the image has been poorly designed ON PURPOSE. Can't make a point with well designed graphics :)

Tuesday, August 24, 2010

How to Show Text Left and Right in UIWebView

When you need to show text on same line at left and right in UIWebView, this is one way:
NSString *myHtml = [NSString stringWithFormat:
    @"<html><body>"
    "<span style='float:left'>%@</span>"
    "<span style='float:right'>%@</span>"
    @"</body></html>",
    @"I'm left", @"I'm right"];
   
[self.cellWebView loadHTMLString:myHtml baseURL:nil];
Can't use div, since it seems to force a linebreak by default.

Monday, May 10, 2010

UIWebView with Custom Background Image

Taking UIWebView into use is pretty easy, customizing is a bit more complicated. Here's how you can define your own image as background.

NSString *myHtml = [NSString stringWithFormat:
                    @"<html><head>"
                    "<style type=\"text/css\">"
                    "body{"
                    "background-image:url(%@);"
                    "}"
                    ",</style>"
                    "</head>"
                    "<body>Hello world</body></html>", imgName];

// baseURL for background image, otherwise nil
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.myWebView loadHTMLString:myHtml baseURL:baseURL];

Thursday, February 25, 2010

How to Disable UIWebView Selection

You could do this by running a short javaScript line at webViewDidFinishLoad, but I find this more elegant: define UIWebView properties in CSS.
NSString *myHtml = [NSString stringWithFormat:
   @"<html><head>"
   "<style type=\"text/css\">"
   // Disable Open/Copy/Cancel alert with long link selection
   "* {"
   "-webkit-touch-callout: none;"
   // Disable selection/Copy of UIWebView
   "-webkit-user-select: none;"
   "}"
   ",</style>"
   "</head><body>%@</body></html>", message.body];
[myWebView loadHTMLString:myHtml baseURL:nil];
As a bonus I left there also how to disable link selection popup.

Wednesday, February 10, 2010

How to Make UIWebView Open target="_blank" Links

There are several ways to do this, here's mine.
NSString *js = @"\
var a = document.getElementsByTagName('a');\
for (var i=0; i<a.length; i++) {\
    a[i].removeAttribute('target');\
}\
";
[self.myWebView stringByEvaluatingJavaScriptFromString:js];
Possible advantage: reduces size of HTML document.