Hi guys Im using font in the CMP project(UI in the...
# multiplatform
k
Hi guys Im using font in the CMP project(UI in the jetpack compose), I placed my font files in the commonMain->composeResouces->font And when I'm trying to access the font via Res.font.<name> I got error in the logcat: java.lang.IllegalStateException: Could Could not load font Attaching logcat error, please help with this.
Process: com.roposo.android.debug, PID: 24934 java.lang.IllegalStateException: Could not load font at androidx.compose.ui.text.font.TypefaceRequestCache.runCached(FontFamilyResolver.kt:207) at androidx.compose.ui.text.font.FontFamilyResolverImpl.resolve(FontFamilyResolver.kt:92) at androidx.compose.ui.text.font.FontFamilyResolverImpl.resolve-DPcqOEQ(FontFamilyResolver.kt:79) at androidx.compose.ui.text.platform.AndroidParagraphIntrinsics$resolveTypeface$1.invoke-DPcqOEQ(AndroidParagraphIntrinsics.android.kt:92) at androidx.compose.ui.text.platform.AndroidParagraphIntrinsics$resolveTypeface$1.invoke(AndroidParagraphIntrinsics.android.kt:90) at androidx.compose.ui.text.platform.extensions.TextPaintExtensions_androidKt.applySpanStyle(TextPaintExtensions.android.kt:63) at androidx.compose.ui.text.platform.AndroidParagraphIntrinsics.<init>(AndroidParagraphIntrinsics.android.kt:109) at androidx.compose.ui.text.platform.AndroidParagraphIntrinsics_androidKt.ActualParagraphIntrinsics(AndroidParagraphIntrinsics.android.kt:184) at androidx.compose.ui.text.ParagraphIntrinsicsKt.ParagraphIntrinsics(ParagraphIntrinsics.kt:98) at androidx.compose.ui.text.ParagraphIntrinsicsKt.ParagraphIntrinsics$default(ParagraphIntrinsics.kt:91) at androidx.compose.foundation.text.modifiers.ParagraphLayoutCache.setLayoutDirection(ParagraphLayoutCache.kt:250) at androidx.compose.foundation.text.modifiers.ParagraphLayoutCache.layoutText-K40F9xA(ParagraphLayoutCache.kt:273) at androidx.compose.foundation.text.modifiers.ParagraphLayoutCache.layoutWithConstraints-K40F9xA(ParagraphLayoutCache.kt:183) at androidx.compose.foundation.text.modifiers.TextStringSimpleNode.measure-3p2s80s(TextStringSimpleNode.kt:326) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.measure-BRTryo0(LayoutModifierNodeCoordinator.kt:116) at androidx.compose.ui.graphics.SimpleGraphicsLayerModifier.measure-3p2s80s(GraphicsLayerModifier.kt:646) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.measure-BRTryo0(LayoutModifierNodeCoordinator.kt:116) at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasureBlock$1.invoke(LayoutNodeLayoutDelegate.kt:252) at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasureBlock$1.invoke(LayoutNodeLayoutDelegate.kt:251) at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2303) at androidx.compose.runtime.snapshots.SnapshotStateObserver$ObservedScopeMap.observe(SnapshotStateObserver.kt:500) at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:256) at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:133) at androidx.compose.ui.node.OwnerSnapshotObserver.observeMeasureSnapshotReads$ui_release(OwnerSnapshotObserver.kt:113) at androidx.compose.ui.node.LayoutNodeLayoutDelegate.performMeasure-BRTryo0(LayoutNodeLayoutDelegate.kt:1617) at androidx.compose.ui.node.LayoutNodeLayoutDelegate.access$performMeasure-BRTryo0(LayoutNodeLayoutDelegate.kt:36) at androidx.compose.ui.node.LayoutNodeLayoutDelegate$MeasurePassDelegate.remeasure-BRTryo0(LayoutNodeLayoutDelegate.kt:620) at androidx.compose.ui.node.LayoutNodeLayoutDelegate$MeasurePassDelegate.measure-BRTryo0(LayoutNodeLayoutDelegate.kt:596) at androidx.compose.foundation.layout.BoxMeasurePolicy.measure-3p2s80s(Box.kt:144) at androidx.compose.ui.node.InnerNodeCoordinator.measure-BRTryo0(InnerNodeCoordinator.kt:126) at androidx.compose.foundation.layout.FillNode.measure-3p2s80s(Size.kt:699) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.measure-BRTryo0(LayoutModifierNodeCoordinator.kt:116) at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasureBlock$1.invoke(LayoutNodeLayoutDelegate.kt:252) at androidx.compose.ui.node.LayoutNodeLayoutDelegate$performMeasureBlock$1.invoke(LayoutNodeLayoutDelegate.kt:251) at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2303)
Copy code
composeVersion = "1.6.1"
kotlin = "1.9.22"
composePlugin = "1.6.0"
composeRuntime = "1.6.1"
java 17
Im using these version.
v
Are you able to access drawables?
k
Yes
They are working fine
@Vivek Choudhary
v
Copy code
Text(
                        text = photo.user.name,
                        maxLines = 2,
                        fontSize = 12.sp,
                        fontFamily = FontFamily(Font(Res.font.Roboto_Black)),
                        modifier = Modifier.weight(1f, fill = false).clickable {
                            onProfileImageClick(photo.user.links.profileLink)
                        }
                    )
Works for me. Could you try rebuilding the project?
I am using java 11 btw, not sure if it has something to do with the error.
k
which versions for cmpose and other stuff ?
y
just go to build -> clean project then run it should work
k
Tried 100 times :)
And drawables are working but not font
y
it could be because a certain font doesn't exist like bold or something like that could you show how are you setting your font
so if you have a list of font like so
Copy code
internal val CairoFontFamily: FontFamily
    @Composable
    get() =
        FontFamily(
            Font(
                resource = Res.font.cairo_bold,
                weight = FontWeight.Bold,
            ),
            Font(
                Res.font.cairo_extra_bold,
                weight = FontWeight.ExtraBold,
            ),
            Font(
                Res.font.cairo_light,
                weight = FontWeight.Light,
            ),
            Font(
                Res.font.cairo_medium,
                weight = FontWeight.Medium,
            ),
            Font(
                Res.font.cairo_extra_light,
                weight = FontWeight.ExtraLight,
            ),
            Font(
                Res.font.cairo_regular,
                weight = FontWeight.Normal,
            ),
            Font(
                Res.font.cairo_semi_bold,
                weight = FontWeight.SemiBold,
            ),
        )
try only with one font and comment out the rest sometimes you may miss to add a certain one, this happened to me before and even tho the font did exist in the font folder but for some reason it didn't load
1
k
Nope, still same error