Hi guys. I am searching for a way to write a custo...
# compose
b
Hi guys. I am searching for a way to write a custom modifier, which scales the font size if the composable has a specific size (width)
Copy code
Text("This is a test", modifier = Modifier.dynamicTextSize(14.sp))
• composable width <=100dp it should use the specified 14.sp • composable width > 100dp it should use the specified 14.sp multiplied with a scale factor -> e.g. scalefactor = 1.5 * 14.sp = 21.sp Can you please show me how to achieve this best? Thx
l
You can use BoxWithConstraint() on top of your Text composable, and using
maxWidth
you can edit your text size
b
Thx for this info. I know i can, but i think it will be much nicer using a modifier than always have to set a BoxWithConstraint around each Text Composable
a
You can't create a modifier for this because you can't set font size using a modifier.
☝️ 1
b
Ok this is a good reason 🙂 So it would be best if i create a custom composable like this?
Copy code
@Composable
fun DynamicText(text: String, fontSize: Sp) {
  BoxWithConstraint {
    val textSize = if (maxWidth > 100) fontSize * 1.5 else fontSize
    TextSize(text = text, fontSize = textSize)
  }
}
Or can i do better?