:sparkles: Today I loaded a custom font in Compose/iOS :sparkles: It was not an easy experience, so ...
d
✨ Today I loaded a custom font in Compose/iOS ✨ It was not an easy experience, so hopefully this can save someone's sanity šŸ™ƒ I think there's a bug around the
identity
field... Take the iOS function:
Copy code
androidx.compose.ui.text.platform.Font(
    identity: String,
    data: ByteArray,
    weight: FontWeight = FontWeight.Normal,
    style: FontStyle = FontStyle.Normal
): Font
The
identity
field is documented as:
identity - Unique identity for a font. Used internally to distinguish fonts.
...which suggests it can be any arbitrary, unique string right? Seemingly not. See in 🧵 šŸ‘‰ for more...
I can't identify the pattern here so I'll just share my results: • My first thought was to just use the Font filename for the
identity
- easy, right? Let's call the font InterStellar, so I had five variants to load: ā—¦
InterStellar-Regular
ā—¦
InterStellar-Bold
ā—¦
InterStellar-Light
ā—¦
InterStellar-SemiBold
ā—¦
InterStellar-Medium
• Using these above strings as identifiers causes a crash šŸ’„; not at the point of loading the Font, but rather at the point of trying to render the font:
Copy code
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableDictionary __addObject:forKey:]: object cannot be nil'
Note at this point, I had no reason to believe the identifier was causing the issue; I thought this crash could be coming from anywhere in the Font rendering system... it was only after several hours of trial and error 😪 that I trimmed the
FontFamily
down to one font with the identifier
test
and... it rendered!
Here's where it starts to get weird... If I have two fonts with
isa
and
isb
as names, that crashes šŸ’„
If I have the same two fonts with
test
and
tsst
as names, it works āœ…
I thought that maybe, in addition to minimum length, maybe the dashes in the original names were rejected, so I filtered those out... still crash šŸ’„
Are uppercase letters a problem? I lower case it all so now we have e.g.
interstellarbold
,
interstellarlight
etc... nope, crash šŸ’„
Only around 4-6 characters seems to work, not to short, not too long āœ… alphabet yellow question
I ended up implementing a static counter in my iOS loader so now I have
font1
,
font2
,
font3
etc. as internal identifiers.
This works, but I don't know why šŸ¤”
@Dima Avdeev Maybe this will be of some use... ā˜ļø
d
Yeah, it's very interesting, Thanks! Do you have public repo with this experiments?
d
Not at present. I'll try to reproduce one, but that was enough for today šŸ™‚
c
how are you reading the font content to send it as
data
@darkmoon_uk? šŸ‘¼