Share
Embed
Transcript
68 slides
-
SwiftUI Text Rendering Deep Dive Text(“\(price)”)がピクセルになるまで elmetal iOSDC Japan 2026
2026-09-12 #iosdc #d 1 -
Textは簡単ではない • 普段何気なく使っているText • 簡単ではない • ロケールを変えるとレイアウトが崩れる • TextとStringでローカライズの挙動が異なる •
AttributedStringの一部だけスタイルを変えたい • テキストエフェクトをカスタムしたい #iosdc #d 4 -
値が画面に表示されるまで Value 画面への表示 Formatting Text Construction Layout #iosdc #d 5
表示内容の構築 Rendering Pixels -
FormatStyleとStringInterpolation Value 画面への表示 Formatting Text Construction Layout #iosdc #d 7
表示内容の構築 Rendering Pixels -
Textは同じ表示を複数の書き方で作れる Text("¥1,234") これらはTextに渡す値を 作る途中経過が異なる Text(verbatim: "¥1,234") Text(price, format: .currency(code: "JPY”))
Text(verbatim: price.formatted(.currency(code: "JPY"))) #iosdc #d 10 -
値とFormatStyleをTextへ渡す Text(price, format: .currency(code: "JPY")) • Textの中でフォーマットが行われる • SwiftUIのenvironmentを使ってLocaleを表示に反映できる •
先にフォーマットする場合はFormatStyleのlocaleが使われるので、SwiftUI のenvironmentではLocaleが反映されない #iosdc #d 14 -
Text.StringInterpolation Text("価格は\(price, format: .currency(code: "JPY"))です") • リテラルはText.StringInterpolationで処理される • Text.StringInterpolationにより、「価格は¥1,234です」全体を一つのローカライズ単位と して扱える
• 翻訳リソース側では値をプレースホルダーとして動的に組み込める • 文全体の表現や語順を変更できる • TextのInterpolationに値とFormatStyleを渡すことで、リテラル、値、FormatStyleを統合 して、localizable textにできる #iosdc #d 21 -
LocaleとICUによる国際化処理 Value 画面への表示 Formatting Text Construction Layout #iosdc #d 23
表示内容の構築 Rendering Pixels -
通貨フォーマットを支えるもの FormatStyleのCon gurationとLocaleをもとにICUが表示規則を適用 1234 ICU 表示規則を適用 • FormatStyleのCon guration .currency(code:
“JPY”) • Locale ja̲JP #iosdc #d fi fi 25 ¥1,234 -
FormatStyleのCon guration • Con gurationでは値の表現方法を精度・桁区切り・符号などの要素ごとに指定する • 例えばNumberFormatStyleCon guration • currency:
JPY • Precision: integer • Grouping: automatic • Sign: automatic • 表現方法の指定から具体的な記号がどうやって決まるのか? #iosdc #d fi fi fi 27 -
ICU(International Components for Unicode) Unicode対応の文字列処理や、Localeに応じた書式設定を提供する国際化ライブラリ • Swift Foundationの国際化ではICUが利用されている • FormatStyleの設定とLocaleを使ってICU
number formatterを構成する • ICUが通過記号・区切り・配置などをLocaleに応じて解決 • Swift FoundationではICUNumberFormatter.swiftから実装を追える • 実装の詳細は省略 • ここで表示用の文字列に変換される #iosdc #d 28 -
AttributedString Value 画面への表示 Formatting Text Construction Layout #iosdc #d 30
表示内容の構築 Rendering Pixels -
AttributedString 文字列の範囲ごとに属性を持てる値型 ¥ currency symbol 1 integer , grouping separater
234 integer #iosdc #d 32 -
AttributedScopes 用途ごとに属性を整理 • AttributedStringで扱える属性は見た目だけではない • AttributedScopesで用途ごとに属性が整理されている • FoundationAttributes • SwiftUIAttributes
/ UIKitAttributes / AppKitAttributes • Custom Attributes • 型でスコープを分けることで属性名の衝突を避けて属性追加ができる #iosdc #d 33 -
AttributedString.Runs • AttributedString.Runsで属性が一定範囲ごとに分割される • 隣り合う文字が同じ属性を持っていれば一つのrunにまとまる ¥ currency symbol 1 integer
, grouping separater 234 integer #iosdc #d 34 -
FormatStyleでAttributedStringを返す let attributed = price.formatted( .currency(code: "JPY").attributed ) for run
in attributed.runs { print(attributed[run.range]) print(run.attributes) } #iosdc #d 36 -
FormatStyleでAttributedStringを返す let attributed = price.formatted( .currency(code: "JPY").attributed ) ¥ currency
symbol 1 integer for run in attributed.runs { print(attributed[run.range]) print(run.attributes) } , grouping separater 234 #iosdc #d 37 integer -
SwiftUIのテキストレイアウト Value 画面への表示 Formatting Text Construction Layout #iosdc #d 40
表示内容の構築 Rendering Pixels -
A text view always uses exactly the amount of space
it needs to display its rendered contents, but you can a ect the viewʼs layout. Text ̶ https://developer.apple.com/documentation/swiftui/text #iosdc #d ff 43 -
Textのレイアウト結果に影響する属性 タイポグラフィ Font ローカライズ Locale Layout direction Dynamic type size
Line spacing Line limit Multiline text alignment 段落設定 Available width Truncation #iosdc #d 46 表示制約 -
レイアウト結果への影響の例 文字とグリフが1対1にならない • 複数のunicode scalarが一つの文字を構成する • 複数の文字が合字によって一つのグリフになる • 前後の文字に応じてグリフが変わる •
使用するフォントによってグリフの形や幅が変わる #iosdc #d 47 -
シェーピング 文字列をフォントに応じたグリフ列に変換する 文字列 フォント情報 シェーピング • グリフの選択 • 位置の調整 #iosdc
#d 49 グリフ列 グリフ同士の配置 -
Text.Layout • Text.Layoutはレイアウト済みテキ ストの結果 Text.Layout ├─ Line │ ├─ Run
│ │ ├─ Glyph │ │ └─ Glyph │ └─ Run └─ Line • 複数のLineで構成され、各Line は複数のRunを持つ • Runにはシェーピングされ、位 置が決まったGlyphが含まれる • TextRendererがこの構造を走査し て描画する #iosdc #d 51 -
TextRendererと描画 Value 画面への表示 Formatting Text Construction Layout #iosdc #d 55
表示内容の構築 Rendering Pixels -
TextRenderer SwiftUIのTextの描画を置き換えるプロトコル • レイアウト済みのText.Layoutを受け取る • GraphicsContextへ描画する • .textRenderer(̲:)をつけたView階層内のTextに適用される SwiftUIによるカスタムビジュアルエフェクトの作成 -
WWDC24 - https://developer.apple.com/jp/videos/play/wwdc2024/10151/ #iosdc #d 56 -
draw(layout: in:) レイアウト結果と描画先を受け取る • layoutからLine・Run・RunSlice を走査する func draw( layout: Text.Layout,
in context: inout GraphicsContext ) • contextへ各要素を描画する • TextRendererはレイアウト結果 を描画するだけで、再レイアウト はしない SwiftUIによるカスタムビジュアルエフェクトの作成 - WWDC24 - https://developer.apple.com/jp/videos/play/wwdc2024/10151/ #iosdc #d 57 -
描画する単位 Text.Layoutを走査 • Lineを描画すると、行単位で効 果を適用できる Text.Layout ├─ Line │ ├─
Run │ │ ├─ RunSlice │ │ └─ RunSlice │ └─ Run └─ Line • Runを描画すると、属性のまとま りごとに処理できる • RunSliceを利用すると、グリフ や埋め込み要素ごとに処理でき る #iosdc #d 58 -
RunをGraphicsContextに描画する typographicBoundsを参照して描画する struct BoundsTextRenderer: TextRenderer { func draw( layout: Text.Layout,
in context: inout GraphicsContext ) { for line in layout { for run in line { let rect = run.typographicBounds.rect context.stroke( Path(rect), with: .color(.red), lineWidth: 1 ) context.draw(run) } } } } #iosdc #d 60 -
TextAttributeで描画を変更したいRunを指定する • TextAttributeはTextにつけるカスタム属性 • TextRendererはRunからTextAttributeを参照できる • TextAttribute自体は見た目を変更しない • TextRendererでTextAttributeを条件に描画方法を変更できる •
テキストの内容やローカライズによってrunの分割が変わっても TextAttributeを参照することで視覚効果をテキスト内容と分離して扱える #iosdc #d 61 -
AttributedStringとTextAttributeの違い AttributedString TextAttribute 対象 文字列の範囲 Textの範囲 主な用途 内容と書式を保持する TextRendererへ対象の情報を渡す レイアウトへの影響
フォントなどの属性が影響する 属性そのものはレイアウトを変更しない 描画 Textの描画に反映される TextRendererが解釈すると反映される #iosdc #d 62 -
描画が元の領域をはみ出す場合 • Shadow、ぼかし、移動はレイアウト済み領域の外まで描画される • 描画効果を加えても、元のTextのレイアウト領域は自動では広がらない • 必要に応じてRenderer側で表示の余白やサイズを考慮する • displayPaddingで領域外へ広がる描画が切れないようにする •
Textの折り返しや配置は変わらない • View.paddingはレイアウト上の余白を扱うので少し役割が異なる #iosdc #d 63 -
TextがPixelになるまで Value 画面への表示 Formatting Text Construction Layout #iosdc #d 66
表示内容の構築 Rendering Pixels -
Textの見た目は段階ごとに決まる • 表示内容を変える • FormatStyle/AttributedString • 配置を変える • Layout •
描画を変える • TextRenderer