Slide 1

Slide 1 text

TextKit͔Βදݱ͕޿͕Δ @ iOS DC2018 2018/09/02 10:50ʙ Track C ϨΪϡϥʔτʔΫ Kohei Nishioka / paper_and_paper

Slide 2

Slide 2 text

Introduction

Slide 3

Slide 3 text

Profile • Kohei Nishioka / Pref. Fukui • iOS Engineer @ iRidge Inc. • Swift, Objective-C, C++, Ocaml, 
 PHP, JavaScript … • Japanese / English • Github takumi314 • Twitter #paper_and_paper

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Agenda ɾ8IZ5FYU,JU ɾ8IBUT5FYU,JU ɾ4USVDUVSFT ɾ/4"UUSJCVUFE4USJOH ɾ/4$POUBJOFS ɾ/45FYU-BZPVU.BOHFS ɾ4VNNBSZ

Slide 6

Slide 6 text

Why TextKit ?

Slide 7

Slide 7 text

ൃ୺ ɹɹɹʮ͜ͷߦؒΛ2p͚ͩখͯ͘͞͠΋Β͑·͢ʁʯ ɹɹɹʮ͜͜ͷจࣈ͚ͩϑΥϯτม͑ͯཉ͍͠ʯ ɹɹɹʮจࣈͷִؒΛͪΐͬͱ͚ͩ…. ʯ

Slide 8

Slide 8 text

ʮ͸ɺ͸͍….ʯ

Slide 9

Slide 9 text

WWDC 2018 Session 221

Slide 10

Slide 10 text

WWDC 2018

Slide 11

Slide 11 text

WWDC 2013

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

“This is what customers pay us for – to sweat all these details so it’s easy and pleasant for them to use our computers. We’re supposed to be really good at this. That doesn’t mean we don’t listen to customers, but it’s hard for them to tell you what they want when they’ve never seen anything remotely like it. Take desktop video editing. I never got one request from someone who wanted to edit movies on his computer. Yet now that people see it, they say, ‘Oh my God, that’s great!’ ”

Slide 14

Slide 14 text

“Sweating the Details ”

Slide 15

Slide 15 text

What’s TextKit ?

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

TextKit • Rendering Engine for Texting • iOS7 or Later • Based on Core Text

Slide 18

Slide 18 text

TextKit and CoreText

Slide 19

Slide 19 text

Before iOS7 … HTMLͰඳը

Slide 20

Slide 20 text

iOS7 or Later

Slide 21

Slide 21 text

API groups w ςΩετΛදࣔ͢Δ w 6*5FYU7JFX w 6*5FYU'JFME w 6*-BCFM w ςΩετඳըΛ੍ޚ͢Δ w /4-BZPVU.BOBHFS w /45FYU$POUBJOFS w σΩετσʔλΛอ؅͢Δ w /4"UUSJCVUF4USJOH TextKitͷຊؙ

Slide 22

Slide 22 text

API groups w ςΩετΛදࣔ͢Δ w 6*5FYU7JFX w 6*5FYU'JFME w 6*-BCFM w ςΩετඳըΛ੍ޚ͢Δ w /4-BZPVU.BOBHFS w /45FYU$POUBJOFS w σΩετσʔλΛอ؅͢Δ w /4"UUSJCVUF4USJOH View Controller Model

Slide 23

Slide 23 text

TextKit Like “MVC” w7JFX w6*5FYU7JFX w6*5FYU'JFME w6*-BCFM w$POUSPMMFS w-BZPVU.BOBHFS w/45FYU$POUBJOFS w.PEFM w/4"UUSJCVUFE4USJOH

Slide 24

Slide 24 text

Structure

Slide 25

Slide 25 text

The Reletions between Objects UITextView NSTextContainer NSLayoutManager NSTextStorage

Slide 26

Slide 26 text

Structures ɾNSAttributeString ɾNSContainer ɾNSTextLayoutManger

Slide 27

Slide 27 text

NSTextStorage

Slide 28

Slide 28 text

The Reletions between Objects UITextView NSTextContainer NSLayoutManager NSTextStorage

Slide 29

Slide 29 text

NSTextStorage • TextKitͷModelʹ૬౰͢Δ • NSMutableAttributedStringͷSubclass (ΫϥεΫϥελ) • NSLayoutManagerͷมߋΛcharacters ΍ attributes ʹ
 ௨஌ͯ͠ඞཁͳΒ͹࠶දࣔΛଅ͢ • 1ͭຢ͸ෳ਺ͷNSLayoutManagerΛ࣋ͭ • ͲͷεϨου͔ΒͰ΋ΞΫηεՄೳ • ಉ࣌ʹෳ਺εϨου͔ΒͷΞΫηε͸ආ͚ͨํ͕ແ೉ https://developer.apple.com/documentation/uikit/nstextstorage

Slide 30

Slide 30 text

Original Storage NSAttributedStringͷϓϦϛςΟϒϝιου NSMutableAttributedStringͷϓϦϛςΟϒϝιου ৄࡉ͸AppleͷʮObjective-C ϓϩάϥϛϯά֓೦ʯͳͲΛࢀর

Slide 31

Slide 31 text

Ex. CustomTextStorage

Slide 32

Slide 32 text

NSAttributedString

Slide 33

Slide 33 text

NSAttributedString /4"UUSJCVUFE4USJOHͱಉ͡ػೳ

Slide 34

Slide 34 text

UIFont

Slide 35

Slide 35 text

UIFont • pointSize • lineHeight • ascender • desceder • leading • capHeight

Slide 36

Slide 36 text

UIFont • pointSize • lineHeight • ascender • desceder • leading • capHeight

Slide 37

Slide 37 text

UIFont • pointSize • lineHeight • ascender • desceder • leading • capHeight

Slide 38

Slide 38 text

UIFont • pointSize • lineHeight • ascender • desceder • leading • capHeight

Slide 39

Slide 39 text

UIFont • pointSize • lineHeight • ascender • desceder • leading • capHeight

Slide 40

Slide 40 text

UIFont • pointSize • lineHeight • ascender • desceder • leading • capHeight

Slide 41

Slide 41 text

UIFont • pointSize • lineHeight • ascender • desceder • leading • capHeight

Slide 42

Slide 42 text

usesFontLeading LeadingΛແࢹ͢Δ৔߹

Slide 43

Slide 43 text

Adding a Custom Font • αϙʔτ͍ͯ͠Δܗࣜ • True Type Font (*.ttf) • Open TypeFont (*.otf)
 • ະ֬ೝ • True Type Collection (*.ttc) ʁʁ

Slide 44

Slide 44 text

NSAttributedString • σϑΥϧτ஋͕ଘࡏ͢Δ

Slide 45

Slide 45 text

I Love iOSDC Apply bold font with "Comic Sans MS"

Slide 46

Slide 46 text

I Love iOSDC textView.text = "I Love iOSDC" guard let originFont = UIFont(name: "Comic Sans MS", size: 24.0) else { return } let boldFontDescriper = originFont.fontDescriptor.withSymbolicTraits(.traitBold) let boldFont = UIFont(descriptor: boldFontDescriper!, size: (originFont.pointSize)) let newText = NSMutableAttributedString(string: textView.text) newText.addAttribute(.font, value: boldFont, range: NSRange(location: 1, length: 5)) textView.attributedText = newText

Slide 47

Slide 47 text

I Love iOSDC textView.text = "I Love iOSDC" guard let originFont = UIFont(name: "Comic Sans MS", size: 24.0) else { return } let boldFontDescriper = originFont.fontDescriptor.withSymbolicTraits(.traitBold) let boldFont = UIFont(descriptor: boldFontDescriper!, size: (originFont.pointSize)) let newText = NSMutableAttributedString(string: textView.text) newText.addAttribute(.font, value: boldFont, range: NSRange(location: 1, length: 5)) textView.attributedText = newText I Love iOSDC

Slide 48

Slide 48 text

textView.text = "I Love iOSDC" guard let originFont = UIFont(name: "Comic Sans MS", size: 24.0) else { return } let boldFontDescriper = originFont.fontDescriptor.withSymbolicTraits(.traitBold) let boldFont = UIFont(descriptor: boldFontDescriper!, size: (originFont.pointSize)) let newText = NSMutableAttributedString(string: textView.text) newText.addAttribute(.font, value: boldFont, range: NSRange(location: 1, length: 5)) textView.attributedText = newText

Slide 49

Slide 49 text

I Love iOSDC Comic Sans MS 24-pt I Love iOSDC Arial Black 12-pt I Love iOSDC Helvetica 12-pt ద༻લ ॳظԽ ଐੑઃఆ

Slide 50

Slide 50 text

Attributes with default values Baseline offset 0.0 Font Helvetica 12-pt Background color None (no background) Kern None (use font's default kerning) Ligature None (use font's standard ligatures) Link None Foreground color None (no background) Paragraph style defaultParagapghStyle

Slide 51

Slide 51 text

I Love iOSDC textView.text = "I Love iOSDC" guard let originFont = UIFont(name: "Comic Sans MS", size: 24.0) else { return } let boldFontDescriper = originFont.fontDescriptor.withSymbolicTraits(.traitBold) let boldFont = UIFont(descriptor: boldFontDescriper!, size: (originFont.pointSize)) let newText = NSMutableAttributedString(attributedString: textView.attributedText) newText.addAttribute(.font, value: boldFont, range: NSRange(location: 1, length: 5)) textView.attributedText = newText ΠχγϟϥΠβʹଐੑ৘ใΛ༩͑Δ

Slide 52

Slide 52 text

The Reletions between Objects UITextView NSTextContainer NSLayoutManager NSTextStorage

Slide 53

Slide 53 text

NSLayoutManager

Slide 54

Slide 54 text

NSLayoutManager • ϨΠΞ΢τͱϨϯμϦϯάʹؔ͢Δௐ੔Λߦ͏ • ςΩετΛදࣔ͢΂͖࠲ඪͱܗঢ় -> NSTextContainer • දࣔ͢Δίϯςϯπ -> NSTextStorage
 • ̍ͭ·ͨ͸ෳ਺ͷNSTextContainerΛ࣋ͭ • ̍ͭͷNSTextStorageΛ࣋ͭ

Slide 55

Slide 55 text

NSLayoutManager

Slide 56

Slide 56 text

NSTextContainer

Slide 57

Slide 57 text

NSTextContainer ʮUIKitపఈղઆ iOSϢʔβʔΠϯλʔϑΣΠεͷ։ൃʯΑΓൈਮ 

Slide 58

Slide 58 text

ʮUIKitపఈղઆ iOSϢʔβʔΠϯλʔϑΣΠεͷ։ൃʯΑΓൈਮ

Slide 59

Slide 59 text

NSTextContainer ඇදࣔྖҬ

Slide 60

Slide 60 text

NSTextContainer • UITextView্ʹදࣔ͢ΔςΩετͷ࠲ඪͱܗঢ়Λఆٛ •

Slide 61

Slide 61 text

NSTextContainer → NSLaytouManager͕ςΩετҐஔΛڭ͑ͯ͘ΕΔ

Slide 62

Slide 62 text

NSTextContainer ιʔείʔυ͸ͪ͜Β ->ɹhttps://github.com/takumi314/Sensitive-TextView

Slide 63

Slide 63 text

Summary • TextKitΛ੍͢Ε͹ɺେ൒ͷςΩετඳըΛϚελͰ͖Δ • ࡉ෦ʹϑΥʔΧε͢Δ΄Ͳଟ༷ͳςΩετදݱʹ

Slide 64

Slide 64 text

• Text Programming Guide for iOS - https:// developer.apple.com/library/archive/documentation/ StringsTextFonts/Conceptual/TextAndWebiPhoneOS/ CustomTextProcessing/CustomTextProcessing.html • WWDC2018 “TextKit Best Practice” - session 221 • WWDC2013 “Introducing TextKit” - session 210 References

Slide 65

Slide 65 text

Thank you

Slide 66

Slide 66 text

͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ m(__)m