https://kotlinlang.org logo
#compose
Title
# compose
c

Colton Idle

07/14/2022, 5:11 AM
Is there any way to programatically retrieve the font family applied to text? I'm building a text debug menu in my app and it seems like for each text style I can programatically access all attributes about the style, (weight, size, line height) but not the name of the Font (i.e. Roboto or my other custom font). Ideas?
c

Chris Sinco [G]

07/14/2022, 5:38 AM
cc @Sean McQuillan [G]
z

Zoltan Demant

07/14/2022, 9:17 AM
Doesnt
MaterialTheme.typography.body2.fontFamily
(etc)
toString
provide a pretty good (maybe not perfect though) name for them? Digging through the code it seems to use the font names, unfortunately I cant test it myself right now homer disappear
c

Colton Idle

07/14/2022, 6:42 PM
Hm. Tried that and I just got a resource Id I believe
s

Sean McQuillan [G]

07/14/2022, 7:26 PM
Uh not in first class API
Let me see if there's another way
🤞 2
c

Chris Sinco [G]

07/14/2022, 7:48 PM
I’ve personally wanted this myself for Theme preview reasons
c

Colton Idle

07/14/2022, 7:54 PM
lmao. thats basically exactly what im doing.
trying to build a thing where if you click any of my text in the app you will get a message of what params that font has. font family, size, etc. lol
👀 1
s

Sean McQuillan [G]

07/14/2022, 7:59 PM
No
in the generic case
I thought you'd be able to reflect into Typeface to check the names in the system font map, but you won't be able to do a instance or equality check on the results due to instantiation in create(String) https://cs.android.com/android/platform/superproject/+/master:frameworks/base/graphics/java/android/graphics/Typeface.java;l=154?q=Typeface
that'll work sometimes though, but sometimes fail
If you control the (Compose) Font then there's an exit, you can define a subclass of AndroidFont that exposes the name https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]in/androidx/compose/ui/text/font/AndroidFont.kt?q=AndroidFont
Basically, just copy the existing Font declarations and add what you want
As for answering "From a font family, and a text style, what font is chosen" that's not public API - it's done in FontMatcher which returns a font fallback list https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]droidx/compose/ui/text/font/FontMatcher.kt;l=28?q=FontMatcher
The actual "what font is being displayed right now" is not public API (or any API 🙂 )
c

Colton Idle

07/14/2022, 8:04 PM
Interesting. I'm gonna put a pin in this for now, but Chris if you ever take a stab at this... let me know. got some deadlines to hit by end of next week so im not gonna spend too many cycles on this. Appreciate it sean! nice to know i didn't miss something basic!
s

Sean McQuillan [G]

07/14/2022, 8:06 PM
It's not what you're looking for, but we did put the params into Font.toStrings
np! Good luck - sorry the answer got complicated 😄
❤️ 2
48 Views