Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Handling rich text in Swift
Search
Kishikawa Katsumi
August 20, 2016
Programming
17
6.5k
Handling rich text in Swift
Handling rich text
in Swift
iOSDC Japan 2016
Kishikawa Katsumi
August 20, 2016
Tweet
Share
More Decks by Kishikawa Katsumi
See All by Kishikawa Katsumi
iOSDC 2024 SMBファイル共有をSwiftで実装する
kishikawakatsumi
1
170
Enhancing Applications with Accessibility API
kishikawakatsumi
3
3.4k
Mastering SwiftSyntax
kishikawakatsumi
4
5.5k
My SwiftData Review
kishikawakatsumi
7
1.4k
Swift Expression Macros: a practical introduction
kishikawakatsumi
3
1.9k
Xcode Cloudの評価
kishikawakatsumi
2
1.3k
Regular expressions basics/正規表現の基本
kishikawakatsumi
7
730
家のいろいろな数値を計測する
kishikawakatsumi
4
2.2k
GitHub Actionsでテストの結果をわかりやすく表示する
kishikawakatsumi
1
1.1k
Other Decks in Programming
See All in Programming
Refactor your code - refactor yourself
xosofox
1
260
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
370
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
120
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
270
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
210
たのしいparse.y
ydah
3
120
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
160
Full stack testing :: basic to basic
up1
1
930
return文におけるstd::moveについて
onihusube
1
920
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Building an army of robots
kneath
302
44k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
We Have a Design System, Now What?
morganepeng
51
7.3k
Navigating Team Friction
lara
183
15k
Faster Mobile Websites
deanohume
305
30k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Visualization
eitanlees
146
15k
Transcript
Handling rich text in Swift iOSDC Japan 2016 Katsumi Kishikawa
[email protected]
Katsumi Kishikawa Realm Inc.
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Agenda ˖ J04ךذؗأزٖ؎،ؐزחאְג ˖ ة؎هؚٓؿ؍ך㛇燉 ˖ 5FYU,JUך㛇燉ה䘔欽
[email protected]
iOS 7Ҏ߱ͷςΩετϨΠΞτ
[email protected]
TextKit
[email protected]
TextKitͱ ˖ 넝鸞זٌتٝذؗأزٖ؎،ؐز٥ٖٝت ؚٔٝؒٝآٝ ˖ $PSF5FYUד⡲גְ ˖ 6*,JUהך窟さ
[email protected]
https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/CustomTextProcessing/CustomTextProcessing.html
UILabel
[email protected]
[email protected]
UILabelΛී௨ʹ͏
[email protected]
ώϥΪϊͱγεςϜϑΥϯτ UIFont(name: "HiraginoSans-W3", size: 32) UIFont.systemFontOfSize(32)
[email protected]
ώϥΪϊͱγεςϜϑΥϯτ
[email protected]
ώϥΪϊͱγεςϜϑΥϯτ
[email protected]
General Header
[email protected]
ΞϧϑΝϕοτΛؚ·ͳ͍߹
ߦͷߴ͞ԿͰܾ·Δ͔
[email protected]
[email protected]
Font metrics https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/FontHandling/FontHandling.html
[email protected]
Font metrics https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/FontHandling/FontHandling.html
[email protected]
Font metrics
[email protected]
Font metrics Baseline X-Height Cap height Ascent Descent
[email protected]
ώϥΪϊಛघͳϝτϦΫεΛ࣋ͭ
[email protected]
ʢࢀߟʣNoto Sans CJK JP
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Λิ͏
[email protected]
্ԼʹDescentΛิ͏
[email protected]
্ԼʹDescentΛิ͏
[email protected]
ΦϦδφϧͱൺֱ
• ώϥΪϊΛ໌ࣔతʹࢦఆ͠ͳ͍ • γεςϜϑΥϯτΛ͏ • σβΠϯࢦࣔॻͳͲͰɺώϥΪϊ͍͍ͨͯγε ςϜϑΥϯτΛҙਤ͍ͯ͠Δͣ • தࠃޠ༻ϑΥϯτʹϑΥʔϧόοΫͨ͘͠ͳ͍Մ ೳੑ
• ώϥΪϊΛࢦఆ͢Δ߹ɺΞϧϑΝϕοτʹؾΛ ͚ͭΔ
[email protected]
ώϥΪϊͷϝτϦΫεʹҙ
[email protected]
Tips: ຊޠϑΥϯτʹϑΥʔϧόοΫͤ͞Δ let attributes = [ NSFontAttributeName: font, kCTLanguageAttributeName
as String: "ja", ... ]
αΠζͷܭࢉ
[email protected]
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]
ඳըαΠζΛಘΔ
[email protected]
ඳըαΠζΛಘΔ
UITextView
[email protected]
[email protected]
UITextViewΛී௨ʹ͏
[email protected]
UITextViewΛී௨ʹ͏ UIFont(name: "HiraginoSans-W3", size: 32) UIFont.systemFontOfSize(32)
[email protected]
ඳըαΠζ
[email protected]
ඳըαΠζ
[email protected]
σϑΥϧτͷϚʔδϯ
σϑΥϧτͷϚʔδϯ textView.textContainer.lineFragmentPadding textView.textContainerInset UIEdgeInsets(top: 8.0, left: 0.0, bottom: 8.0, right:
0.0) 5.0
[email protected]
[email protected]
ώϥΪϊΛࢦఆͨ͠߹ font.leading
σϑΥϧτͷελΠϧΛϦηοτ͢Δ
[email protected]
let textView = UITextView(frame: view.bounds) ... textView.textContainerInset = UIEdgeInsetsZero textView.sizeToFit()
[email protected]
textContainerInsetΛऔΓআ͘
let textView = UITextView(frame: view.bounds) ... textView.textContainerInset = UIEdgeInsetsZero textView.sizeToFit()
[email protected]
textContainerInsetΛऔΓআ͘
[email protected]
textContainerInsetΛऔΓআ͘
let textView = UITextView(frame: view.bounds) ... textView.textContainer.lineFragmentPadding = 0 textView.sizeToFit()
[email protected]
lineFragmentPaddingΛऔΓআ͘
let textView = UITextView(frame: view.bounds) ... textView.textContainer.lineFragmentPadding = 0 textView.sizeToFit()
[email protected]
lineFragmentPaddingΛऔΓআ͘
[email protected]
lineFragmentPaddingΛऔΓআ͘
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]
ඳըαΠζΛಘΔ
[email protected]
ඳըαΠζΛಘΔ
let textView = UITextView(frame: view.bounds) ... textView.layoutManager.usesFontLeading = false textView.sizeToFit()
[email protected]
font.leadingΛऔΓআ͘
let textView = UITextView(frame: view.bounds) ... textView.layoutManager.usesFontLeading = false textView.sizeToFit()
[email protected]
font.leadingΛऔΓআ͘
[email protected]
font.leadingΛऔΓআ͘
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ΛؚΊͯܭࢉ͢Δ
[email protected]
ʢผղʣfont.leadingΛؚΊͯܭࢉ͢Δ
[email protected]
ʢผղʣfont.leadingΛؚΊͯܭࢉ͢Δ
[email protected]
textView.textContainerInset = UIEdgeInsetsZero textView.textContainer.lineFragmentPadding = 0 textView.layoutManager.usesFontLeading = false
σϑΥϧτελΠϧͷϦηοτ
͞·͟·ͳελΠϧʹରԠ͢Δ
[email protected]
[email protected]
ߦؒͷมߋ
NSAttributedString
[email protected]
NSParagraphStyle
[email protected]
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
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
[email protected]
ߦؒͷมߋ
[email protected]
ߦؒͷมߋ
let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.minimumLineHeight = ceil(font.lineHeight) paragraphStyle.maximumLineHeight = ceil(font.lineHeight)
paragraphStyle.lineSpacing = 8
[email protected]
min/maxLineHeight
[email protected]
min/maxLineHeight
[email protected]
minimumLineHeight
[email protected]
maximumLineHeight
NSAttributedStringΛ׆༻͢Δ
[email protected]
[email protected]
͞ΒʹߦؒΛ͘
[email protected]
͞ΒʹߦؒΛ͘
[email protected]
Օॻ͖
·ͱΊ • iOS 7Ҏ߱ͷςΩετϨΠΞτɺ͍͔ʹ ΩϨΠͳNSAttributedStringΛ࡞Δ͔ • λΠϙάϥϑΟͷجૅΛΖ͏ • ϑΥϯτʹώϥΪϊΛࢦఆ͢Δͱ͖ҙ •
Ͱ͖Δ͚ͩγεςϜϑΥϯτΛ͓͏
[email protected]
Questions? Katsuma Kishikawa
[email protected]
www.realm.io/jp @k_katsumi