Slide 1

Slide 1 text

Mastering TextKit try! Swift NYC 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 ˖ )PXUFYUTMBJEPVUJOJ04 ˖ #BTJDUZQPHSBQIZ ˖ #BTJDVTBHFPG5FYU,JU ˖ "EWBODFEFYBNQMFT [email protected]

Slide 8

Slide 8 text

Text Layout after iOS 7 [email protected]

Slide 9

Slide 9 text

What is TextKit? [email protected]

Slide 10

Slide 10 text

TextKit is ˖ .PEFSOUFYUSFOEFSJOHFOHJOF ˖ #VJMUPOUPQPG$PSF5FYU ˖ )JHIMZJOUFHSBUFEXJUI6*,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

Slide 13

Slide 13 text

Font Metrics [email protected]

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

How to know the display size [email protected]

Slide 18

Slide 18 text

let size = CGSize(width: label.bounds.width, height: CGFloat.max) let boundingRect = NSString(string: text).boundingRectWithSize(size, options: [], attributes: [NSFontAttributeName: font], context: nil) [email protected] Get Bounding Rect Single Line

Slide 19

Slide 19 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] Get Bounding Rect Multiple Lines

Slide 20

Slide 20 text

[email protected] Get Bounding Rect

Slide 21

Slide 21 text

UITextView [email protected]

Slide 22

Slide 22 text

[email protected] UITextView

Slide 23

Slide 23 text

[email protected] UITextView

Slide 24

Slide 24 text

[email protected] UITextView

Slide 25

Slide 25 text

[email protected] Default margins

Slide 26

Slide 26 text

[email protected] Default margins textView.textContainer.lineFragmentPadding textView.textContainerInset UIEdgeInsets(top: 8.0, left: 0.0, bottom: 8.0, right: 0.0) 5.0

Slide 27

Slide 27 text

Slide 28

Slide 28 text

[email protected] CJK Font font.leading

Slide 29

Slide 29 text

[email protected] Custom Font (Not built-in) font.leading

Slide 30

Slide 30 text

Reset Default Margins [email protected]

Slide 31

Slide 31 text

let textView = UITextView(frame: view.bounds) ... textView.textContainerInset = UIEdgeInsetsZero textView.sizeToFit() [email protected] Remove textContainerInset

Slide 32

Slide 32 text

let textView = UITextView(frame: view.bounds) ... textView.textContainerInset = UIEdgeInsetsZero textView.sizeToFit() [email protected] Remove textContainerInset

Slide 33

Slide 33 text

[email protected] Remove textContainerInset

Slide 34

Slide 34 text

let textView = UITextView(frame: view.bounds) ... textView.textContainer.lineFragmentPadding = 0 textView.sizeToFit() [email protected] Remove lineFragmentPadding

Slide 35

Slide 35 text

let textView = UITextView(frame: view.bounds) ... textView.textContainer.lineFragmentPadding = 0 textView.sizeToFit() [email protected] Remove lineFragmentPadding

Slide 36

Slide 36 text

[email protected] Remove lineFragmentPadding

Slide 37

Slide 37 text

let textView = UITextView(frame: view.bounds) ... textView.layoutManager.usesFontLeading = false textView.sizeToFit() [email protected] Ignore font.leading

Slide 38

Slide 38 text

[email protected] Ignore font.leading @interface NSLayoutManager : NSObject ... // By default, a layout manager will use leading as specified by the font. However, this is not appropriate for most UI text, for which a fixed leading is usually specified by UI layout guidelines. These methods allow the use of the font's leading to be turned off. @property(NS_NONATOMIC_IOSONLY) BOOL usesFontLeading;

Slide 39

Slide 39 text

let textView = UITextView(frame: view.bounds) ... textView.layoutManager.usesFontLeading = false textView.sizeToFit() [email protected] Ignore font.leading

Slide 40

Slide 40 text

[email protected] Ignore font.leading

Slide 41

Slide 41 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] Note: Including font.leading

Slide 42

Slide 42 text

[email protected] Including font.leading

Slide 43

Slide 43 text

[email protected] textView.textContainerInset = UIEdgeInsetsZero textView.textContainer.lineFragmentPadding = 0 textView.layoutManager.usesFontLeading = false Reset Default Margins

Slide 44

Slide 44 text

Displaying Rich Text [email protected]

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

[email protected] Change Line Height

Slide 47

Slide 47 text

[email protected] Change Line Height

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

[email protected] min/maxLineHeight https://github.com/ibireme/YYText MinimumLineHeight MaximumLineHeight

Slide 51

Slide 51 text

[email protected] Line spacing https://github.com/ibireme/YYText

Slide 52

Slide 52 text

[email protected] FirstLineHeadIndent https://github.com/ibireme/YYText

Slide 53

Slide 53 text

[email protected] TextAlignment https://github.com/ibireme/YYText

Slide 54

Slide 54 text

[email protected] Kerning https://github.com/ibireme/YYText

Slide 55

Slide 55 text

Advanced Example of NSAttributedString [email protected]

Slide 56

Slide 56 text

Slide 57

Slide 57 text

Slide 58

Slide 58 text

Slide 59

Slide 59 text

Slide 60

Slide 60 text

Slide 61

Slide 61 text

Slide 62

Slide 62 text

[email protected] Mathematical Formulas

Slide 63

Slide 63 text

[email protected] Mathematical Formulas

Slide 64

Slide 64 text

[email protected] Mathematical Formulas

Slide 65

Slide 65 text

[email protected] Mathematical Formulas [NSFontAttributeName: UIFont(name: "LatinModernMath-Regular"]

Slide 66

Slide 66 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: 18]

Slide 67

Slide 67 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: -26]

Slide 68

Slide 68 text

[email protected] Mathematical Formulas [NSKernAttributeName: -68]

Slide 69

Slide 69 text

[email protected] Mathematical Formulas [String(kCTSuperscriptAttributeName): 1]

Slide 70

Slide 70 text

[email protected] Mathematical Formulas [NSFontAttributeName: font.scale(x: 0.6, y: 0.6)]

Slide 71

Slide 71 text

[email protected] Scaling font extension UIFont { func scale(x x: CGFloat, y: CGFloat) -> UIFont { return transform(CGAffineTransformMakeScale(x, y)) } func transform(matrix: CGAffineTransform) -> UIFont { return UIFont(descriptor: fontDescriptor().fontDescriptorWithMatrix(matrix), size: pointSize) } }

Slide 72

Slide 72 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: 2]

Slide 73

Slide 73 text

[email protected] Mathematical Formulas "\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}"

Slide 74

Slide 74 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: 14]

Slide 75

Slide 75 text

[email protected] Mathematical Formulas [NSKernAttributeName: -12]

Slide 76

Slide 76 text

[email protected] Mathematical Formulas “\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}...”

Slide 77

Slide 77 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: -12]

Slide 78

Slide 78 text

[email protected] Mathematical Formulas [NSKernAttributeName: -135]

Slide 79

Slide 79 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: -12]

Slide 80

Slide 80 text

[email protected] Mathematical Formulas

Slide 81

Slide 81 text

[email protected] Mathematical Formulas

Slide 82

Slide 82 text

[email protected] Mathematical Formulas [NSFontAttributeName: UIFont(name: "LatinModernMath-Regular"]

Slide 83

Slide 83 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: -20]

Slide 84

Slide 84 text

[email protected] Mathematical Formulas [NSKernAttributeName: -14]

Slide 85

Slide 85 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: -8]

Slide 86

Slide 86 text

[email protected] Mathematical Formulas [NSKernAttributeName: -14]

Slide 87

Slide 87 text

[email protected] Mathematical Formulas [NSKernAttributeName: 4]

Slide 88

Slide 88 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: -8]

Slide 89

Slide 89 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: -24]

Slide 90

Slide 90 text

[email protected] Mathematical Formulas [NSFontAttributeName: font.scale(x: 0.6, y: 0.6)]

Slide 91

Slide 91 text

[email protected] Mathematical Formulas [NSKernAttributeName: -20]

Slide 92

Slide 92 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: 10]

Slide 93

Slide 93 text

[email protected] Mathematical Formulas [NSFontAttributeName: font.scale(x: 0.6, y: 0.6)]

Slide 94

Slide 94 text

[email protected] Mathematical Formulas [NSKernAttributeName: -14]

Slide 95

Slide 95 text

[email protected] Mathematical Formulas [String(kCTSuperscriptAttributeName): -1]

Slide 96

Slide 96 text

[email protected] Mathematical Formulas [NSFontAttributeName: font.scale(x: 0.6, y: 0.6)]

Slide 97

Slide 97 text

[email protected] Mathematical Formulas [String(kCTSuperscriptAttributeName): 1]

Slide 98

Slide 98 text

[email protected] Mathematical Formulas [NSFontAttributeName: font.scale(x: 0.6, y: 0.6)]

Slide 99

Slide 99 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: -8]

Slide 100

Slide 100 text

[email protected] Mathematical Formulas [NSFontAttributeName: font.scale(x: 1, y: 2.4)]

Slide 101

Slide 101 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: 20]

Slide 102

Slide 102 text

[email protected] Mathematical Formulas [NSKernAttributeName: 2]

Slide 103

Slide 103 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: 16]

Slide 104

Slide 104 text

[email protected] Mathematical Formulas [NSKernAttributeName: -118]

Slide 105

Slide 105 text

[email protected] Mathematical Formulas [NSBaselineOffsetAttributeName: -8]

Slide 106

Slide 106 text

[email protected] https://github.com/ kishikawakatsumi/TextKitExamples

Slide 107

Slide 107 text

Summary • No longer use CoreText directly • Carefully to choose font. Text layout is based on font metrics • The most important is constructing NSAttributedString [email protected]