Date post: | 19-May-2015 |
Category: |
Technology |
Upload: | smartlogic |
View: | 2,021 times |
Download: | 1 times |
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