![Page 1: Intro to DTCoreText: Moving Past UIWebView | iOS Development](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/1.jpg)
Intro to DTCoreText
Tom von Schwerdtner
@tvon
www.smartlogicsolutions.com
Moving past UIWebView
![Page 2: Intro to DTCoreText: Moving Past UIWebView | iOS Development](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/2.jpg)
DTCoreText
Tools for using HTML with CoreText“Using HTML without UIWebView”
![Page 3: Intro to DTCoreText: Moving Past UIWebView | iOS Development](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/4.jpg)
UIWebView
![Page 5: Intro to DTCoreText: Moving Past UIWebView | iOS Development](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/7.jpg)
Benefits
Functional
Supports bulk of HTML/CSS
![Page 8: Intro to DTCoreText: Moving Past UIWebView | iOS Development](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/9.jpg)
DTCoreText(DTAttributedTextView)
![Page 10: Intro to DTCoreText: Moving Past UIWebView | iOS Development](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/12.jpg)
Benefits
It’s not a hammer...
“Native”
Bonus: Improved Typography
![Page 13: Intro to DTCoreText: Moving Past UIWebView | iOS Development](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/13.jpg)
Drawbacks
Limited HTML/CSS Support
No Text Selection
![Page 14: Intro to DTCoreText: Moving Past UIWebView | iOS Development](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/14.jpg)
github.com/cocoanetics/DTCoreText
![Page 15: Intro to DTCoreText: Moving Past UIWebView | iOS Development](https://reader035.vdocuments.site/reader035/viewer/2022062405/555ab193d8b42ad0538b50a4/html5/thumbnails/15.jpg)
@smartlogic
facebook.com/smartlogic
github.com/smartlogic