Skip to content
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ Text composable to show html text from resources
&lt;span style="color: #0000FF"> - Colored text (Markdown does not allow a preview here)<br>
&lt;span style="color: red"> - Colored text (Markdown does not allow a preview here)<br>
&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>
&lt;font color="red"> - Colored text (Markdown does not allow a preview here)<br>
&lt;big> - <big>BIG TEXT</big> (Markdown does not allow a preview here)<br>
&lt;small> - <small> small text</small> (Markdown does not allow a preview here)<br><br>

## MaterialTheme colors in HtmlText
To use colors like `MaterialTheme.colors.primary` in `HtmlText`, map simple colors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class MainActivity : ComponentActivity() {
MultipleLinks()
ReturnLink()
ReturnLinks()
BigText()
SmallText()
}
}
}
Expand Down Expand Up @@ -120,3 +122,13 @@ fun ReturnLinks() {
}
)
}

@Composable
fun BigText() {
HtmlText(text = "<big>BIG</big> text")
}

@Composable
fun SmallText() {
HtmlText(text = "<small>small</small> text")
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Build.VERSION.SDK_INT
import android.text.Html
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StrikethroughSpan
import android.text.style.StyleSpan
import android.text.style.URLSpan
Expand Down Expand Up @@ -41,6 +42,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.em
import androidx.core.text.getSpans

/**
Expand Down Expand Up @@ -318,6 +320,7 @@ fun Spanned.toAnnotatedString(
val colorSpans = getSpans<ForegroundColorSpan>()
val underlineSpans = getSpans<UnderlineSpan>()
val strikethroughSpans = getSpans<StrikethroughSpan>()
val relativeSizeSpans = getSpans<RelativeSizeSpan>()
urlSpans.forEach { urlSpan ->
val start = getSpanStart(urlSpan)
val end = getSpanEnd(urlSpan)
Expand Down Expand Up @@ -356,5 +359,10 @@ fun Spanned.toAnnotatedString(
val end = getSpanEnd(strikethroughSpan)
addStyle(SpanStyle(textDecoration = TextDecoration.LineThrough), start, end)
}
relativeSizeSpans.forEach { relativeSizeSpan ->
val start = getSpanStart(relativeSizeSpan)
val end = getSpanEnd(relativeSizeSpan)
addStyle(style = SpanStyle(fontSize = relativeSizeSpan.sizeChange.em), start, end)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Build.VERSION.SDK_INT
import android.text.Html
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StrikethroughSpan
import android.text.style.StyleSpan
import android.text.style.URLSpan
Expand Down Expand Up @@ -41,6 +42,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.em
import androidx.core.text.getSpans

/**
Expand Down Expand Up @@ -318,6 +320,7 @@ fun Spanned.toAnnotatedString(
val colorSpans = getSpans<ForegroundColorSpan>()
val underlineSpans = getSpans<UnderlineSpan>()
val strikethroughSpans = getSpans<StrikethroughSpan>()
val relativeSizeSpans = getSpans<RelativeSizeSpan>()
urlSpans.forEach { urlSpan ->
val start = getSpanStart(urlSpan)
val end = getSpanEnd(urlSpan)
Expand Down Expand Up @@ -356,5 +359,10 @@ fun Spanned.toAnnotatedString(
val end = getSpanEnd(strikethroughSpan)
addStyle(SpanStyle(textDecoration = TextDecoration.LineThrough), start, end)
}
relativeSizeSpans.forEach { relativeSizeSpan ->
val start = getSpanStart(relativeSizeSpan)
val end = getSpanEnd(relativeSizeSpan)
addStyle(style = SpanStyle(fontSize = relativeSizeSpan.sizeChange.em), start, end)
}
}
}