Skip to content

Commit

Permalink
Merge pull request #142 from pucamafra/bugfix/main/bold-style-not-bei…
Browse files Browse the repository at this point in the history
…ng-applied-properly

Fix loading font along to its style and enable accessibility on the component
  • Loading branch information
jeziellago authored Jan 20, 2025
2 parents 0b56eb4 + ac15afd commit 70ec23d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Build
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.view.View
import android.widget.TextView
import androidx.annotation.FontRes
import androidx.annotation.IdRes
Expand Down Expand Up @@ -47,6 +48,7 @@ fun MarkdownText(
syntaxHighlightTextColor: Color = Color.Unspecified,
headingBreakColor: Color = Color.Transparent,
enableUnderlineForLink: Boolean = true,
importForAccessibility: Int = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO,
beforeSetMarkdown: ((TextView, Spanned) -> Unit)? = null,
afterSetMarkdown: ((TextView) -> Unit)? = null,
onLinkClicked: ((String) -> Unit)? = null,
Expand Down Expand Up @@ -88,6 +90,7 @@ fun MarkdownText(
CustomTextView(factoryContext).apply {
viewId?.let { id = viewId }
fontResource?.let { font -> applyFontResource(font) }
importantForAccessibility = importForAccessibility

setMaxLines(maxLines)
setLinkTextColor(linkTextColor.toArgb())
Expand Down Expand Up @@ -120,7 +123,7 @@ fun MarkdownText(

with(style) {
applyTextAlign(textAlign)
fontFamily?.let { applyFontFamily(it) }
fontFamily?.let { applyFontFamily(this) }
fontStyle?.let { applyFontStyle(it) }
fontWeight?.let { applyFontWeight(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import android.view.Gravity
import android.widget.TextView
import androidx.annotation.FontRes
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontSynthesis
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.font.FontWeight.Companion.Bold
import androidx.compose.ui.text.font.FontWeight.Companion.ExtraBold
Expand Down Expand Up @@ -46,8 +46,13 @@ fun TextView.applyFontStyle(fontStyle: FontStyle) {
setTypeface(typeface, type)
}

fun TextView.applyFontFamily(fontFamily: FontFamily) {
typeface = createFontFamilyResolver(context).resolveAsTypeface(fontFamily).value
fun TextView.applyFontFamily(textStyle: TextStyle) {
typeface = createFontFamilyResolver(context).resolveAsTypeface(
fontFamily = textStyle.fontFamily,
fontWeight = textStyle.fontWeight ?: FontWeight.Normal,
fontStyle = textStyle.fontStyle ?: FontStyle.Normal,
fontSynthesis = textStyle.fontSynthesis ?: FontSynthesis.All,
).value
}

fun TextView.applyFontResource(@FontRes font: Int) {
Expand Down

0 comments on commit 70ec23d

Please sign in to comment.