英文:
Using fonts in a Compose Multiplatform project
问题
我正在寻找一种在Compose多平台项目中轻松使用自定义字体的方法。
我发现我们需要使用androidx.compose.ui.text.platform.Font
包中的Font
对象。但是,该对象接受data: ByteArray
作为参数。
到目前为止,我还没有找到一种方法可以在commonMain
资源目录中导入字体文件并使用Font
。
如何在Compose多平台项目的commonMain
部分中使用自定义字体?
感谢您的帮助!
英文:
I am looking for a way to easily use custom fonts in a Compose Multiplatform project.
I found that we need to use Font
from the androidx.compose.ui.text.platform.Font
package. But this object takes in parameter data: ByteArray
.
Until now, I haven't found a way to use Font
to import a font file from the commonMain
resource directory.
How to use custom fonts in the commonMain
part of a Compose Multiplatform project?
Thank you for your help!
答案1
得分: 2
最简单的方法是使用moko资源:https://github.com/icerockdev/moko-resources
将字体添加到commonMain/resources/MR/fonts目录中
例如:PlayfairDisplay-Bold.ttf文件
然后加载FontFamily:
val fontFamilyExtraBold: FontFamily = fontFamilyResource(SharedRes.fonts.PlayfairDisplay.extraBold)
最后,在您的主题中的Typography中使用它。有关更多信息,请参阅Moko资源文档。
英文:
The easiest way is to use moko resources : https://github.com/icerockdev/moko-resources
Add the font in commonMain/resources/MR/fonts
for example: PlayfairDisplay-Bold.ttf file
Then load the FontFamily :
val fontFamilyExtraBold: FontFamily = fontFamilyResource(SharedRes.fonts.PlayfairDisplay.extraBold)
And finaly use it in your Typography in your theme. More infos on Moko resources docs.
答案2
得分: 2
以下是您要翻译的内容:
There is a easy way to use common resources directory for both android and desktop.
创建 src/commonMain/resources/
目录在 :shared
模块内。
然后将 android
和 jvm
的 android 源集设置如下:
kotlin {
targetHierarchy.default()
android {
compilations.all {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
jvm {
sourceSets {
named("jvmMain") {
resources.srcDir("src/commonMain/resources") // <============= 这里
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
// compose libraries
implementation(compose.runtime)
implementation(compose.foundation)
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.components.resources)
}
}
val jvmMain by getting {
dependencies {
implementation(compose.desktop.currentOs)
}
}
}
}
android {
namespace = "a.b.c"
compileSdk = 33
defaultConfig {
minSdk = 21
}
sourceSets {
named("main") {
manifest.srcFile("src/androidMain/AndroidManifest.xml")
res.srcDirs("src/commonMain/resources") // <============= 这里
}
}
}
要获取字体,请在 :shared:commonMain/
内创建一个提供程序文件,如下所示:
expect val acmeTypography: Typography
现在在 :shared:androidMain
和 :shared:jvmMain
内实现它,如下所示:
// shared:jvmMain/
import androidx.compose.ui.text.platform.Font
actual val acmeTypography = Typography(
defaultFontFamily = FontFamily(
Font(resource = "font/acme_regular.ttf", FontWeight.Normal)
),
)
// shared:androidMain
import androidx.compose.ui.text.font.Font
actual val acmeTypography = Typography(
defaultFontFamily = FontFamily(
Font(R.font.acme_regular, FontWeight.Normal)
),
)
英文:
There is a easy way to use common resources directory for both android and desktop.
create src/commonMain/resources/
directory inside :shared
module.
then set the android source sets of android
and jvm
as follows:
kotlin {
targetHierarchy.default()
android {
compilations.all {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
jvm {
sourceSets {
named("jvmMain") {
resources.srcDir("src/commonMain/resources") // <============= here
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
// compose libraries
implementation(compose.runtime)
implementation(compose.foundation)
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.components.resources)
}
}
val jvmMain by getting {
dependencies {
implementation(compose.desktop.currentOs)
}
}
}
}
android {
namespace = "a.b.c"
compileSdk = 33
defaultConfig {
minSdk = 21
}
sourceSets {
named("main") {
manifest.srcFile("src/androidMain/AndroidManifest.xml")
res.srcDirs("src/commonMain/resources") // <============= here
}
}
}
To get the fonts create a provider file inside :shared:commonMain/
as:
expect val acmeTypography: Typography
Now implement it inside both :shared:androidMain
and :shared:jvmMain
as:
// shared:jvmMain/
import androidx.compose.ui.text.platform.Font
actual val acmeTypography = Typography(
defaultFontFamily = FontFamily(
Font(resource = "font/acme_regular.ttf", FontWeight.Normal)
),
)
// shared:androidMain
import androidx.compose.ui.text.font.Font
actual val acmeTypography = Typography(
defaultFontFamily = FontFamily(
Font(R.font.acme_regular, FontWeight.Normal)
),
)
答案3
得分: 1
这是在Compose多平台中链接和使用自定义Compose字体的另一种方式:
英文:
This is other way to link and use custom Compose fonts in Compose Multiplatform
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论