intro to dtcoretext: moving past uiwebview | ios development

15
Intro to DTCoreText Tom von Schwerdtner @tvon www.smartlogicsolutions.com Moving past UIWebView

Upload: smartlogic

Post on 19-May-2015

2.021 views

Category:

Technology


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Intro to DTCoreText: Moving Past UIWebView | iOS Development

Intro to DTCoreText

Tom von Schwerdtner

@tvon

www.smartlogicsolutions.com

Moving past UIWebView

Page 2: Intro to DTCoreText: Moving Past UIWebView | iOS Development

DTCoreText

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

Page 3: Intro to DTCoreText: Moving Past UIWebView | iOS Development

Why?

HTML is ubiquitous

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

How to present this to users?

Page 4: Intro to DTCoreText: Moving Past UIWebView | iOS Development

UIWebView

Page 5: Intro to DTCoreText: Moving Past UIWebView | iOS Development

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;

Page 6: Intro to DTCoreText: Moving Past UIWebView | iOS Development

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;}

Page 7: Intro to DTCoreText: Moving Past UIWebView | iOS Development

Benefits

Functional

Supports bulk of HTML/CSS

Page 8: Intro to DTCoreText: Moving Past UIWebView | iOS Development

Drawbacks

When all you have is a hammer...

Need to adjust text with CSS

Hackish

Page 9: Intro to DTCoreText: Moving Past UIWebView | iOS Development

DTCoreText(DTAttributedTextView)

Page 10: Intro to DTCoreText: Moving Past UIWebView | iOS Development

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;

Page 11: Intro to DTCoreText: Moving Past UIWebView | iOS Development

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];}

Page 12: Intro to DTCoreText: Moving Past UIWebView | iOS Development

Benefits

It’s not a hammer...

“Native”

Bonus: Improved Typography

Page 13: Intro to DTCoreText: Moving Past UIWebView | iOS Development

Drawbacks

Limited HTML/CSS Support

No Text Selection

Page 14: Intro to DTCoreText: Moving Past UIWebView | iOS Development

github.com/cocoanetics/DTCoreText

Page 15: Intro to DTCoreText: Moving Past UIWebView | iOS Development

@smartlogic

facebook.com/smartlogic

github.com/smartlogic