Slide 1

Slide 1 text

͋ ͭ CoreText ͷ͸ͳ͠ Yusuke Ito, @novi_

Slide 2

Slide 2 text

֓ཁ ग़ݱ: 3.2 (iPad only) 2010/4/3 (3.0͔Β͕͋ͬͨ·ͩPrivate) ςΩετϨϯμϦϯάΤϯδϯ TextEdit, Cocoa(NSText*)Ͱ࢖ΘΕ͍ͯΔ may be Pages, iMovie, Keynote ....

Slide 3

Slide 3 text

iOS ͱ OS X AttributedStringͱηοτͰ࢖͏ (ඳը͸CoreGraphics) RTLɾॎॻ͖༻άϦϑʹରԠ→ॎॻ͖ άϦϑͷஔ׵ (CTGlyphInfo) ҟମࣈʹ࢖͑Δ ϑΥϯτͷબ୒(σΟεΫϦϓλ) (CTFontDescriptor) ϑΥϯτϑΝΠϧͷύʔεɾ֤छϝλσʔλ (CTFont)

Slide 4

Slide 4 text

CoreText.framework (iOS) ApplicationServices.framework/CoreText.framework (OS X) CݴޠͷΠϯλʔϑΣʔε CoreFoundationϕʔε CFTypeΛܧঝ(ͱͯ͠ৼΔ෣͏)

Slide 5

Slide 5 text

NSAttributedString CFAttributedStringͱToll-free NSMutableAttributedString, CFMutableAttributedString म০จࣈ (৭ɾϑΥϯτɾߦؒ...) Λදݱ͢ΔΫϥε ෦෼จࣈྻʹରͯ͠ɺ৭(ଐੑ)ͳͲΛઃఆ͢Δ iOS4.0૬౰Ҏ্͔Β͸BlocksʹΑΔଐੑͷྻڍΛα ϙʔτ

Slide 6

Slide 6 text

Ͳ͏Ͱ΋ྑ͍࿩ ☞ ౰࣌ iOS3.2 ☞ CoreText͕࢖͑ΔΑ͏ʹͳͬͨ ☞ ॎॻ͖͕Ͱ͖Δ!! (ΞςʹͳΒͳ͍υΩϡϝϯτʹΑ Δͱʣ ☞ ͔͠͠ɺग़དྷͳ͍!! ☞ Macͱಉ͡ίʔυͰ΋ग़དྷͳ͍!!

Slide 7

Slide 7 text

☞ υΩϡϝϯτʹ͸ࡌ͍ͬͯΔͷʹ... (࢖͑Δͱ͸ॻ͍ ͯ͋Δ) ☞ ͍ͭ΋௨Γग़དྷͳ͍ΜͩΖ͏→ఘΊΔ ☞ ͱΓ͋͑ͣAppleʹόάϨϙ ☞ ςετίʔυΛૹΕͱݴΘΕΔ ☞ iOSͱMac༻ʹ࡞ͬͯૹΔ ☞ εΫϦʔϯγϣοτΛૹΔ ☞ ͦͯ͠ɺԻࠫଡແ͠...

Slide 8

Slide 8 text

೥ޙ J04ϦϦʔεલ

Slide 9

Slide 9 text

Apple: “ग़དྷͨΑʔ” ͓ͤʔΑ!!! ͱ͍͏͜ͱͰɺ ࠓ͸ॎॻ͖Ͱ͖·͢ɻ

Slide 10

Slide 10 text

αϙʔτ͸͞Ε͕ͨ... ௒όΪʔͩͬͨ! ߦͷRTL(Right to Left)ɺॎॻ͖ܥ͕·ͱ΋ʹग़དྷͳ͍ RTLϞʔυͩͱάϦϑͷbounds͕͓͔͍͠ Unicodeॲཧ΋एׯ͓͔͍͠ (ؾ͕͢Δ) ೔ຊޠͱԤจϑΥϯτࠞ߹࣌ͷߦؒ໰୊ (࢓༷) ͍͔ͭ͘ͷτϦοΫͰमਖ਼Մೳ

Slide 11

Slide 11 text

αϙʔτ͸͞Ε͕ͨ... ϑΥϯτσΟεΫϦϓλʹ೔ຊޠΛ৯ΘͤΔͱམͪΔ ώϥΪϊҎ֎·ͱ΋ʹϨϯμϦϯά͞Εͳ͍͜ͱ΋ (e.g.) IPA TrueType ϑΥϯτͷॎॻ͖͕·ͱ΋ʹͰ ͖ͳ͍ ԣ͸OKɺॎ͕μϝ (ϑΥϯτͷ࢓༷͔΋) FontForgeͰίϯόʔτͯ͠΋ͩΊ

Slide 12

Slide 12 text

How to Use ࠶ͼ: AttributedStringͱηοτͰ࢖͏ ࣮ࡍͷඳը͸CoreGraphics (ྫ͑͹-drawRect:) View͸UIViewΛܧঝͯࣗ͠෼Ͱ࣮૷ ៉ྷͳ݁ՌΛಘΔʹ͸៉ྷͳAttributedStringΛ࡞ΕΔ ͔ɻͨͿΜϙΠϯτ͸ͬͪ͜ɻ ͱ͍͏͜ͱͰɺAttributedStringͷ͸ͳ͠

Slide 13

Slide 13 text

1. Attributed StringΛ࡞Δ ଐੑ͸ Core Text String Attributes Λ࢖͏ NSAttributedStringʹCFAttributedString NSMutableAttributedStringʹCFMutableAttributedString ී௨͸Cocoa(NS)Λ࢖͑͹OK

Slide 14

Slide 14 text

2. CoreTextͰϨϯμϦϯά 1Ͱ࡞੒ͨ͠NSAttributedString͔ΒCoreTextͷΦϒ δΣΫτΛ࡞੒ CTFramesetterCreateWithAttributedString ͳͲ ϨϯμϦϯά CTFramesetterCreateFrame → CTFrameʹม׵

Slide 15

Slide 15 text

3. CoreGraphicsͰඳը͢Δ 1.CTFrameͷDraw(CTFrameDraw)Λ࢖͏ 2.CTLineͷDrawΛ࢖͏→ࣗ෼Ͱߦ͝ͱඳը 1.ۉ౳ἧ͑Λ͢Δͱ͖͸͜ΕΛ࢖͏ 3.CTRunͷDrawΛ࢖͏ 4.CTRun͔ΒάϦϑCGGlyphΛऔΓग़ͯ͠ɺ CGContextShowGlyphsͰඳը͢Δɻ ͍ͣΕ͔ͷํ๏Ͱඳը

Slide 16

Slide 16 text

CTFrameSetter NSAttributedString CTFrame CTFont CTGlyphInfo CTParagraph Style CTLine CTLine CTLine CTRun CoreGraphics -drawRect:౳ CTLine ϨϯμϦϯά ඳը ଐੑͱͯ͠ઃఆ ॳظԽ

Slide 17

Slide 17 text

׽ࣈ123 Hello World ׽ࣈ123 ... NSAttributedString Hello World CTLine ׽ ࣈ 123 CTLine CTRun

Slide 18

Slide 18 text

CTFrameSetter NSAttributedString CTFrame CTLine CTLine CTLine ϨϯμϦϯά ॳظԽ ࠓ·Ͱͷ͸Α͘঺հ͞Ε͍ͯΔ ΍Γํ

Slide 19

Slide 19 text

࣮ࡍCTFramesetter͸ɺ CTTypesetterΛ಺෦Ͱಈ͔͍ͯ͠Δ CTTypesetter NSAttributedString CTLine CTLine CTLine ࣗ෼ͰߦΛ࡞ͬͯ ϨϯμϦϯά ॳظԽ ΋͏Ұͭͷ΍Γํ: CTTypesetterΛ࢖͏ ී௨͸࠷ॳʹ঺հͨ͠΄͏Ͱे෼

Slide 20

Slide 20 text

͜͜ͰσβΠϯύλʔϯͷ࿩Ͱ΋ ΦϒδΣΫτ͸Mutable(Մม)͔Immutable(ෆม)͔ Mutable: NSMutable..., UIView, UIViewController... Immutable: NSObject, NSString, UIImage, UIColor... NSMutableAttributedString จࣈྻࣗମ͸Immutable (-initWithString:) จࣈྻͷଐੑ͸Mutable (-setAttributes:range:) CoreText͸͢΂ͯImmutable

Slide 21

Slide 21 text

CTFrameSetter NSAttributedString NSMutableAttributed... CTFrame CTFont CTGlyphInfo CTParagraph Style CTLine CTLine CTLine CTRun CTLine ϨϯμϦϯά ଐੑͱͯ͠ઃఆ ΠχγϟϥΠβʔ CTFramesetterCreateWithAttributedString CTFramesetterCr eateFrame औΓग़͠ CTFrameGetLines औΓग़͠ CTLineGetGlyphRuns Imuutable Imuutable Imuutable Imuutable Imuutable Imuutable ಈతͳมߋ͸೉͍͠ͷͰ͏·͘ઃܭ͠·͠ΐ͏ɻ

Slide 22

Slide 22 text

Ͱ͖Δ or Ͱ͖ͳ͍, FAQ ϦϯΫΤϥʔ iOS͸CoreText.frameworkͷ௥Ճ(ϦϯΫ)͕ඞཁ ඳը࣌ͷαΠζ͕ཉ͍͠ CTFramesetterSuggestFrameSizeWithConstraints ۉ౳഑ஔ CTLineCreateJustifiedLineͰCTLineΛม׵ͯ͠ CTLine͝ͱඳը

Slide 23

Slide 23 text

ը૾΍ಈը౳ΛຒΊࠐΜͰϨϯμϦϯά AttributedStringʹจࣈU+FFFCΛ௥هͯ͠ɺ kCTRunDelegateAttributeName ଐੑͰCTRunDelegateΛઃఆ ͢Δͱࢦఆͨ͠αΠζͷۭ͖ྖҬ͕Ͱ͖Δ→͜͜ʹޙͰࣗ෼Ͱ ViewΛஔ͘ ඳը࣌ʹCTRunGetAttributesͰCTRunDelegateΛऔಘ ͨͩ͠ɺView͸ࣗ෼Ͱஔ͍ͯͶ

Slide 24

Slide 24 text

άϦϑͷஔ׵ kCTGlyphInfoAttributeNameଐੑͰ CTGlyphInfo Λ ηοτ લ͸iOSͰ͸ಈ͔ͳ͔͕࣮ͬͨ૷͞Εͨ ߦؒͳͲͷஈམͷઃఆ kCTParagraphStyleAttributeNameଐੑͰ CTParagraphStyle Ληοτ

Slide 25

Slide 25 text

೔ຊޠͱԤจࠞࡏ࣌ʹߦ͕ؒෆࣗવ ํ๏1: ߦؒΛࣗ෼Ͱۭ͚ͳ͕ΒɺCTLine͝ͱʹඳը ํ๏2: kCTParagraphStyleSpecifierMinimumLineSpacing, kCTParagraphStyleSpecifierMaximumLineSpacing Λ0.0 ʹηοτ (͜ͷଐੑ͸υΩϡϝϯτʹࡌ͍ͬͯͳ͍ͷ Ͱ஫ҙɻϔομϑΝΠϧࢀর)

Slide 26

Slide 26 text

ॎॻ͖ (࣍ͷ2ͭͷ࡞ۀ͕ඞཁ) ॎॻ͖ͷάϦϑʹ͢Δ: kCTVerticalFormsAttributeName ߦΛॎஔ͖Ͱɺӈ͔Βࠨʹඳը͢Δ: ํ๏1: kCTFrameProgressionAttributeName (LineͳͲͷboundsऔಘͰόά͕͋Δؾ͕͢Δ) ํ๏2: CGContextͰMatrix(CGAffineTransform)Λ ηοτͯ͠ճసͤ͞Δ

Slide 27

Slide 27 text

ςΩετΤσΟλతͳ΋ͷΛ࡞Δ UITextInput Protocol ͔Β࢝ΊΔ Text, Web, and Editing Programming Guide for iOS

Slide 28

Slide 28 text

͓͢͢ΊReference ެࣜ: ATSUIΛ࢖ͬͨUnicodeςΩετඳը ΋͏άάͬͯ΋ग़ͯ͜ͳ͍͕ɺWebArchiveʹ͋ͬͨɻ http://bit.ly/Hh7sP3 ެࣜ: Core Text Reference Attributed String Programming Guide Core Text String Attributes Reference NSMutableAttributedString Class Reference Strings Programming Guide for Core Foundation

Slide 29

Slide 29 text

Reference 2 WWDC Video #110, 2010, Advanced Text Handling for iPhone OS #114, 2010, Advanced Cocoa Text Tips and Tricks #128, 2011, Advanced Text Processing Google ݕࡧ: “coretext filetype:pdf” “attributed string filetype:pdf” SlideShare, SpeakerDeck Github

Slide 30

Slide 30 text

LTCoreText ͦΕͳΓʹܰྔ ϖʔδϯά/ ஈ૊Έ ॎஔ͖/ॎॻ͖ ΧελϜϏϡʔ౳ͷຒΊࠐΈ ಈతͳϖʔδૠೖɾ࡟আ ඇಉظϨΠΞ΢τॲཧ https://github.com/novi/LTCoreText ͓·͚: LTGridView  ్த͚ͩͲGridViewͷϥΠϒϥϦ ެ։தͷϥΠϒϥϦ

Slide 31

Slide 31 text

demo LTCoreText

Slide 32

Slide 32 text

Thank you ❡ https://github.com/novi http://speakerdeck.com/u/yusukeito http://about.me/yusukeito

Slide 33

Slide 33 text

Appendix: RetinaͱCoreText Retinaͩͱएׯจࣈ͕ࡉ͘ݟ͑Δ (ͱࢥ͏) αϒϐΫηϧϨϯμϦϯάͷON/OFFʹΑͬͯ΋ҧ͏ Retina/ඇRetina, αϒϐΫηϧϨϯμϦϯάON/OFF ͷ4ύλʔϯ Mac͸σϑΥϧτON, iOS͸OFF ͝·͔͕͠ޮ͔ͳ͍ͷͰϑΥϯτͷ඼࣭͕෼͔Δ ώϥΪϊͷ͸͔ͬ͠Γ࡞ͬͯ͋Δҹ৅