Created
April 28, 2011 14:06
-
-
Save danielphillips/946404 to your computer and use it in GitHub Desktop.
UILabel rich text solution using UIWebView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "DJPWebViewDelegate.h" | |
#import "DJPWebViewContent.h" | |
@interface DJPWebView : UIWebView { | |
DJPWebViewDelegate* delegateObject; | |
DJPWebViewContent* content; | |
} | |
@property(nonatomic, retain)DJPWebViewContent* content; | |
-(void)display; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface DJPWebViewContent : NSObject { | |
NSMutableString* css; | |
NSMutableString* html; | |
} | |
-(void)appendCSSRule:(NSString*)style; | |
-(void)appendHTMLBody:(NSString*)markup; | |
-(NSString*)getContentsOfPage; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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:%@; 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface DJPWebViewDelegate : NSObject <UIWebViewDelegate, UIAlertViewDelegate> { | |
NSString* clickedLink; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTES
Example usage: