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
英語と日本語の文字をComposeでキレイに並べよう
Search
TomoyaShibata
March 14, 2025
Technology
1
330
英語と日本語の文字をComposeでキレイに並べよう
TomoyaShibata
March 14, 2025
Tweet
Share
Other Decks in Technology
See All in Technology
SES向け、生成AI時代におけるエンジニアリングとセキュリティ
longbowxxx
0
290
ESXi のAIOps だ!2025冬
unnowataru
0
470
AI時代のアジャイルチームを目指して ー スクラムというコンフォートゾーンからの脱却 ー / Toward Agile Teams in the Age of AI
takaking22
9
3.3k
自己管理型チームと個人のセルフマネジメント 〜モチベーション編〜
kakehashi
PRO
5
1.6k
歴史から学ぶ、Goのメモリ管理基礎
logica0419
10
1.9k
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
12k
2025年の医用画像AI/AI×medical_imaging_in_2025_generated_by_AI
tdys13
0
290
[PR] はじめてのデジタルアイデンティティという本を書きました
ritou
0
750
Oracle Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
3
260
『君の名は』と聞く君の名は。 / Your name, you who asks for mine.
nttcom
1
140
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
2.9k
プロンプトエンジニアリングを超えて:自由と統制のあいだでつくる Platform × Context Engineering
yuriemori
0
140
Featured
See All Featured
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
0
33
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.5k
Code Review Best Practice
trishagee
74
19k
Utilizing Notion as your number one productivity tool
mfonobong
2
190
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
1
220
Highjacked: Video Game Concept Design
rkendrick25
PRO
0
260
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
39
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
Mobile First: as difficult as doing things right
swwweet
225
10k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
33
[SF Ruby Conf 2025] Rails X
palkan
0
680
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Transcript
英語と日本語の文字を Composeでキレイに並べよう id:bps_tomoya / @tomoya_shibata 2025/03/14 DroidKaigi.collect { #17@Tokyo }
1
自己紹介 • バトルプログラマー柴田智也 ◦ id:bps_tomoya ◦ @tomoya_shibata • 株式会社はてな •
AndroidときどきiOS 2
3 2つのTextが 横に並ぶ実装を触っていた ある日
2つのTextを横に並べる • ちょっとしたラベル、バッジのような表示が欲しい • それぞれ背景(あるいは枠線など)を持っている • 短い文字列が2つ並ぶ、とてもありふれた要件 4
実装はこんな感じ 5
これで問題ないのでは 🤔 6
日本語に対して英語がちょっと浮いている 7
8 それぞれの Textのベースラインを 揃えたい
Modifier.alignByBaseline() 9 • Rowの中で使える • 設定された要素同士のベースラインを揃える • Text以外にも色々使える
こんな感じ 10
うん? 11
うん? 12 ベースラインは揃った
うん? 13 代わりにTextそのものがズレてしまった
背景色を持っていたりするとうまくいかない 14 • 逆にいえば背景色など無ければこれで大丈夫 • ベースラインがあらかじめ揃えられた日本語・英語の フォントをアプリに入れる? • それともText要素のサイズを計測して差分を埋める? •
要件に対して解決方法がちょっと派手
15 LineHeightStyle API
LineHeightStyle API • 行の中での配置や余白の取 り方を指定できる • Compose 1.2.0+ 16 https://developer.android.com/reference/kotlin/androidx/compose/ui/text/style/LineHeightStyle
LineHeightStyle.Alignment • 行の高さの中で文字をどう配置するか • Top ◦ 下にスペースを入れて上に詰める • Bottom ◦
上にスペースを入れて下に詰める • Proportional(デフォルト) ◦ フォントが持っているメトリクスを 元にスペース配分する • Center ◦ 上下に同量のスペースを入れる 17 https://medium.com/androiddevelopers/fixing-font-padding-in-compose-text-768cd232425b
LineHeightStyle.Alignment 18 • 0~1または-1fの値を渡すこともできる • 使う機会は少ないかも
LineHeightStyle.Trim • 1行目と最終行に含まれるスペースを取 り除くかどうか • FirstLineTop ◦ 1行目の上部分をtrimする • LastLineBottom
◦ 最終行の下部分をtrimする • None ◦ なにもtrimしない • Both(デフォルト) ◦ FirstLineTop/Bottomの両方 19 https://medium.com/androiddevelopers/fixing-font-padding-in-compose-text-768cd232425b
LineHeightStyle.Mode 20 • 行の高さがシステム推奨値より小さいときどうするか • Fixed(デフォルト) ◦ 指定された値を優先 • Minimum
◦ システム推奨値を優先 • Compose 1.8.0-beta03+ ◦ 今回のテーマでは登場しませんが、ご紹介まで
いま解決したいのは 21 • 英語と日本語のベースラインを揃えたい • フォントメトリクスで配置されるといい ◦ Alignment.Proportional(デフォルト) • 上下の余白を取り除かれるのは困る
◦ Trim.None
22 今回は Alignment.Proportional Trim.None の組み合わせがよさそう
こんな感じ 23
どうかな? 24
よさそう! 25
行が複数になると分かりやすい Trim.Both(デフォルト) Trim.None 26
まとめ • ベースラインを揃えたいときは Modifier.alignByBaseline • 背景色があるなど場面によっては上手くいか ないパターンも • LineHeightStyle APIを使って調整しよう
27