Advanced Cocoa Text Tips and Tricks. Aki Inoue Cocoa Engineer

Similar documents
ASSIGNMENT 5 TIPS AND TRICKS

CS193E Lecture 12. Formatters Cocoa Text More View Drawing

Fundamentals of Programming (C)

Chapter 8. Characters and Strings

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

EXPERIMENT 8: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Chapter 3. Information Representation

Connecting UniOP to Datalogic Barcode Readers

Data Representation and Binary Arithmetic. Lecture 2

Chapter 2 Bits, Data Types, and Operations

Lecture (09) x86 programming 8

Chapter 2 Bits, Data Types, and Operations

EXPERIMENT 7: Introduction to Universal Serial Asynchronous Receive Transmit (USART)

Fundamentals of Programming

Chapter 2 Bits, Data Types, and Operations

CS341 *** TURN OFF ALL CELLPHONES *** Practice NAME

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

4/14/2015. Architecture of the World Wide Web. During this session we will discuss: Structure of the World Wide Web

The Binary Number System

Binary Numbers. The Basics. Base 10 Number. What is a Number? = Binary Number Example. Binary Number Example

Adopting Advanced Features of the New UI

Watch the following for more announcements

Chapter 2 Bits, Data Types, and Operations

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Accessibility on OS X

What s New in NSCollectionView Session 225

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

5/17/2009. Digitizing Discrete Information. Ordering Symbols. Analog vs. Digital

DATA REPRESENTATION. Data Types. Complements. Fixed Point Representations. Floating Point Representations. Other Binary Codes. Error Detection Codes

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

PureScan - ML1. Configuration Guide. Wireless Linear Imager Wireless Laser scanner - 1 -

+ Inheritance. n Superclass (base class) higher in the hierarchy. n Subclass (child class) lower in the hierarchy

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

Do not start the test until instructed to do so!

Number Representations

Bits and Bytes. Data Representation. A binary digit or bit has a value of either 0 or 1; these are the values we can store in hardware devices.

FD-011WU. 2D Barcode Reader User Guide V1.6CC

Number Systems II MA1S1. Tristan McLoughlin. November 30, 2013

EE 109 Unit 2. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

Core Animation. Building Animated UI s. Bill Dudney. Gala Factory Software LLC. Bill Dudney Core Animation: Building Animated UI s Slide 1

2a. Codes and number systems (continued) How to get the binary representation of an integer: special case of application of the inverse Horner scheme

Number System (Different Ways To Say How Many) Fall 2016

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

EE 109 Unit 3. Analog vs. Digital. Analog vs. Digital. Binary Representation Systems ANALOG VS. DIGITAL

S-Series Sensor ASCII Protocol v8.1.0

1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1

Hardware. ( Not so hard really )

Do not start the test until instructed to do so!

Chapter 2 Number System

2D BARCODE SCANNER CA-SC-20200B

Chapter 1. Hardware. Introduction to Computers and Programming. Chapter 1.2

User s Manual. Xi3000 Scanner. Table of Contents

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

Unit 3. Analog vs. Digital. Analog vs. Digital ANALOG VS. DIGITAL. Binary Representation

Chapter 2 Bits, Data Types, and Operations

CPSC 301: Computing in the Life Sciences Lecture Notes 16: Data Representation

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

Variables and data types

Introduction. Chapter 1. Hardware. Introduction. Creators of Software. Hardware. Introduction to Computers and Programming (Fall 2015, CSUS)

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,

What s New in Cocoa for macos

3.1. Unit 3. Binary Representation

marson MT8200S 2D Handheld Scanner User Manual V / 6 / 25 - I -

Simple Data Types in C. Alan L. Cox

Characters Lesson Outline

Table of Contents Sleep Settings How to Configure the Scanner. 7 Chapter 2 System Setup

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

Do not start the test until instructed to do so!

Setup Procedures 2 Batch Setup 3. Bar Code Setup Menu 5. 1 Device Selection and Default. 2 Beep and Delay Keyboard Wedge..

Experiment 3. TITLE Optional: Write here the Title of your program.model SMALL This directive defines the memory model used in the program.

Midterm CSE 131 Winter 2014

Menu & commands booklet FOR BAR CODE SCANNERS * *

RS-422 Code-Operated Switches

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, SPRING 2013

^BC Code 128 Bar Code (Subsets A, B, and C)

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Configuration Manual PULSAR C CCD SCANNER. Table of Contents

Xi2000-BT Series Configuration Guide

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 02, FALL 2012

ios Mobile Development

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

Programmable #182 Parallel Interface Cash Drawer Manual

Universal Asynchronous Receiver Transmitter Communication

Fundamental Data Types

Midterm CSE 131 Fall 2014

Source coding and compression

Mounting Dimensions / Viewing 2 Mounting Options 3. Wiring Configuration 4. Quick Set up Procedure 5. Changing Intensity 6.

CSE-1520R Test #1. The exam is closed book, closed notes, and no aids such as calculators, cellphones, etc.

Number Systems Base r

Oberon Data Types. Matteo Corti. December 5, 2001

EE 109 Unit 2. Binary Representation Systems

APPENDIX B. ASSEMBLER DIRECTIVE SUMMARY

MK D Imager Barcode Scanner Configuration Guide

OOstaExcel.ir. J. Abbasi Syooki. HTML Number. Device Control 1 (oft. XON) Device Control 3 (oft. Negative Acknowledgement

Transcription:

Advanced Cocoa Text Tips and Tricks Aki Inoue Cocoa Engineer 2

Introduction Only on Mac OS Diving deeper Understanding the layout process Getting comfortable with extending and customizing base functionalities 3

Prerequisite Download Required for This Session Advanced Cocoa Text Tips and Tricks To be ready for this session: Open WWDC_2010_114.xcodeproj Materials available at: http://developer.apple.com/wwdc/attendee/ 4

What You ll Learn Multiple text view linking Line numbering and ruler view customization Line folding Providing inline UI widgets for user interaction Text animation Features found in advanced text editors 5

Cocoa Text System Easy to access Designed to scale Advanced typography built in MVC design pattern Many customization points Model NSTextViewDelegate -addlayoutmanager: NSTextContainer! -addtextcontainer: View Controller 6

Cocoa Text System The Cocoa Text System primary classes NSTextView View NSLayoutManager Controller NSTextStorage NSTextContainer Model 7

Customizing with Primary Text Classes Flexible MVC configuration Multipage, multipane, multicolumn, multiframe, etc. 8

Customizing with Primary Text Classes Flexible MVC configuration Rich delegation interface points NSTextDelegate, NSTextViewDelegate, NSLayoutManagerDelegate, etc. Editing validation, mouse handling, pasteboard interface overrides, selection validation, typing attributes validation, key command overrides, context menu overrides 9

Customizing with Primary Text Classes Flexible MVC configuration Rich delegation interface points Subclassing Custom text container shape Custom text attributes 10

Cocoa Text System The Cocoa Text System primary classes NSTextView View NSLayoutManager Controller NSTextStorage NSTextContainer Model 11

Cocoa Text System The Cocoa Text System helper classes NSTextView View NSGlyphGenerator NSLayoutManager NSTypesetter Controller NSTextStorage NSTextContainer NSTextAttachment Model 12

Advanced Cocoa Text Tips and Tricks Dan Schimpf Cocoa Engineer 13

NSTextStorage Model of the Cocoa text system Subclass of NSMutableAttributedString Can be reused across many views 14

NSLayoutManager Lays out text in NSTextStorage Uses instance of NSTypesetter to lay out text Arranges glyphs in regions described by NSTextContainer 15

NSTextContainer Defines a region of text Rectangular by default, but can be customized Hit testing 16

NSTextView Subclass of NSView Uses NSLayoutManager to draw text in drawrect: Edits text in NSTextStorage Resizes NSTextContainer as the view resizes 17

Basic Setup NSTextStorage NSLayoutManager NSTextContainer NSTextView 18

Tip: How to Synchronize Multiple Text Views 19

Reusing Text Storages Tell all layout managers to use the same text storage - (void)replacetextstorage:(nstextstorage *)atextstorage; Can be done using text views configured in Interface Builder 20

Multiple Text Views NSLayoutManager NSTextContainer NSTextView NSTextStorage NSLayoutManager NSTextContainer NSTextView 21

Demo Synchronized multiple text views 22

Tip: How to Add Line Numbers 23

Adding Line Numbers to NSTextView Text views are normally inside an NSScrollView NSScrollView can have custom rulers Custom ruler view to show line numbers Driven by changes to text storage NSRulerView NSTextStorageDidProcessEditingNotification Uses information from layout manager to draw - (NSRectArray)rectArrayForCharacterRange:(NSRange)charRange withinselectedcharacterrange:(nsrange)selcharrange intextcontainer:(nstextcontainer *)container rectcount:(nsuinteger *)rectcoun 24

Demo Line number view 25

Advanced Cocoa Text Tips and Tricks Aki Inoue Cocoa Engineer 26

Demo Text-folding editor 27

Tip: How to Customize Control Character Behavior 28

Concatenate Paragraphs Need to concatenate all paragraphs in a folded text range Paragraph1 Paragraph2 Paragraph3 29

NSTypesetter Performs actual line layout operations Determines Soft line breaks Line positions in text container Glyph positions in line NSTextView is the front-end component of t 30

NSTypesetter Performs actual line layout operations Abstract class declaring interface communicating with layout manager NSTextView NSTextContainer NSTypesetter NSLayoutManager NSTextStorage 31

NSTypesetter Performs actual line layout operations Abstract class declaring interface communicating with layout manager NSATSTypesetter is the system-default concrete subclass NSObject NSTypesetter NSATSTypesetter 32

NSTypesetter Performs actual line layout operations Abstract class declaring interface communicating with layout manager NSATSTypesetter is the system-default concrete subclass -layoutparagraphatpoint: drives the layout process 33

Typesetting Process 1. Layout manager requests typesetter to produce layout - (NSRange)layoutCharactersInRange:(NSRange)characterRange forlayoutmanager:(nslayoutmanager *)layoutmanager maximumnumberoflinefragments:(nsuinteger)maxnumlines; 34

Typesetting Process 1. Layout manager requests typesetter to produce layout 2. Typesetter determines paragraph boundaries - (void)setparagraphglyphrange:(nsrange)paragraphrange separatorglyphrange:(nsrange)paragraphseparatorrange; 35

Typesetting Process 1. Layout manager requests typesetter to produce layout 2. Typesetter determines paragraph boundaries 3. Typesetter performs layout - (NSUInteger)layoutParagraphAtPoint:(NSPointPointer)lineFragmentOrigin; 36

Typesetting Process 1. Layout manager requests typesetter to produce layout 2. Typesetter determines paragraph boundaries 3. Typesetter performs layout 4. Go back to 2. 37

Control Character Handling Many characters controlling and formatting text contents NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US SP 38

Control Character Handling Many characters controlling and formatting text contents Tab, whitespace, line break, paragraph break, and container break enum { NSTypesetterZeroAdvancementAction = (1 << 0), NSTypesetterWhitespaceAction = (1 << 1), NSTypesetterHorizontalTabAction = (1 << 2), NSTypesetterLineBreakAction = (1 << 3), NSTypesetterParagraphBreakAction = (1 << 4), NSTypesetterContainerBreakAction = (1 << 5) }; typedef NSUInteger NSTypesetterControlCharacterAction; 39

Control Character Handling Many characters controlling and formatting text contents Tab, whitespace, line break, paragraph break, and container break actionforcontrolcharacteratindex: determines the control action - (NSTypesetterControlCharacterAction)actionForControlCharacterAtIndex:(NSUInteger)index; 40

Control Character Handling Many characters controlling and formatting text contents Tab, whitespace, line break, paragraph break, and container break actionforcontrolcharacteratindex: determines the control action extern NSString *linefoldingattributename; - (NSTypesetterControlCharacterAction)actionForControlCharacterAtIndex:(NSUInteger)index { NSAttributedString *attrstring = [self attributedstring]; id value = [attrstring attribute:linefoldingattributename atindex:index effectiverange:null]; if (value && [value boolvalue]) return NSTypesetterZeroAdvancementAction;! return [super actionforcontrolcharacteratindex:charindex]; } 41

Tip: How to Customize Character to Glyph Mapping 42

Hide Text in Folded Range Substitute glyphs in a folded text range Paragraph1 Paragraph2 Paragraph3 43

Glyph Generation A glyph is the index for a graphical element in the font A U+0041 Chalkboard + = A Glyph 37 44

Glyph Generation A glyph is the index for a graphical element in the font A U+0041! + = A Glyph 04 45

Glyph Generation A glyph is the index for a graphical element in the font Process for mapping Unicode characters to glyph IDs in the font A B C a b c! A B C a b c 46

Glyph Generation A glyph is the index for a graphical element in the font Process for mapping Unicode characters to glyph IDs in the font NSGlyphGenerator performs the glyph generation process 47

Subclassing NSGlyphGenerator NSGlyphGenerator is an abstract class for a class cluster 48

Subclassing NSGlyphGenerator NSGlyphGenerator is an abstract class for a class cluster Layout manager requests glyph generation - (void)generateglyphsforglyphstorage:(id <NSGlyphStorage>)glyphStorage desirednumberofcharacters:(nsuinteger)nchars glyphindex:(nsuinteger *)glyphindex characterindex:(nsuinteger *)charindex; 49

Subclassing NSGlyphGenerator NSGlyphGenerator is an abstract class for a class cluster Layout manager requests glyph generation Glyph generator communicates results via NSGlyphStorage protocol @protocol NSGlyphStorage - (void)insertglyphs:(const NSGlyph *)glyphs length:(nsuinteger)length forstartingglyphatindex:(nsuinteger)glyphindex characterindex:(nsuinteger)charindex; /* rest of the protocol here */ @end 50

Subclassing NSGlyphGenerator NSGlyphGenerator is an abstract class for a class cluster Layout manager requests glyph generation Glyph generator communicates results via NSGlyphStorage protocol An NSGlyphGenerator subclass delegates to the shared instance NSLayoutManager <NSGlyphStorage> <NSGlyphStorage> NSGlyphGenerator Custom shared Generator instance 51

Subclassing NSGlyphGenerator NSGlyphGenerator is an abstract class for a class cluster Layout manager requests glyph generation Glyph generator communicates results via NSGlyphStorage protocol An NSGlyphGenerator subclass delegates to the shared instance NSLayoutManager <NSGlyphStorage> <NSGlyphStorage> Custom Glyph Generator NSGlyphGenerator shared instance 52

Subclassing NSGlyphGenerator @interface LineFoldingGlyphGenerator : NSGlyphGenerator <NSGlyphStorage> { id <NSGlyphStorage> _destination; } @end 53

Subclassing NSGlyphGenerator @implementation LineFoldingGlyphGenerator - (void)generateglyphsforglyphstorage:(id <NSGlyphStorage>)glyphStorage desirednumberofcharacters:(nsuinteger)nchars glyphindex:(nsuinteger *)glyphindex characterindex:(nsuinteger *)charindex { NSGlyphGenerator instance = [NSGlyphGenerator sharedglyphgenerator]; _destination = glyphstorage; [instance generateglyphsforglyphstorage:self desirednumberofcharacters:nchars glyphindex:glyphindex characterindex:charindex]; _destination = nil; } @end 54

Subclassing NSGlyphGenerator @implementation LineFoldingGlyphGenerator - (void)insertglyphs:(const NSGlyph *)glyphs length:(nsuinteger)length forstartingglyphatindex:(nsuinteger)glyphindex characterindex:(nsuinteger)charindex { // Glyph customization code goes here [_destination insertglyphs:glyphs length:length forstartingglyphatindex:glyphindex characterindex:charindex]; } @end 55

Custom Glyph Generation for Text Folding The first character in a folded text range becomes NSControlGlyph A B C a b c Control 56

Custom Glyph Generation for Text Folding The first character in a folded text range becomes NSControlGlyph The rest are NSNullGlyph A B C a b c Control Null Null Null Null Null 57

Custom Glyph Generation for Text Folding The first character in a folded text range becomes NSControlGlyph The rest are NSNullGlyph Changed Build 1 from On Click to Automatically after transition A B C a b c Control Null Null Null Null Null 58

Tip: How to Add UI Widgets to Text Contents 59

Showing Disclosure Triangle Text attachment acting as an inline UI widget Paragraph1 Paragraph2 Paragraph3 60

Embedding Inline Objects Image embedding is built in Files can be handled easily Designed to handle arbitrary objects Beyond just making space for images NSTextAttachment 61

NSTextAttachment Encapsulates both storage and icon NSFileWrapper NSTextAttachmentCell Complete suite of API for object embedding NSTokenField utilizes custom text attachments 62

NSTextAttachmentCell Fully customizable inline object Custom Shape - (void)drawwithframe:(nsrect)cellframe inview:(nsview *)controlview characterindex:(nsuinteger)charindex layoutmanager:(nslayoutmanager *)layoutmanager; - (NSRect)cellFrameForTextContainer:(NSTextContainer *)textcontainer proposedlinefragment:(nsrect)linefrag glyphposition:(nspoint)position characterindex:(nsuinteger)charindex; 63

NSTextAttachmentCell Fully customizable inline object Custom Mouse Handling - (BOOL)wantsToTrackMouseForEvent:(NSEvent *)theevent inrect:(nsrect)cellframe ofview:(nsview *)controlview atcharacterindex:(nsuinteger)charindex; - (BOOL)trackMouse:(NSEvent *)theevent inrect:(nsrect)cellframe ofview:(nsview *)controlview atcharacterindex:(nsuinteger)charindex untilmouseup:(bool)flag; 64

Synthesizing Text Attachment Attribute Returning a custom text attachment cell @interface LineFoldingTextStorage : NSTextStorage { NSTextStorage *_attributedstring; BOOL _linefoldingenabled; } @property(getter=islinefoldingenabled) BOOL linefoldingenabled; @end 65

Synthesizing Text Attachment Attribute Returning a custom text attachment cell @implementation LineFoldingTextStorage - (NSDictionary *)attributesatindex:(nsuinteger)location effectiverange:(nsrangepointer)range { NSDictionary *attributes = [_attributedstring attributesatindex:location effectiverange:range]; if (self.islinefoldingenabled) { /* Add a custom attachment attribute here */ } } return attributes; 66

Synthesizing Text Attachment Attribute Returning a custom text attachment cell Synthesize the attribute when rendering and laying out -[NSLayoutManager drawglyphsforglyphrange:atpoint:] -[NSATSTypesetter layoutparagraphatpoint:] - (void)drawglyphsforglyphrange:(nsrange)glyphstoshow atpoint:(nspoint)origin { NSTextStorage *textstorage = [self textstorage]; } [(LineFoldingTextStorage *)textstorage setlinefoldingenabled:yes]; [super drawglyphsforglyphrange:glyphstoshow atpoint:origin]; [(LineFoldingTextStorage *)textstorage setlinefoldingenabled:no]; 67

Tip: How to Animate Text 68

Text Animation Manipulate like graphic elements Choose appropriate animating text unit Capture starting and ending state Use overlay views Animating by Word Text Text Text 69

Paragraph1 Paragraph2 Paragraph3 Attaching Overlay Animate overlay views Paragraph1 Paragraph2 Paragraph3 70

Overlay Animation Can avoid complication Easier to manage start and end state capturing More efficient by focusing to visible area Easier to prevent font smoothing issues 71

Font Smoothing vs. Transparent Background a.k.a. subpixel rendering Cannot choose appropriate color Use opaque layer background OK to disable font smoothing while animating 72

Animating NSTextView Contents Attaching a layer hosting view on the fly Paragraph1 Paragraph2 Paragraph3 Paragraph3 Removed Base Overlay Out build Base Overlay Animating Overlay 73

Animating NSTextView Contents @interface LineFoldingAnimationOverlay : NSView { void (^_renderer)(void); } @property (copy) void (^renderer)(void); @end @implementation LineFoldingAnimationOverlay : NSView - (void)drawrect:(nsrect)dirtyrect { if (_renderer) _renderer(); } @end 74

Animating NSTextView Contents Cache into CoreAnimation layer bounds = [layoutmanager boundingrectforglyphrange:glyphrange intextcontainer:textcontainer]; [overlay setrenderer:(void (^)(void))^(void) { NSPoint point = NSMakePoint(-NSMinX(bounds), -NSMinY(bounds)); [color set]; NSRectFill([overlay bounds]); [layoutmanager drawglyphsforglyphrange:glyphrange atpoint:point]; }]; [overlay display]; 75

Animating NSTextView Contents Recommend attaching a layer hosting view on the fly Controlling the layout process for capturing the right moment -beginediting and -endediting for delaying layout NSArray *ranges = [self rangesforuserparagraphattributechange]; [textstorage beginediting]; [ranges enumerateobjectsusingblock:^(id obj, NSUInteger idx, BOOL *stop) { NSRange currentrange = [obj rangevalue]; /* cache pre-animation state */ /* modify textstorage for currentrange */ }]; [textstorage endediting]; 76

More Information Bill Dudney Application Frameworks Evangelist dudney@apple.com Documentation Cocoa Text Architecture Guide http://developer.apple.com/mac/library/documentation/textfonts/conceptual/cocoatextarchitecture Apple Developer Forums http://devforums.apple.com 77

Related Sessions API Design for Cocoa and Cocoa Touch Key Event Handling in Cocoa Applications Marina Thursday 4:30PM Russian Hill Friday 11:30AM 78

Labs Cocoa Lab Application Frameworks Lab D Thursday 2:00PM 79

Summary The Cocoa Text System provides virtually unlimited extensibility Its modular design allows customization at the right level for any kind of application needs Seek customization points in the primary classes first; then, consider helper class subclassing 80

Q&A 81

82

The last slide after the logo is intentionally left blank for all 83

84