Skip to content

Instantly share code, notes, and snippets.

@danielphillips
Created April 28, 2011 14:06
Show Gist options
  • Select an option

  • Save danielphillips/946404 to your computer and use it in GitHub Desktop.

Select an option

Save danielphillips/946404 to your computer and use it in GitHub Desktop.

Revisions

  1. Daniel Phillips revised this gist Jun 9, 2011. 5 changed files with 0 additions and 9 deletions.
    1 change: 0 additions & 1 deletion DJPWebView.h
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    #import <Foundation/Foundation.h>
    #import "DJPWebViewDelegate.h"
    #import "DJPWebViewContent.h"

    1 change: 0 additions & 1 deletion DJPWebView.m
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    #import "DJPWebView.h"


    @implementation DJPWebView

    @synthesize content;
    3 changes: 0 additions & 3 deletions DJPWebViewContent.h
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,3 @@
    #import <Foundation/Foundation.h>


    @interface DJPWebViewContent : NSObject {
    NSMutableString* css;
    NSMutableString* html;
    3 changes: 0 additions & 3 deletions DJPWebViewDelegate.h
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,3 @@
    #import <Foundation/Foundation.h>


    @interface DJPWebViewDelegate : NSObject <UIWebViewDelegate, UIAlertViewDelegate> {
    NSString* clickedLink;
    }
    1 change: 0 additions & 1 deletion DJPWebViewDelegate.m
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    #import "DJPWebViewDelegate.h"


    @implementation DJPWebViewDelegate

    -(id)init{
  2. Daniel Phillips revised this gist Apr 28, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion DJPWebViewContent.m
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ -(id)init{
    self = [super init];
    if(self){
    css = [[NSMutableString alloc] initWithString:@"* {-webkit-user-select: none;}"];
    [css appendString:[NSString stringWithFormat:@"a:link {color:#3DB6B6; text-decoration:none}", WEBVIEW_LINK_COLOR]];
    [css appendString:[NSString stringWithFormat:@"a:link {color:%@; text-decoration:none}", WEBVIEW_LINK_COLOR]];
    [css appendString:[NSString stringWithFormat:@"body { "
    "margin:0;"
    "padding:0;"
  3. Daniel Phillips revised this gist Apr 28, 2011. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion DJPWebViewContent.m
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,15 @@ -(id)init{
    if(self){
    css = [[NSMutableString alloc] initWithString:@"* {-webkit-user-select: none;}"];
    [css appendString:[NSString stringWithFormat:@"a:link {color:#3DB6B6; text-decoration:none}", WEBVIEW_LINK_COLOR]];
    [css appendString:[NSString stringWithFormat:@"body { margin:0; padding:0; font-family: \"%@\"; font-size:%d; width:290px; color:gray}", WEBVIEW_TEXT_FONT, [[NSNumber numberWithFloat:WEBVIEW_TEXT_SIZE] intValue]]];
    [css appendString:[NSString stringWithFormat:@"body { "
    "margin:0;"
    "padding:0;"
    "font-family: \"%@\";"
    "font-size:%d;"
    "width:290px;"
    "color:gray}",
    WEBVIEW_TEXT_FONT,
    [[NSNumber numberWithFloat:WEBVIEW_TEXT_SIZE] intValue]]];
    [css appendString:[NSString stringWithFormat:@"strong { font-family: \"%@\"}", WEBVIEW_BOLD_FONT]];
    [css appendString:@"p { margin:0;}"];
    [css appendString:@"p+p { margin-top:15px;}"];
  4. Daniel Phillips revised this gist Apr 28, 2011. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions DJPWebViewDelegate.m
    Original file line number Diff line number Diff line change
    @@ -31,10 +31,10 @@ -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)in
    }

    UIAlertView* av = [[UIAlertView alloc] initWithTitle:alertText
    message:clickedLink
    delegate:self
    cancelButtonTitle:@"Cancel"
    otherButtonTitles:@"OK", nil];
    message:clickedLink
    delegate:self
    cancelButtonTitle:@"Cancel"
    otherButtonTitles:@"OK", nil];
    [av show];
    [av release];
    if(tel){
  5. Daniel Phillips revised this gist Apr 28, 2011. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions DJPWebViewContent.m
    Original file line number Diff line number Diff line change
    @@ -28,11 +28,11 @@ -(void)appendHTMLBody:(NSString*)markup{

    -(NSString*)getContentsOfPage{
    return [NSString stringWithFormat:@"<html>"
    "<head>"
    "<style type=\"text/css\">%@</style>"
    "</head>"
    "<body>%@</body>"
    "</html>", css, html];
    "<head>"
    "<style type=\"text/css\">%@</style>"
    "</head>"
    "<body>%@</body>"
    "</html>", css, html];
    }

    @end
  6. Daniel Phillips revised this gist Apr 28, 2011. 2 changed files with 11 additions and 2 deletions.
    7 changes: 6 additions & 1 deletion DJPWebViewContent.m
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,12 @@ -(void)appendHTMLBody:(NSString*)markup{
    }

    -(NSString*)getContentsOfPage{
    return [NSString stringWithFormat:@"<html><head><style type=\"text/css\">%@</style></head><body>%@</body></html>", css, html];
    return [NSString stringWithFormat:@"<html>"
    "<head>"
    "<style type=\"text/css\">%@</style>"
    "</head>"
    "<body>%@</body>"
    "</html>", css, html];
    }

    @end
    6 changes: 5 additions & 1 deletion DJPWebViewDelegate.m
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,11 @@ -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)in
    alertText = @"Follow link";
    }

    UIAlertView* av = [[UIAlertView alloc] initWithTitle:alertText message:clickedLink delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    UIAlertView* av = [[UIAlertView alloc] initWithTitle:alertText
    message:clickedLink
    delegate:self
    cancelButtonTitle:@"Cancel"
    otherButtonTitles:@"OK", nil];
    [av show];
    [av release];
    if(tel){
  7. Daniel Phillips created this gist Apr 28, 2011.
    14 changes: 14 additions & 0 deletions DJPWebView.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    #import <Foundation/Foundation.h>
    #import "DJPWebViewDelegate.h"
    #import "DJPWebViewContent.h"

    @interface DJPWebView : UIWebView {
    DJPWebViewDelegate* delegateObject;
    DJPWebViewContent* content;
    }

    @property(nonatomic, retain)DJPWebViewContent* content;

    -(void)display;

    @end
    32 changes: 32 additions & 0 deletions DJPWebView.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #import "DJPWebView.h"


    @implementation DJPWebView

    @synthesize content;

    -(id)init{
    self = [super init];
    if(self){
    delegateObject = [[DJPWebViewDelegate alloc] init];
    [super setDelegate:delegateObject];

    ((UIScrollView*)[[super subviews] objectAtIndex:0]).bounces = NO;

    [super setUserInteractionEnabled:YES];

    content = [[DJPWebViewContent alloc] init];
    }
    return self;
    }

    -(void)display{
    [super loadHTMLString:[content getContentsOfPage] baseURL:[NSURL URLWithString:@""]];
    }

    -(void)dealloc{
    [delegateObject release];
    [super dealloc];
    }

    @end
    13 changes: 13 additions & 0 deletions DJPWebViewContent.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    #import <Foundation/Foundation.h>


    @interface DJPWebViewContent : NSObject {
    NSMutableString* css;
    NSMutableString* html;
    }

    -(void)appendCSSRule:(NSString*)style;
    -(void)appendHTMLBody:(NSString*)markup;
    -(NSString*)getContentsOfPage;

    @end
    33 changes: 33 additions & 0 deletions DJPWebViewContent.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #import "DJPWebViewContent.h"
    #import "DJPWebViewSettings.h"

    @implementation DJPWebViewContent

    -(id)init{
    self = [super init];
    if(self){
    css = [[NSMutableString alloc] initWithString:@"* {-webkit-user-select: none;}"];
    [css appendString:[NSString stringWithFormat:@"a:link {color:#3DB6B6; text-decoration:none}", WEBVIEW_LINK_COLOR]];
    [css appendString:[NSString stringWithFormat:@"body { margin:0; padding:0; font-family: \"%@\"; font-size:%d; width:290px; color:gray}", WEBVIEW_TEXT_FONT, [[NSNumber numberWithFloat:WEBVIEW_TEXT_SIZE] intValue]]];
    [css appendString:[NSString stringWithFormat:@"strong { font-family: \"%@\"}", WEBVIEW_BOLD_FONT]];
    [css appendString:@"p { margin:0;}"];
    [css appendString:@"p+p { margin-top:15px;}"];

    html = [[NSMutableString alloc] initWithString:@""];
    }
    return self;
    }

    -(void)appendCSSRule:(NSString*)style{
    [css appendString:style];
    }

    -(void)appendHTMLBody:(NSString*)markup{
    [html appendString:markup];
    }

    -(NSString*)getContentsOfPage{
    return [NSString stringWithFormat:@"<html><head><style type=\"text/css\">%@</style></head><body>%@</body></html>", css, html];
    }

    @end
    8 changes: 8 additions & 0 deletions DJPWebViewDelegate.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    #import <Foundation/Foundation.h>


    @interface DJPWebViewDelegate : NSObject <UIWebViewDelegate, UIAlertViewDelegate> {
    NSString* clickedLink;
    }

    @end
    65 changes: 65 additions & 0 deletions DJPWebViewDelegate.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #import "DJPWebViewDelegate.h"


    @implementation DJPWebViewDelegate

    -(id)init{
    self = [super init];
    if(self){
    clickedLink = [[NSString alloc] init];
    }
    return self;
    }

    -(void)dealloc{
    [clickedLink release];
    [super dealloc];
    }

    -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if ( inType == UIWebViewNavigationTypeLinkClicked ) {
    clickedLink = [NSString stringWithFormat:@"%@",[inRequest URL]];
    NSString* alertText;
    BOOL tel = NO;
    if([[clickedLink substringToIndex:3] isEqualToString:@"tel"]){
    alertText = @"Call";
    clickedLink = [clickedLink substringFromIndex:4];
    clickedLink = [clickedLink stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
    tel = YES;
    }else{
    alertText = @"Follow link";
    }

    UIAlertView* av = [[UIAlertView alloc] initWithTitle:alertText message:clickedLink delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [av show];
    [av release];
    if(tel){
    clickedLink = [clickedLink stringByReplacingOccurrencesOfString:@" " withString:@""];
    clickedLink = [[NSString stringWithFormat:@"tel:%@", clickedLink] retain];
    }
    return NO;
    }

    return YES;
    }

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    switch (buttonIndex) {
    case 0:
    {
    NSLog(@"user cancelled");
    }
    break;
    case 1:
    {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:clickedLink]];
    }
    break;

    default:
    break;
    }
    }

    @end
    4 changes: 4 additions & 0 deletions DJPWebViewSettings.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    #define WEBVIEW_LINK_COLOR @"#3DB6B6"
    #define WEBVIEW_TEXT_FONT @"Avant Guard IOS"
    #define WEBVIEW_TEXT_SIZE 12.5f
    #define WEBVIEW_BOLD_FONT @"Avant Garde Demi IOS"