Skip to content

Instantly share code, notes, and snippets.

@danielphillips
Created April 28, 2011 14:06
Show Gist options
  • Save danielphillips/946404 to your computer and use it in GitHub Desktop.
Save danielphillips/946404 to your computer and use it in GitHub Desktop.
UILabel rich text solution using UIWebView
#import "DJPWebViewDelegate.h"
#import "DJPWebViewContent.h"
@interface DJPWebView : UIWebView {
DJPWebViewDelegate* delegateObject;
DJPWebViewContent* content;
}
@property(nonatomic, retain)DJPWebViewContent* content;
-(void)display;
@end
#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
@interface DJPWebViewContent : NSObject {
NSMutableString* css;
NSMutableString* html;
}
-(void)appendCSSRule:(NSString*)style;
-(void)appendHTMLBody:(NSString*)markup;
-(NSString*)getContentsOfPage;
@end
#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
@interface DJPWebViewDelegate : NSObject <UIWebViewDelegate, UIAlertViewDelegate> {
NSString* clickedLink;
}
@end
#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
#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"
@danielphillips
Copy link
Author

NOTES

  • You can use a custom font by adding the .ttf file to your project and adding the filename of the font to the info.plist for the property "Fonts provided by application" (Array). To use the font, you need to know the font's name (this can be different from the fonts file base name).
  • You need to preview the output and manually provide the correct height to use for setting the frame.

Example usage:

DJPWebView* webview = [[DJPWebView alloc] init];
[webview.content appendHTML:@"<p>Hello <strong>world</strong> !</p>"];
[webview display];
[webview setFrame:CGRectMake(15, 0, 290, 25)];
[self.view addSubview:webview];
[webview release];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment