You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description
I'm migrating from MVVM to MVI Compose, and in one of the screens I have a string where I have two texts in bold. Well, my final code is working, however when I generate my printscreen, my screen generates after the first text that should be in bold.
Steps to Reproduce
In my string <string name="lorem">Lorem ipsum dolor sit amet, <b>consectetur adipiscing elit</b>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. <b>Ut enim ad minim veniam</b>, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</string>
StringResources.kt file @Composable @ReadOnlyComposable private fun getCurrentResources(): Resources = LocalContext.current.resources
fun Spanned.toHtmlWithoutParagraphs(): String { return HtmlCompat.toHtml(this, HtmlCompat.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE) .substringAfter("<p dir=\"ltr\">").substringBeforeLast("</p>") }
fun Resources.getText(@StringRes id: Int, vararg args: Any): CharSequence { val escapedArgs = args.map { if (it is Spanned) it.toHtmlWithoutParagraphs() else it }.toTypedArray() val resource = SpannedString(getText(id)) val htmlResource = resource.toHtmlWithoutParagraphs() val formattedHtml = String.format(htmlResource, *escapedArgs) return HtmlCompat.fromHtml(formattedHtml, HtmlCompat.FROM_HTML_MODE_LEGACY) }
@Composable fun annotatedStringResource(@StringRes id: Int, vararg formatArgs: Any): AnnotatedString { val resources = getCurrentResources() val density = LocalDensity.current return remember(id, formatArgs) { val text = resources.getText(id, *formatArgs) spannableStringToAnnotatedString(text, density) } }
@Composable fun annotatedStringResource(@StringRes id: Int): AnnotatedString { val resources = getCurrentResources() val density = LocalDensity.current return remember(id) { val text = resources.getText(id) spannableStringToAnnotatedString(text, density) } }
private fun spannableStringToAnnotatedString( text: CharSequence, density: Density, ): AnnotatedString { return if (text is Spanned) { with(density) { buildAnnotatedString { append("$text") text.getSpans(0, text.length, Any::class.java).forEach { val start = text.getSpanStart(it) val end = text.getSpanEnd(it) when (it) { is StyleSpan -> when (it.style) { Typeface.NORMAL -> addStyle( SpanStyle( fontWeight = FontWeight.Normal, fontStyle = FontStyle.Normal, ), start, end, )
Expected behavior
When I run my app it works fine with bold texts. In my Screen.kt design class it only shows me the text without the bold, but when I run ./gradlew clean lintReportDebug it only shows my text Lorem ipsum dolor sit amet, consectetur adipiscing elit without the bold defined in my string.
Additional information:
Paparazzi Version: 1.2.0
OS: Android API 33
Compile SDK: corretto version 11.0.19
Gradle Version: 7.3.0
Android Gradle Plugin Version:
Screenshots
The text was updated successfully, but these errors were encountered:
@danielvilha For the truncated text in snapshot from Paparazzi, it is fixed on master when enabling the new resource loading mechanism, tracked in #524.
For the text not bold in both preview and Paparazzi, an issue has been filed to the layoutlib team and it is fixed in Android Studio Hedgehog
Description
I'm migrating from MVVM to MVI Compose, and in one of the screens I have a string where I have two texts in bold. Well, my final code is working, however when I generate my printscreen, my screen generates after the first text that should be in bold.
Steps to Reproduce
In my string
<string name="lorem">Lorem ipsum dolor sit amet, <b>consectetur adipiscing elit</b>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. <b>Ut enim ad minim veniam</b>, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</string>
StringResources.kt file
@Composable
@ReadOnlyComposable
private fun getCurrentResources(): Resources = LocalContext.current.resources
fun Spanned.toHtmlWithoutParagraphs(): String {
return HtmlCompat.toHtml(this, HtmlCompat.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE)
.substringAfter("<p dir=\"ltr\">").substringBeforeLast("</p>")
}
fun Resources.getText(@StringRes id: Int, vararg args: Any): CharSequence {
val escapedArgs = args.map {
if (it is Spanned) it.toHtmlWithoutParagraphs() else it
}.toTypedArray()
val resource = SpannedString(getText(id))
val htmlResource = resource.toHtmlWithoutParagraphs()
val formattedHtml = String.format(htmlResource, *escapedArgs)
return HtmlCompat.fromHtml(formattedHtml, HtmlCompat.FROM_HTML_MODE_LEGACY)
}
@Composable
fun annotatedStringResource(@StringRes id: Int, vararg formatArgs: Any): AnnotatedString {
val resources = getCurrentResources()
val density = LocalDensity.current
return remember(id, formatArgs) {
val text = resources.getText(id, *formatArgs)
spannableStringToAnnotatedString(text, density)
}
}
@Composable
fun annotatedStringResource(@StringRes id: Int): AnnotatedString {
val resources = getCurrentResources()
val density = LocalDensity.current
return remember(id) {
val text = resources.getText(id)
spannableStringToAnnotatedString(text, density)
}
}
private fun spannableStringToAnnotatedString(
text: CharSequence,
density: Density,
): AnnotatedString {
return if (text is Spanned) {
with(density) {
buildAnnotatedString {
append("$text")
text.getSpans(0, text.length, Any::class.java).forEach {
val start = text.getSpanStart(it)
val end = text.getSpanEnd(it)
when (it) {
is StyleSpan -> when (it.style) {
Typeface.NORMAL -> addStyle(
SpanStyle(
fontWeight = FontWeight.Normal,
fontStyle = FontStyle.Normal,
),
start,
end,
)
Typeface.BOLD -> addStyle(
SpanStyle(
fontWeight = FontWeight.Bold,
fontStyle = FontStyle.Normal,
),
start,
end,
)
Typeface.ITALIC -> addStyle(
SpanStyle(
fontWeight = FontWeight.Normal,
fontStyle = FontStyle.Italic,
),
start,
end,
)
Typeface.BOLD_ITALIC -> addStyle(
SpanStyle(
fontWeight = FontWeight.Bold,
fontStyle = FontStyle.Italic,
),
start,
end,
)
}
is TypefaceSpan -> addStyle(
SpanStyle(
fontFamily = when (it.family) {
FontFamily.SansSerif.name -> FontFamily.SansSerif
FontFamily.Serif.name -> FontFamily.Serif
FontFamily.Monospace.name -> FontFamily.Monospace
FontFamily.Cursive.name -> FontFamily.Cursive
else -> FontFamily.Default
},
),
start,
end,
)
is BulletSpan -> {
addStyle(SpanStyle(), start, end)
}
is AbsoluteSizeSpan -> addStyle(
SpanStyle(fontSize = if (it.dip) it.size.dp.toSp() else it.size.toSp()),
start,
end,
)
is RelativeSizeSpan -> addStyle(
SpanStyle(fontSize = it.sizeChange.em),
start,
end,
)
is StrikethroughSpan -> addStyle(
SpanStyle(textDecoration = TextDecoration.LineThrough),
start,
end,
)
is UnderlineSpan -> addStyle(
SpanStyle(textDecoration = TextDecoration.Underline),
start,
end,
)
is SuperscriptSpan -> addStyle(
SpanStyle(baselineShift = BaselineShift.Superscript),
start,
end,
)
is SubscriptSpan -> addStyle(
SpanStyle(baselineShift = BaselineShift.Subscript),
start,
end,
)
is ForegroundColorSpan -> addStyle(
SpanStyle(color = Color(it.foregroundColor)),
start,
end,
)
else -> addStyle(SpanStyle(), start, end)
}
}
}
}
} else {
AnnotatedString("$text")
}
}
@LightDarkPreview
@Composable
fun VtuScanHelpPreview() {
VtuScanHelpScreen()
}
/**
* Compose Screen UI.
*/
@Composable
fun VtuScanHelpScreen() {
VzcTheme {
Content()
}
}
My Screen.kt file
@Composable
private fun Content() {
Surface {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
) {
Text(
annotatedStringResource(
R.string.lorem,
),
)
}
}
}
Expected behavior
When I run my app it works fine with bold texts. In my Screen.kt design class it only shows me the text without the bold, but when I run
./gradlew clean lintReportDebug
it only shows my textLorem ipsum dolor sit amet, consectetur adipiscing elit
without the bold defined in my string.Additional information:
Screenshots
![Screenshot 2023-05-02 at 15 35 46](https://user-images.githubusercontent.com/1951354/235710121-767fa48c-63b0-45b9-86b0-3380e8117355.png)
![Screenshot 2023-05-02 at 15 35 07](https://user-images.githubusercontent.com/1951354/235710180-5298f76d-20f2-4eac-b329-02c2b9f2ada3.png)
![Screenshot 2023-05-02 at 15 44 32](https://user-images.githubusercontent.com/1951354/235711222-36982547-25ec-430f-9973-941b612e96f9.png)
The text was updated successfully, but these errors were encountered: