Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add capability to handle unordered list #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Text composable to show html text from resources
&lt;font color="#FF0000"> - Colored text (Markdown does not allow a preview here)<br>
&lt;font color="red"> - Colored text (Markdown does not allow a preview here)<br><br>

**Additional**<br>
&lt;ul>&lt;li>&lt;/li>&lt;/ul> - Unordered lists
• Item
• Item

## MaterialTheme colors in HtmlText
To use colors like `MaterialTheme.colors.primary` in `HtmlText`, map simple colors.
```kotlin
Expand Down Expand Up @@ -93,6 +98,7 @@ HtmlText(textId = R.string.hello_world)
# Preview

![HtmlText](https://github.com/ch4rl3x/HtmlText/blob/main/art/screenshot.png)
![HtmlText with unordered list](https://github.com/ch4rl3x/HtmlText/blob/main/art/ul-screenshot.png)


That's it!
Expand Down
Binary file added art/ul-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class MainActivity : ComponentActivity() {
MultipleLinks()
ReturnLink()
ReturnLinks()
UnorderedList()
}
}
}
Expand Down Expand Up @@ -120,3 +121,10 @@ fun ReturnLinks() {
}
)
}

@Composable
fun UnorderedList() {
HtmlText(
text = "Unordered list<ul><li>Item</li><li>Item</li><li>Item</li></il>",
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package de.charlex.compose.material
import android.graphics.Typeface
import android.os.Build.VERSION.SDK_INT
import android.text.Html
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.BulletSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StrikethroughSpan
import android.text.style.StyleSpan
Expand Down Expand Up @@ -42,6 +44,7 @@ import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
import androidx.core.text.getSpans
import androidx.core.text.toSpannable

/**
* Simple Text composable to show the text with html styling from string resources.
Expand Down Expand Up @@ -312,7 +315,14 @@ fun Spanned.toAnnotatedString(
colorMapping: Map<Color, Color> = emptyMap(),
): AnnotatedString {
return buildAnnotatedString {
append([email protected]())
val bulletSpans = getSpans<BulletSpan>()
val spannableStringBuilder = SpannableStringBuilder.valueOf(this@toAnnotatedString)
bulletSpans.forEach { bulletSpan ->
val start = getSpanStart(bulletSpan)
spannableStringBuilder.insert(start, " • ")
spannableStringBuilder.removeSpan(bulletSpan)
}
append(spannableStringBuilder.toSpannable())
val urlSpans = getSpans<URLSpan>()
val styleSpans = getSpans<StyleSpan>()
val colorSpans = getSpans<ForegroundColorSpan>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package de.charlex.compose.material3
import android.graphics.Typeface
import android.os.Build.VERSION.SDK_INT
import android.text.Html
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.BulletSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StrikethroughSpan
import android.text.style.StyleSpan
Expand Down Expand Up @@ -42,6 +44,7 @@ import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
import androidx.core.text.getSpans
import androidx.core.text.toSpannable

/**
* Simple Text composable to show the text with html styling from string resources.
Expand Down Expand Up @@ -312,7 +315,14 @@ fun Spanned.toAnnotatedString(
colorMapping: Map<Color, Color> = emptyMap(),
): AnnotatedString {
return buildAnnotatedString {
append([email protected]())
val bulletSpans = getSpans<BulletSpan>()
val spannableStringBuilder = SpannableStringBuilder.valueOf(this@toAnnotatedString)
bulletSpans.forEach { bulletSpan ->
val start = getSpanStart(bulletSpan)
spannableStringBuilder.insert(start, " • ")
spannableStringBuilder.removeSpan(bulletSpan)
}
append(spannableStringBuilder.toSpannable())
val urlSpans = getSpans<URLSpan>()
val styleSpans = getSpans<StyleSpan>()
val colorSpans = getSpans<ForegroundColorSpan>()
Expand Down