intro to dtcoretext: moving past uiwebview | ios development

Post on 19-May-2015

2.021 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Check out this intro to DTCoreText which will help you move past UIWebView. Improve your iOS development skills.

TRANSCRIPT

Intro to DTCoreText

Tom von Schwerdtner

@tvon

www.smartlogicsolutions.com

Moving past UIWebView

DTCoreText

Tools for using HTML with CoreText“Using HTML without UIWebView”

Why?

HTML is ubiquitous

(I don’t really think I need to convince anyone of this)

How to present this to users?

UIWebView

UIWebView

// Common to all implementationsNSString* filePath = [[NSBundle mainBundle] pathForResource:@"content" ofType:@"html"];NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

// Wrap our HTML content in the default native font face and sizeNSString *wrappedHTMLString = [NSString stringWithFormat:@"" "<style type='text/css'>" "* { font-family: helvetica; font-size: 12px; }" "</style>" "%@", htmlString ];

[self.webView loadHTMLString:wrappedHTMLString baseURL:nil];

// Remove the shadow that is displayed when scrolling past the boundsfor (UIView *shadowView in self.webView.scrollView.subviews) { if ([shadowView isKindOfClass:[UIImageView class]]) { [shadowView setHidden:YES]; }}

// Assign our delegate so we can handle opening links in Mobile Safariself.webView.delegate = self;

UIWebView

#pragma mark - UIWebViewDelegate

// Open external links from this UIWebView in MobileSafari- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ if(navigationType == UIWebViewNavigationTypeLinkClicked) { [[UIApplication sharedApplication] openURL:[request URL]]; return NO; } return YES;}

Benefits

Functional

Supports bulk of HTML/CSS

Drawbacks

When all you have is a hammer...

Need to adjust text with CSS

Hackish

DTCoreText(DTAttributedTextView)

DTAttributedTextView

// Common to all implementations NSString* filePath = [[NSBundle mainBundle] pathForResource:@"content" ofType:@"html"];NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

// Set our builder to use the default native font face (uses Times otherwise)NSDictionary *builderOptions = @{ DTDefaultFontFamily: @"Helvetica" };

DTHTMLAttributedStringBuilder *stringBuilder = [[DTHTMLAttributedStringBuilder alloc] initWithHTML:htmlData options:builderOptions documentAttributes:nil];

self.textView.attributedString = [stringBuilder generatedAttributedString];

// Assign our delegate, this is required to handle link eventsself.textView.textDelegate = self;

DTAttributedTextView

#pragma mark - DTAttributedTextContentViewDelegate

- (UIView *)attributedTextContentView:(DTAttributedTextContentView *)attributedTextContentView viewForLink:(NSURL *)url identifier:(NSString *)identifier frame:(CGRect)frame{ DTLinkButton *linkButton = [[DTLinkButton alloc] initWithFrame:frame]; linkButton.URL = url; [linkButton addTarget:self action:@selector(linkButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; return linkButton;}

#pragma mark - Events

- (IBAction)linkButtonClicked:(DTLinkButton *)sender{ [[UIApplication sharedApplication] openURL:sender.URL];}

Benefits

It’s not a hammer...

“Native”

Bonus: Improved Typography

Drawbacks

Limited HTML/CSS Support

No Text Selection

github.com/cocoanetics/DTCoreText

@smartlogic

facebook.com/smartlogic

github.com/smartlogic

top related