Slide 1

Slide 1 text

Handling rich text in Swift iOSDC Japan 2016 Katsumi Kishikawa [email protected]

Slide 2

Slide 2 text

Katsumi Kishikawa Realm Inc. [email protected]

Slide 3

Slide 3 text

Slide 4

Slide 4 text

Slide 5

Slide 5 text

Slide 6

Slide 6 text

Slide 7

Slide 7 text

Agenda ˖ J04ךذؗأزٖ؎،ؐزחאְג ˖ ة؎هؚٓؿ؍ך㛇燉 ˖ 5FYU,JUך㛇燉ה䘔欽 [email protected]

Slide 8

Slide 8 text

iOS 7Ҏ߱ͷςΩετϨΠΞ΢τ [email protected]

Slide 9

Slide 9 text

Slide 10

Slide 10 text

TextKitͱ͸ ˖ 넝鸞זٌتٝذؗأزٖ؎،ؐز٥ٖٝت ؚٔٝؒٝآٝ ˖ $PSF5FYUד⡲׵׸גְ׷ ˖ 6*,JUהך窟さ [email protected] https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/CustomTextProcessing/CustomTextProcessing.html

Slide 11

Slide 11 text

Slide 12

Slide 12 text

[email protected] UILabelΛී௨ʹ࢖͏

Slide 13

Slide 13 text

[email protected] ώϥΪϊͱγεςϜϑΥϯτ UIFont(name: "HiraginoSans-W3", size: 32) UIFont.systemFontOfSize(32)

Slide 14

Slide 14 text

[email protected] ώϥΪϊͱγεςϜϑΥϯτ

Slide 15

Slide 15 text

[email protected] ώϥΪϊͱγεςϜϑΥϯτ

Slide 16

Slide 16 text

[email protected] General Header

Slide 17

Slide 17 text

[email protected] ΞϧϑΝϕοτΛؚ·ͳ͍৔߹

Slide 18

Slide 18 text

ߦͷߴ͞͸ԿͰܾ·Δ͔ [email protected]

Slide 19

Slide 19 text

[email protected] Font metrics https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/FontHandling/FontHandling.html

Slide 20

Slide 20 text

[email protected] Font metrics https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/FontHandling/FontHandling.html

Slide 21

Slide 21 text

[email protected] Font metrics

Slide 22

Slide 22 text

[email protected] Font metrics Baseline X-Height Cap height Ascent Descent

Slide 23

Slide 23 text

[email protected] ώϥΪϊ͸ಛघͳϝτϦΫεΛ࣋ͭ

Slide 24

Slide 24 text

[email protected] ʢࢀߟʣNoto Sans CJK JP

Slide 25

Slide 25 text

let label = UILabel(frame: view.bounds) label.backgroundColor = ... label.textColor = ... label.font = ... label.text = text ... label.sizeToFit() label.frame.size.height += ceil(abs(label.font.descender * 2)) [email protected] ্ԼʹDescentΛิ͏

Slide 26

Slide 26 text

[email protected] ্ԼʹDescentΛิ͏

Slide 27

Slide 27 text

[email protected] ্ԼʹDescentΛิ͏

Slide 28

Slide 28 text

[email protected] ΦϦδφϧͱൺֱ

Slide 29

Slide 29 text

• ώϥΪϊΛ໌ࣔతʹࢦఆ͠ͳ͍ • γεςϜϑΥϯτΛ࢖͏ • σβΠϯࢦࣔॻͳͲͰɺώϥΪϊ͸͍͍ͨͯγε ςϜϑΥϯτΛҙਤ͍ͯ͠Δ͸ͣ • தࠃޠ༻ϑΥϯτʹϑΥʔϧόοΫͨ͘͠ͳ͍Մ ೳੑ • ώϥΪϊΛࢦఆ͢Δ৔߹ɺΞϧϑΝϕοτʹؾΛ ͚ͭΔ [email protected] ώϥΪϊͷϝτϦΫεʹ஫ҙ

Slide 30

Slide 30 text

[email protected] Tips: ೔ຊޠϑΥϯτʹϑΥʔϧόοΫͤ͞Δ let attributes = [ NSFontAttributeName: font, kCTLanguageAttributeName as String: "ja", ... ]

Slide 31

Slide 31 text

αΠζͷܭࢉ [email protected]

Slide 32

Slide 32 text

let size = CGSize(width: label.bounds.width, height: CGFloat.max) let boundingRect = NSString(string: text).boundingRectWithSize(size, options: [.UsesLineFragmentOrigin], attributes: [NSFontAttributeName: font], context: nil) [email protected] ඳըαΠζΛಘΔ

Slide 33

Slide 33 text

[email protected] ඳըαΠζΛಘΔ

Slide 34

Slide 34 text

UITextView [email protected]

Slide 35

Slide 35 text

[email protected] UITextViewΛී௨ʹ࢖͏

Slide 36

Slide 36 text

[email protected] UITextViewΛී௨ʹ࢖͏ UIFont(name: "HiraginoSans-W3", size: 32) UIFont.systemFontOfSize(32)

Slide 37

Slide 37 text

[email protected] ඳըαΠζ

Slide 38

Slide 38 text

[email protected] ඳըαΠζ

Slide 39

Slide 39 text

[email protected] σϑΥϧτͷϚʔδϯ

Slide 40

Slide 40 text

σϑΥϧτͷϚʔδϯ textView.textContainer.lineFragmentPadding textView.textContainerInset UIEdgeInsets(top: 8.0, left: 0.0, bottom: 8.0, right: 0.0) 5.0 [email protected]

Slide 41

Slide 41 text

[email protected] ώϥΪϊΛࢦఆͨ͠৔߹ font.leading

Slide 42

Slide 42 text

σϑΥϧτͷελΠϧΛϦηοτ͢Δ [email protected]

Slide 43

Slide 43 text

let textView = UITextView(frame: view.bounds) ... textView.textContainerInset = UIEdgeInsetsZero textView.sizeToFit() [email protected] textContainerInsetΛऔΓআ͘

Slide 44

Slide 44 text

let textView = UITextView(frame: view.bounds) ... textView.textContainerInset = UIEdgeInsetsZero textView.sizeToFit() [email protected] textContainerInsetΛऔΓআ͘

Slide 45

Slide 45 text

[email protected] textContainerInsetΛऔΓআ͘

Slide 46

Slide 46 text

let textView = UITextView(frame: view.bounds) ... textView.textContainer.lineFragmentPadding = 0 textView.sizeToFit() [email protected] lineFragmentPaddingΛऔΓআ͘

Slide 47

Slide 47 text

let textView = UITextView(frame: view.bounds) ... textView.textContainer.lineFragmentPadding = 0 textView.sizeToFit() [email protected] lineFragmentPaddingΛऔΓআ͘

Slide 48

Slide 48 text

[email protected] lineFragmentPaddingΛऔΓআ͘

Slide 49

Slide 49 text

let size = CGSize(width: textView.bounds.width, height: CGFloat.max) let boundingRect = NSString(string: text).boundingRectWithSize(size, options: [.UsesLineFragmentOrigin], attributes: [NSFontAttributeName: font], context: nil) [email protected] ඳըαΠζΛಘΔ

Slide 50

Slide 50 text

[email protected] ඳըαΠζΛಘΔ

Slide 51

Slide 51 text

let textView = UITextView(frame: view.bounds) ... textView.layoutManager.usesFontLeading = false textView.sizeToFit() [email protected] font.leadingΛऔΓআ͘

Slide 52

Slide 52 text

let textView = UITextView(frame: view.bounds) ... textView.layoutManager.usesFontLeading = false textView.sizeToFit() [email protected] font.leadingΛऔΓআ͘

Slide 53

Slide 53 text

[email protected] font.leadingΛऔΓআ͘

Slide 54

Slide 54 text

let size = CGSize(width: textView.bounds.width, height: CGFloat.max) let boundingRect = NSString(string: text).boundingRectWithSize(size, options: [.UsesLineFragmentOrigin, .UsesFontLeading], attributes: [NSFontAttributeName: font], context: nil) [email protected] ʢผղʣfont.leadingΛؚΊͯܭࢉ͢Δ

Slide 55

Slide 55 text

[email protected] ʢผղʣfont.leadingΛؚΊͯܭࢉ͢Δ

Slide 56

Slide 56 text

[email protected] ʢผղʣfont.leadingΛؚΊͯܭࢉ͢Δ

Slide 57

Slide 57 text

[email protected] textView.textContainerInset = UIEdgeInsetsZero textView.textContainer.lineFragmentPadding = 0 textView.layoutManager.usesFontLeading = false σϑΥϧτελΠϧͷϦηοτ

Slide 58

Slide 58 text

͞·͟·ͳελΠϧʹରԠ͢Δ [email protected]

Slide 59

Slide 59 text

[email protected] ߦؒͷมߋ

Slide 60

Slide 60 text

NSAttributedString [email protected]

Slide 61

Slide 61 text

NSParagraphStyle [email protected]

Slide 62

Slide 62 text

let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.minimumLineHeight = ceil(font.lineHeight) paragraphStyle.maximumLineHeight = ceil(font.lineHeight) paragraphStyle.lineSpacing = 8 let attributes = [ NSFontAttributeName: font, NSForegroundColorAttributeName: UIColor(...), NSParagraphStyleAttributeName: paragraphStyle, ] let attributedText = NSAttributedString(string: text, attributes: attributes) textView.attributedText = attributedText [email protected] NSAttributedString

Slide 63

Slide 63 text

let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.minimumLineHeight = ceil(font.lineHeight) paragraphStyle.maximumLineHeight = ceil(font.lineHeight) paragraphStyle.lineSpacing = 8 let attributes = [ NSFontAttributeName: font, NSForegroundColorAttributeName: UIColor(...), NSParagraphStyleAttributeName: paragraphStyle, ] let attributedText = NSAttributedString(string: text, attributes: attributes) textView.attributedText = attributedText [email protected] NSAttributedString

Slide 64

Slide 64 text

[email protected] ߦؒͷมߋ

Slide 65

Slide 65 text

[email protected] ߦؒͷมߋ

Slide 66

Slide 66 text

let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.minimumLineHeight = ceil(font.lineHeight) paragraphStyle.maximumLineHeight = ceil(font.lineHeight) paragraphStyle.lineSpacing = 8 [email protected] min/maxLineHeight

Slide 67

Slide 67 text

[email protected] min/maxLineHeight

Slide 68

Slide 68 text

[email protected] minimumLineHeight

Slide 69

Slide 69 text

[email protected] maximumLineHeight

Slide 70

Slide 70 text

NSAttributedStringΛ׆༻͢Δ [email protected]

Slide 71

Slide 71 text

[email protected] ͞ΒʹߦؒΛ޿͘

Slide 72

Slide 72 text

[email protected] ͞ΒʹߦؒΛ޿͘

Slide 73

Slide 73 text

[email protected] Օ৚ॻ͖

Slide 74

Slide 74 text

·ͱΊ • iOS 7Ҏ߱ͷςΩετϨΠΞ΢τ͸ɺ͍͔ʹ ΩϨΠͳNSAttributedStringΛ࡞Δ͔ • λΠϙάϥϑΟͷجૅΛ஌Ζ͏ • ϑΥϯτʹώϥΪϊΛࢦఆ͢Δͱ͖͸஫ҙ • Ͱ͖Δ͚ͩγεςϜϑΥϯτΛ࢖͓͏ [email protected]

Slide 75

Slide 75 text

Questions? Katsuma Kishikawa [email protected] www.realm.io/jp @k_katsumi