Skip to content

Instantly share code, notes, and snippets.

@halilozercan
Created June 28, 2022 16:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halilozercan/96dfe0cece5dc8929fda28337b5576c4 to your computer and use it in GitHub Desktop.
Save halilozercan/96dfe0cece5dc8929fda28337b5576c4 to your computer and use it in GitHub Desktop.
/**
* - Requires two frames
* - Need to hide BasicText from view
* - Unnecessary extra composition, way too hacky
* - Can do everything that a BasicText can do
* - Constraints must be applied to BasicText
*/
@Composable
fun UsingBasicText() {
Box(Modifier.fillMaxSize()) {
var textLayoutResult by remember { mutableStateOf<TextLayoutResult?>(null) }
BasicText(
text = "Here is a text",
style = TextStyle(Color.Red),
modifier = Modifier.alpha(0f),
onTextLayout = { textLayoutResult = it }
)
Canvas(modifier = Modifier.matchParentSize()) {
translate(100f, 100f) {
textLayoutResult?.let {
drawIntoCanvas { canvas ->
TextPainter.paint(canvas, it)
}
}
}
}
}
}
/**
* - Does not support clipping out of the box.
* - FontFamilyResolver must be passed via reading Local
* - Must call resolveDefaults on TextStyle to have predictable outcome
*/
@Composable
fun UsingMultiParagraph() {
val fontFamilyResolver = LocalFontFamilyResolver.current
Box(modifier = Modifier.fillMaxSize().drawWithCache {
val multiParagraph = MultiParagraph(
annotatedString = AnnotatedString("Here is a text"),
style = resolveDefaults(TextStyle(Color.Red), layoutDirection),
constraints = Constraints(
maxWidth = size.width.toInt(),
maxHeight = size.height.toInt()
),
density = this,
fontFamilyResolver = fontFamilyResolver
)
onDrawBehind {
translate(100f, 100f) {
drawIntoCanvas { canvas ->
multiParagraph.paint(canvas)
}
}
}
})
}
/**
* - No support for multi-styling
* - Single ParagraphStyle
* - Does not support clipping out of the box.
* - FontFamilyResolver must be passed via reading Local
* - Must call resolveDefaults on TextStyle to have predictable outcome
*/
@Composable
fun UsingParagraph() {
val fontFamilyResolver = LocalFontFamilyResolver.current
Box(modifier = Modifier.fillMaxSize().drawWithCache {
val paragraph = Paragraph(
text = "Here is a text",
style = resolveDefaults(TextStyle(Color.Red), layoutDirection),
constraints = Constraints(
maxWidth = size.width.toInt(),
maxHeight = size.height.toInt()
),
density = this,
fontFamilyResolver = fontFamilyResolver
)
onDrawBehind {
translate(100f, 100f) {
drawIntoCanvas { canvas ->
paragraph.paint(canvas)
}
}
}
})
}
/**
* - Does not support async fonts
* - Platform dependent
* - Requires converting Compose concepts into platform APIs
* - Measurement only returns width
* - No support for softwrap, line breaks must be calculated manually via breakText
*/
@Composable
fun UsingNative() {
Box(modifier = Modifier.fillMaxSize().drawWithCache {
val paint = TextPaint()
paint.color = android.graphics.Color.RED
paint.textSize = 14.sp.toPx()
val text = "Here is a text"
onDrawBehind {
drawIntoCanvas { canvas ->
canvas.nativeCanvas.drawText(text, 100f, 100f, paint)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment