SlideShare a Scribd company logo
Hi-Performance Table
Views with QuartzCore
    and CoreText
  Beginners guide to an advanced concept




              Mugunth Kumar
  Steinlogic Consulting and Training Pte Ltd
About me
About me
    •   Author of the iOS 5, iOS 6 Programming:
        Pushing the Limits book - Reached the top
        100 books in Amazon’s Computer and
        Technology books list

    •   Trainer - Conducts training on iOS
        programming at iOSTraining.sg.

    •   Developer

    •   MKNetworkKit (1200+ watchers)

    •   MKStoreKit (700+ watchers)

    •   Several other “minor” projects with 200+
        watchers

    •   Clients include startups in Singapore like
        Squiryl, Found and MNC’s including
        Microsoft Redmond, Oracle and such.
Why?



•   What makes apps pleasant to use?

    •   Fast scrolling

    •   Why?
Why?
Why?


•   iPhone is a direct manipulation device
Why?


•   iPhone is a direct manipulation device

•   iPhone screen is closer to your eye than your HDTV or your
    computer monitor
Why?


•   iPhone is a direct manipulation device

•   iPhone screen is closer to your eye than your HDTV or your
    computer monitor



•   60 frames per second = 16.66ms per frame
Why?


•   iPhone is a direct manipulation device

•   iPhone screen is closer to your eye than your HDTV or your
    computer monitor



•   60 frames per second = 16.66ms per frame

•   Anything less, you will get a headache
Agenda
Agenda

•   Why?
Agenda

•   Why?

•   Three different methods
Agenda

•   Why?

•   Three different methods

•   Pros and Cons
Agenda

•   Why?

•   Three different methods

•   Pros and Cons

•   QuartzCore/CoreText introduction
Agenda

•   Why?

•   Three different methods

•   Pros and Cons

•   QuartzCore/CoreText introduction

•   A simple table view cell example
Agenda

•   Why?

•   Three different methods

•   Pros and Cons

•   QuartzCore/CoreText introduction

•   A simple table view cell example

•   What else can you build? - Facebook style news feed
Compositing Table View Cells



•   UITableViewCell

    •   Subviews (UILabel, UIImageView)
Pros/Cons
Pros/Cons


•   Advantages

    •   Programmatically easy

    •   Fast for compositing images

    •   Built in cells are rendered differently
Pros/Cons


•   Advantages

    •   Programmatically easy

    •   Fast for compositing images

    •   Built in cells are rendered differently

•   Drawbacks

    •   Slow for text based tables
Direct Drawing



•   UITableViewCell drawRect

    •   NSString -drawInRect, drawAtPoint

    •   UIImage -drawInRect, drawAtPoint
Pros/Cons
Pros/Cons


•   Advantages

    •   Fast

    •   Really fast!
Pros/Cons


•   Advantages

    •   Fast

    •   Really fast!

•   Drawbacks

    •   Difficult (Annoyingly complex to build complex layouts)

    •   CGContextDrawImage is really slow compared to using
        UIImageView
Hybrid




•   A mix of drawRect + UIImageViews
Cons
Cons

•   Still cannot render shadows around images views
Cons

•     Still cannot render shadows around images views


    self.view.layer.masksToBounds = NO;
    self.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.view.layer.shadowOffset = CGSizeMake(0.0f, -1.0f);
    self.view.layer.shadowOpacity = 0.5f;
    self.view.layer.shadowRadius = 1.0f;
Cons

    •     Still cannot render shadows around images views


        self.view.layer.masksToBounds = NO;
        self.view.layer.shadowColor = [UIColor blackColor].CGColor;
        self.view.layer.shadowOffset = CGSizeMake(0.0f, -1.0f);
        self.view.layer.shadowOpacity = 0.5f;
        self.view.layer.shadowRadius = 1.0f;




•       The above code is dog slow.

•       Good for views, very bad for table view cells or collection view
        cells
Is there a better way?



•   QuartzCore.framework



•   CoreText.framework
Pros/Cons
Pros/Cons


•   Advantages

    •   Fast

    •   Can render text and image within our 16ms deadline

    •   Rendering highly customized text is hard
Pros/Cons


•   Advantages

    •   Fast

    •   Can render text and image within our 16ms deadline

    •   Rendering highly customized text is hard




               This is BOLD and this is in italics.
QuartzCore



•   CALayer

•   CATextLayer

•   CAGradientLayer

•   CAShapeLayer
CoreText


•   NSAttributedString

•   NSMutableAttributedString



•   UIBezierPath
Composition


@interface SCTCoreTextCell

@property   (strong,   nonatomic)   CATextLayer *nameTextLayer;
@property   (strong,   nonatomic)   CATextLayer *timeTextLayer;
@property   (strong,   nonatomic)   CALayer *avatarImageLayer;
@property   (strong,   nonatomic)   CALayer *avatarImageShadowLayer;
@property   (strong,   nonatomic)   CATextLayer *descriptionTextLayer;

@end
CALayer - Images

  self.backgroundLayer = [CALayer layer];
  self.backgroundLayer.frame = CGRectMake(0, 0, 320, 150);
  self.backgroundLayer.contentsScale = [[UIScreen mainScreen] scale];
  self.backgroundLayer.actions = [NSDictionary actionsDictionary];
  self.backgroundLayer.contents = (id) backgroundImage.CGImage;

  self.backgroundLayer.contentsCenter = CGRectMake(1.0/
backgroundImage.size.width, 8.0/backgroundImage.size.height,
                                                   1.0/
backgroundImage.size.width,1.0/backgroundImage.size.height);
  self.backgroundLayer.contentsGravity = kCAGravityResize;

  [self.contentView.layer addSublayer:self.backgroundLayer];
CATextLayer - Text


self.nameTextLayer = [CATextLayer layer];

self.nameTextLayer.frame = CGRectMake(65, 3, 240, 21);
self.nameTextLayer.alignmentMode = kCAAlignmentLeft;
self.nameTextLayer.wrapped = YES;
self.nameTextLayer.contentsScale = [[UIScreen mainScreen] scale];

self.nameTextLayer.actions = [NSDictionary actionsDictionary];

[self.contentView.layer addSublayer:self.nameTextLayer];
Composition

+(NSDictionary*) actionsDictionary {

    return @{
      @"onOrderIn" : [NSNull null],
      @"onOrderOut" : [NSNull null],
      @"sublayers" : [NSNull null],
      @"contents" : [NSNull null],
      @"position" : [NSNull null],
      @"bounds" : [NSNull null],
      @"onLayout" : [NSNull null],
      @"hidden" : [NSNull null],
      };
    });
}
Contents
Contents


•   CALayer

    •   Mostly Images

    •   Rendering a graphics context in background
Contents


•   CALayer

    •   Mostly Images

    •   Rendering a graphics context in background
Contents


•   CALayer

    •   Mostly Images

    •   Rendering a graphics context in background



•   CAGradientLayer

    •   Adding gradient backgrounds
Contents
Contents


•   CAShapeLayer

    •   Mostly Paths

    •   Use UIBezierPath to create a path
Contents


•   CAShapeLayer

    •   Mostly Paths

    •   Use UIBezierPath to create a path
Contents


•   CAShapeLayer

    •   Mostly Paths

    •   Use UIBezierPath to create a path



•   CATextLayer (Most useful + complicated)

    •   Text (NSAttributedString)
NSAttributedString



•   The basic building block for complex text rendering



•   NSAttributedString = NSString + Attributes Dictionary
Demo 1 - Simple Bold
Composition
-(void) setText {

  CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)[UIFont
boldSystemFontOfSize:17].fontName, [UIFont boldSystemFontOfSize:17].pointSize, NULL);
  NSDictionary *boldAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                              (__bridge id)ctBoldFont,
                              (id)kCTFontAttributeName,
                              [UIColor blackColor].CGColor,
(id)kCTForegroundColorAttributeName, nil];
  CFRelease(ctBoldFont);

  CTFontRef ctNormalFont = CTFontCreateWithName((__bridge CFStringRef)[UIFont
systemFontOfSize:16].fontName, [UIFont systemFontOfSize:16].pointSize, NULL);
  NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                              (__bridge id)ctNormalFont,
                              (id)kCTFontAttributeName,
                              [UIColor blackColor].CGColor,
(id)kCTForegroundColorAttributeName, nil];
  CFRelease(ctNormalFont);

  NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
initWithString:@"iOS Dev Scout is a AWESOME iOS group!" attributes:normalAttributes];
  [string addAttributes:boldAttributes range:NSMakeRange(19, 7)];
  self.textLayer.string = string;
}
Composition



  CTFontRef ctNormalFont = CTFontCreateWithName((__bridge CFStringRef)[UIFont
systemFontOfSize:16].fontName, [UIFont systemFontOfSize:16].pointSize, NULL);
  NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                              (__bridge id)ctNormalFont,
                              (id)kCTFontAttributeName,
                              [UIColor blackColor].CGColor,
(id)kCTForegroundColorAttributeName, nil];
  CFRelease(ctNormalFont);
Composition



  CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)[UIFont
boldSystemFontOfSize:17].fontName, [UIFont boldSystemFontOfSize:17].pointSize, NULL);
  NSDictionary *boldAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                              (__bridge id)ctBoldFont,
                              (id)kCTFontAttributeName,
                              [UIColor blackColor].CGColor,
(id)kCTForegroundColorAttributeName, nil];
  CFRelease(ctBoldFont);
Composition



  NSMutableAttributedString *string = [[NSMutableAttributedString alloc]
initWithString:@"iOS Dev Scout is a AWESOME iOS group!" attributes:normalAttributes];
  [string addAttributes:boldAttributes range:NSMakeRange(19, 7)];
  self.textLayer.string = string;
What did we use?




•   kCTForegroundColorAttributeName

•   kCTFontAttributeName
What else available?


•   kCTCharacterShapeAttributeName

•   kCTKernAttributeName

•   kCTLigatureAttributeName

•   kCTParagraphStyleAttributeName

•   kCTStrokeWidthAttributeName

•   kCTStrokeColorAttributeName
What else available?

•   kCTSuperscriptAttributeName

•   kCTUnderlineColorAttributeName

•   kCTUnderlineStyleAttributeName

•   kCTVerticalFormsAttributeName

•   kCTGlyphInfoAttributeName

•   kCTRunDelegateAttributeName



•   NSLinkAttributeName (only on Mac)
What else available?

•   And that is just text.



•   Lot more for image rendering



•   Even lot more for animation



•   NSLinkAttributeName not available on iOS. You should look
    at OHAttributedLabel or play around with UIButtons
Demo 2 - Facebook
Performance tips
Performance tips


•   Use dispatch_once for almost any “constants”

    •   UIFont, UIBezierPath, UIColor etc.,
Performance tips


•   Use dispatch_once for almost any “constants”

    •   UIFont, UIBezierPath, UIColor etc.,
Performance tips


•   Use dispatch_once for almost any “constants”

    •   UIFont, UIBezierPath, UIColor etc.,



•   Use strptime* methods instead of NSDateFormatter

    •   No support for locale, but crazy fast
Thanks
       @mugunthkumar
    mugunth@steinlogic.com

         iostraining.sg



    Available for consulting
            services

     iOS App Development
          API Design
          Mobile UX

More Related Content

PDF
Advance text rendering in i os
PDF
iOS 7 SDK特訓班
PDF
Objective-C Is Not Java
PDF
Advanced AV Foundation (CocoaConf, Aug '11)
PDF
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
PPTX
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
PDF
Intro to node.js - Ran Mizrahi (28/8/14)
PDF
From YUI3 to K2
Advance text rendering in i os
iOS 7 SDK特訓班
Objective-C Is Not Java
Advanced AV Foundation (CocoaConf, Aug '11)
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
SenchaCon 2016: Modernizing the Ext JS Class System - Don Griffin
Intro to node.js - Ran Mizrahi (28/8/14)
From YUI3 to K2

What's hot (20)

PDF
Performance Optimization and JavaScript Best Practices
PDF
MFF UK - Advanced iOS Topics
PPT
Javascript and Jquery Best practices
PPTX
From Ruby to Scala
PDF
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
PDF
Solid And Sustainable Development in Scala
PDF
Node.js vs Play Framework (with Japanese subtitles)
PPTX
JavaFX 2 and Scala - Like Milk and Cookies (33rd Degrees)
PDF
FI MUNI 2012 - iOS Basics
PDF
Introduction to node.js by Ran Mizrahi @ Reversim Summit
PDF
Intro To JavaScript Unit Testing - Ran Mizrahi
PDF
Intro to JavaScript Testing
PPT
Practical JRuby
PDF
Creating Dynamic Charts With JFreeChart
PDF
How and Where in GLORP
PPT
jQuery Objects
PDF
[2015/2016] Require JS and Handlebars JS
KEY
Building a real life application in node js
PPTX
SenchaCon 2016: How to Auto Generate a Back-end in Minutes - Per Minborg, Emi...
PDF
Simpler Core Data with RubyMotion
Performance Optimization and JavaScript Best Practices
MFF UK - Advanced iOS Topics
Javascript and Jquery Best practices
From Ruby to Scala
What's a macro?: Learning by Examples / Scalaのマクロに実用例から触れてみよう!
Solid And Sustainable Development in Scala
Node.js vs Play Framework (with Japanese subtitles)
JavaFX 2 and Scala - Like Milk and Cookies (33rd Degrees)
FI MUNI 2012 - iOS Basics
Introduction to node.js by Ran Mizrahi @ Reversim Summit
Intro To JavaScript Unit Testing - Ran Mizrahi
Intro to JavaScript Testing
Practical JRuby
Creating Dynamic Charts With JFreeChart
How and Where in GLORP
jQuery Objects
[2015/2016] Require JS and Handlebars JS
Building a real life application in node js
SenchaCon 2016: How to Auto Generate a Back-end in Minutes - Per Minborg, Emi...
Simpler Core Data with RubyMotion
Ad

Similar to Hi performance table views with QuartzCore and CoreText (20)

PDF
The Great CALayer Tour
PDF
Advanced iOS
PDF
Advanced Imaging on iOS
KEY
iPhone/iPad开发讲座 第五讲 定制视图和多点触摸
PDF
iOS Views
ZIP
Cocoa text talk.key
KEY
Getting Started with CoreGraphics
PDF
MOPCON 2014 - Best software architecture in app development
PDF
아이폰강의(7) pdf
PDF
Core Animation
PDF
Quartz 2D with Swift 3
PDF
iOS Core Animation
PDF
Text Layout With Core Text
PDF
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
KEY
CATiled Layer - Melbourne Cocoheads February 2012
KEY
Implementing CATiledLayer
PDF
MFF UK - Introduction to iOS
PDF
Core Image
KEY
Core animation
PDF
Programming iOS 14 11th Edition Matt Neuburg
The Great CALayer Tour
Advanced iOS
Advanced Imaging on iOS
iPhone/iPad开发讲座 第五讲 定制视图和多点触摸
iOS Views
Cocoa text talk.key
Getting Started with CoreGraphics
MOPCON 2014 - Best software architecture in app development
아이폰강의(7) pdf
Core Animation
Quartz 2D with Swift 3
iOS Core Animation
Text Layout With Core Text
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
CATiled Layer - Melbourne Cocoheads February 2012
Implementing CATiledLayer
MFF UK - Introduction to iOS
Core Image
Core animation
Programming iOS 14 11th Edition Matt Neuburg
Ad

More from Mugunth Kumar (6)

PDF
Using CoreML to Push the Limits of your App
PDF
The new Apple TV and the tvOS
PDF
App store economics
PDF
Designing your API Server for mobile apps
KEY
My experience as a indie consultant
KEY
In App Purchases
Using CoreML to Push the Limits of your App
The new Apple TV and the tvOS
App store economics
Designing your API Server for mobile apps
My experience as a indie consultant
In App Purchases

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
KodekX | Application Modernization Development
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Big Data Technologies - Introduction.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPT
Teaching material agriculture food technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
A Presentation on Artificial Intelligence
NewMind AI Weekly Chronicles - August'25 Week I
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Chapter 3 Spatial Domain Image Processing.pdf
20250228 LYD VKU AI Blended-Learning.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Mobile App Security Testing_ A Comprehensive Guide.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
KodekX | Application Modernization Development
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Big Data Technologies - Introduction.pptx
MYSQL Presentation for SQL database connectivity
Understanding_Digital_Forensics_Presentation.pptx
Empathic Computing: Creating Shared Understanding
Digital-Transformation-Roadmap-for-Companies.pptx
Review of recent advances in non-invasive hemoglobin estimation
Teaching material agriculture food technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Per capita expenditure prediction using model stacking based on satellite ima...
A Presentation on Artificial Intelligence

Hi performance table views with QuartzCore and CoreText

  • 1. Hi-Performance Table Views with QuartzCore and CoreText Beginners guide to an advanced concept Mugunth Kumar Steinlogic Consulting and Training Pte Ltd
  • 3. About me • Author of the iOS 5, iOS 6 Programming: Pushing the Limits book - Reached the top 100 books in Amazon’s Computer and Technology books list • Trainer - Conducts training on iOS programming at iOSTraining.sg. • Developer • MKNetworkKit (1200+ watchers) • MKStoreKit (700+ watchers) • Several other “minor” projects with 200+ watchers • Clients include startups in Singapore like Squiryl, Found and MNC’s including Microsoft Redmond, Oracle and such.
  • 4. Why? • What makes apps pleasant to use? • Fast scrolling • Why?
  • 6. Why? • iPhone is a direct manipulation device
  • 7. Why? • iPhone is a direct manipulation device • iPhone screen is closer to your eye than your HDTV or your computer monitor
  • 8. Why? • iPhone is a direct manipulation device • iPhone screen is closer to your eye than your HDTV or your computer monitor • 60 frames per second = 16.66ms per frame
  • 9. Why? • iPhone is a direct manipulation device • iPhone screen is closer to your eye than your HDTV or your computer monitor • 60 frames per second = 16.66ms per frame • Anything less, you will get a headache
  • 11. Agenda • Why?
  • 12. Agenda • Why? • Three different methods
  • 13. Agenda • Why? • Three different methods • Pros and Cons
  • 14. Agenda • Why? • Three different methods • Pros and Cons • QuartzCore/CoreText introduction
  • 15. Agenda • Why? • Three different methods • Pros and Cons • QuartzCore/CoreText introduction • A simple table view cell example
  • 16. Agenda • Why? • Three different methods • Pros and Cons • QuartzCore/CoreText introduction • A simple table view cell example • What else can you build? - Facebook style news feed
  • 17. Compositing Table View Cells • UITableViewCell • Subviews (UILabel, UIImageView)
  • 19. Pros/Cons • Advantages • Programmatically easy • Fast for compositing images • Built in cells are rendered differently
  • 20. Pros/Cons • Advantages • Programmatically easy • Fast for compositing images • Built in cells are rendered differently • Drawbacks • Slow for text based tables
  • 21. Direct Drawing • UITableViewCell drawRect • NSString -drawInRect, drawAtPoint • UIImage -drawInRect, drawAtPoint
  • 23. Pros/Cons • Advantages • Fast • Really fast!
  • 24. Pros/Cons • Advantages • Fast • Really fast! • Drawbacks • Difficult (Annoyingly complex to build complex layouts) • CGContextDrawImage is really slow compared to using UIImageView
  • 25. Hybrid • A mix of drawRect + UIImageViews
  • 26. Cons
  • 27. Cons • Still cannot render shadows around images views
  • 28. Cons • Still cannot render shadows around images views self.view.layer.masksToBounds = NO; self.view.layer.shadowColor = [UIColor blackColor].CGColor; self.view.layer.shadowOffset = CGSizeMake(0.0f, -1.0f); self.view.layer.shadowOpacity = 0.5f; self.view.layer.shadowRadius = 1.0f;
  • 29. Cons • Still cannot render shadows around images views self.view.layer.masksToBounds = NO; self.view.layer.shadowColor = [UIColor blackColor].CGColor; self.view.layer.shadowOffset = CGSizeMake(0.0f, -1.0f); self.view.layer.shadowOpacity = 0.5f; self.view.layer.shadowRadius = 1.0f; • The above code is dog slow. • Good for views, very bad for table view cells or collection view cells
  • 30. Is there a better way? • QuartzCore.framework • CoreText.framework
  • 32. Pros/Cons • Advantages • Fast • Can render text and image within our 16ms deadline • Rendering highly customized text is hard
  • 33. Pros/Cons • Advantages • Fast • Can render text and image within our 16ms deadline • Rendering highly customized text is hard This is BOLD and this is in italics.
  • 34. QuartzCore • CALayer • CATextLayer • CAGradientLayer • CAShapeLayer
  • 35. CoreText • NSAttributedString • NSMutableAttributedString • UIBezierPath
  • 36. Composition @interface SCTCoreTextCell @property (strong, nonatomic) CATextLayer *nameTextLayer; @property (strong, nonatomic) CATextLayer *timeTextLayer; @property (strong, nonatomic) CALayer *avatarImageLayer; @property (strong, nonatomic) CALayer *avatarImageShadowLayer; @property (strong, nonatomic) CATextLayer *descriptionTextLayer; @end
  • 37. CALayer - Images self.backgroundLayer = [CALayer layer]; self.backgroundLayer.frame = CGRectMake(0, 0, 320, 150); self.backgroundLayer.contentsScale = [[UIScreen mainScreen] scale]; self.backgroundLayer.actions = [NSDictionary actionsDictionary]; self.backgroundLayer.contents = (id) backgroundImage.CGImage; self.backgroundLayer.contentsCenter = CGRectMake(1.0/ backgroundImage.size.width, 8.0/backgroundImage.size.height, 1.0/ backgroundImage.size.width,1.0/backgroundImage.size.height); self.backgroundLayer.contentsGravity = kCAGravityResize; [self.contentView.layer addSublayer:self.backgroundLayer];
  • 38. CATextLayer - Text self.nameTextLayer = [CATextLayer layer]; self.nameTextLayer.frame = CGRectMake(65, 3, 240, 21); self.nameTextLayer.alignmentMode = kCAAlignmentLeft; self.nameTextLayer.wrapped = YES; self.nameTextLayer.contentsScale = [[UIScreen mainScreen] scale]; self.nameTextLayer.actions = [NSDictionary actionsDictionary]; [self.contentView.layer addSublayer:self.nameTextLayer];
  • 39. Composition +(NSDictionary*) actionsDictionary { return @{ @"onOrderIn" : [NSNull null], @"onOrderOut" : [NSNull null], @"sublayers" : [NSNull null], @"contents" : [NSNull null], @"position" : [NSNull null], @"bounds" : [NSNull null], @"onLayout" : [NSNull null], @"hidden" : [NSNull null], }; }); }
  • 41. Contents • CALayer • Mostly Images • Rendering a graphics context in background
  • 42. Contents • CALayer • Mostly Images • Rendering a graphics context in background
  • 43. Contents • CALayer • Mostly Images • Rendering a graphics context in background • CAGradientLayer • Adding gradient backgrounds
  • 45. Contents • CAShapeLayer • Mostly Paths • Use UIBezierPath to create a path
  • 46. Contents • CAShapeLayer • Mostly Paths • Use UIBezierPath to create a path
  • 47. Contents • CAShapeLayer • Mostly Paths • Use UIBezierPath to create a path • CATextLayer (Most useful + complicated) • Text (NSAttributedString)
  • 48. NSAttributedString • The basic building block for complex text rendering • NSAttributedString = NSString + Attributes Dictionary
  • 49. Demo 1 - Simple Bold
  • 50. Composition -(void) setText { CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)[UIFont boldSystemFontOfSize:17].fontName, [UIFont boldSystemFontOfSize:17].pointSize, NULL); NSDictionary *boldAttributes = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)ctBoldFont, (id)kCTFontAttributeName, [UIColor blackColor].CGColor, (id)kCTForegroundColorAttributeName, nil]; CFRelease(ctBoldFont); CTFontRef ctNormalFont = CTFontCreateWithName((__bridge CFStringRef)[UIFont systemFontOfSize:16].fontName, [UIFont systemFontOfSize:16].pointSize, NULL); NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)ctNormalFont, (id)kCTFontAttributeName, [UIColor blackColor].CGColor, (id)kCTForegroundColorAttributeName, nil]; CFRelease(ctNormalFont); NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"iOS Dev Scout is a AWESOME iOS group!" attributes:normalAttributes]; [string addAttributes:boldAttributes range:NSMakeRange(19, 7)]; self.textLayer.string = string; }
  • 51. Composition CTFontRef ctNormalFont = CTFontCreateWithName((__bridge CFStringRef)[UIFont systemFontOfSize:16].fontName, [UIFont systemFontOfSize:16].pointSize, NULL); NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)ctNormalFont, (id)kCTFontAttributeName, [UIColor blackColor].CGColor, (id)kCTForegroundColorAttributeName, nil]; CFRelease(ctNormalFont);
  • 52. Composition CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)[UIFont boldSystemFontOfSize:17].fontName, [UIFont boldSystemFontOfSize:17].pointSize, NULL); NSDictionary *boldAttributes = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)ctBoldFont, (id)kCTFontAttributeName, [UIColor blackColor].CGColor, (id)kCTForegroundColorAttributeName, nil]; CFRelease(ctBoldFont);
  • 53. Composition NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"iOS Dev Scout is a AWESOME iOS group!" attributes:normalAttributes]; [string addAttributes:boldAttributes range:NSMakeRange(19, 7)]; self.textLayer.string = string;
  • 54. What did we use? • kCTForegroundColorAttributeName • kCTFontAttributeName
  • 55. What else available? • kCTCharacterShapeAttributeName • kCTKernAttributeName • kCTLigatureAttributeName • kCTParagraphStyleAttributeName • kCTStrokeWidthAttributeName • kCTStrokeColorAttributeName
  • 56. What else available? • kCTSuperscriptAttributeName • kCTUnderlineColorAttributeName • kCTUnderlineStyleAttributeName • kCTVerticalFormsAttributeName • kCTGlyphInfoAttributeName • kCTRunDelegateAttributeName • NSLinkAttributeName (only on Mac)
  • 57. What else available? • And that is just text. • Lot more for image rendering • Even lot more for animation • NSLinkAttributeName not available on iOS. You should look at OHAttributedLabel or play around with UIButtons
  • 58. Demo 2 - Facebook
  • 60. Performance tips • Use dispatch_once for almost any “constants” • UIFont, UIBezierPath, UIColor etc.,
  • 61. Performance tips • Use dispatch_once for almost any “constants” • UIFont, UIBezierPath, UIColor etc.,
  • 62. Performance tips • Use dispatch_once for almost any “constants” • UIFont, UIBezierPath, UIColor etc., • Use strptime* methods instead of NSDateFormatter • No support for locale, but crazy fast
  • 63. Thanks @mugunthkumar mugunth@steinlogic.com iostraining.sg Available for consulting services iOS App Development API Design Mobile UX