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

Fix loading font along to its style and enable accessibility on the component #142

Merged
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
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
Loading