Is it possible to add `lineSpacingExtra` property ...
# compose
m
Is it possible to add
lineSpacingExtra
property when creating
TextStyle
? For example, when building style in .xml we can define something like this
Copy code
<style name="TextBody">
    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textSize">14sp</item>
    <item name="android:lineSpacingExtra">2.2sp</item>
    <item name="android:textColor">@color/primary_grey</item>
</style>
But when creating TextStyle in Compose I can't define
lineSpacingExtra
Copy code
val textBody = TextStyle(
  fontFamily = Font(familyName = DeviceFontFamilyName("sans-serif-medium")).toFontFamily(),
  fontSize = 14.sp,
  color = PrimaryGrey,
  lineSpacingExtra = 2.2.sp // does not exist
)
Is there any way to do this?
h
Unfortunately Compose does not provide a way to set
lineSpacingExtra
. It's set to its default value
1.0f
while constructing a text layout in
AndroidParagraph
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]in/androidx/compose/ui/text/AndroidParagraph.android.kt;l=521 Please file a feature request.
m
Is there any workaround to implement this behaviour?
h
The only workaround is using AndroidView wrapped TextView. Another one would be creating your own StaticLayout and drawing it in Canvas but I wouldn't recommend it.
m
Thanks for suggestions. I also thought about wrapping TextView inside AndroidView but I'm not for that option since I should provide all parameters which Text composable provides. That would be something like this:
Copy code
@Composable
fun CustomText(
  text: String,
  color: Color,
  ...
  lineSpacingExtra: TextUnit,
) {
  AndroidView(factory = {
    TextView(it).apply {
      setText(text)
      this.color = color
      this.setLineSpacing(/* add = */ lineSpacingExtra.value, /* mult = */ 0f)
    }
  })
}