ste
05/07/2021, 1:10 PMandroidx.compose.ui.text.TextStyle
to androidx.graphics.Typeface
?
I'm currently doing:
val fontResourceId = ((textStyle.fontFamily as FontListFontFamily).fonts[0] as ResourceFont).resId // a lot of doubts about this
val typeface = ResourcesCompat.getFont(context, fontResourceId)
I need to ellipsize text at start, but this isn't possible yet, so I just draw that on a Canvas
. Code in threadste
05/07/2021, 1:11 PM@Composable
fun EllipsizeAtStartText(text: String, style: TextStyle, modifier: Modifier = Modifier) {
val context = LocalContext.current
val textSize = style.fontSize.toPx()
Spacer(modifier = modifier
.height(height = style.fontSize.toDp())
.drawWithCache {
val fontResourceId = ((style.fontFamily as FontListFontFamily).fonts[0] as ResourceFont).resId
val textPaint = Paint()
.also {
it.isAntiAlias = true
it.color = style.color
}
.asFrameworkPaint()
.also {
it.isAntiAlias = true
it.textSize = textSize
it.typeface = ResourcesCompat.getFont(context, fontResourceId)
}
val count = textPaint.breakText(text, false, size.width, null)
val ellipsizedText = '…' + text.substring(text.length - count + 1)
onDrawWithContent {
drawIntoCanvas {
it.nativeCanvas.drawText(
ellipsizedText,
0f,
size.height / 2,
textPaint
)
}
}
}
)
}