Skip to content

Commit

Permalink
Fix #66 - Add font weight from TextStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
jeziellago committed Dec 8, 2023
1 parent 5278317 commit 64322fd
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import android.content.Context
import android.graphics.Paint
import android.graphics.text.LineBreaker
import android.os.Build
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import androidx.annotation.FontRes
import androidx.annotation.IdRes
Expand All @@ -29,7 +26,6 @@ import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.doOnNextLayout
import androidx.core.widget.TextViewCompat
import coil.ImageLoader
import io.noties.markwon.AbstractMarkwonPlugin
Expand Down Expand Up @@ -167,14 +163,9 @@ private fun createTextView(
movementMethod = LinkMovementMethod.getInstance()

viewId?.let { id = viewId }
textAlign?.let { align ->
textAlignment = when (align) {
TextAlign.Left, TextAlign.Start -> View.TEXT_ALIGNMENT_TEXT_START
TextAlign.Right, TextAlign.End -> View.TEXT_ALIGNMENT_TEXT_END
TextAlign.Center -> View.TEXT_ALIGNMENT_CENTER
else -> View.TEXT_ALIGNMENT_TEXT_START
}
}
textAlign?.let { applyTextAlign(it) }
mergedStyle.fontStyle?.let { applyFontStyle(it) }
mergedStyle.fontWeight?.let { applyFontWeight(it) }

if (lineHeight != TextUnit.Unspecified) {
setLineSpacing(lineHeight.value, 1f)
Expand All @@ -188,25 +179,7 @@ private fun createTextView(
typeface = ResourcesCompat.getFont(context, font)
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && textAlign == TextAlign.Justify) {
justificationMode = LineBreaker.JUSTIFICATION_MODE_INTER_WORD
}

if (truncateOnTextOverflow) {
doOnNextLayout {
if (maxLines != -1 && lineCount > maxLines) {
val endOfLastLine = layout.getLineEnd(maxLines - 1)
val spannedDropLast3Chars = text.subSequence(0, endOfLastLine - 3) as? Spanned
if (spannedDropLast3Chars != null) {
val spannableBuilder = SpannableStringBuilder()
.append(spannedDropLast3Chars)
.append("")

text = spannableBuilder
}
}
}
}
if (truncateOnTextOverflow) enableTextOverflow()

autoSizeConfig?.let { config ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Expand Down Expand Up @@ -253,4 +226,4 @@ private fun createMarkdownRender(
}
})
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dev.jeziellago.compose.markdowntext

import android.graphics.Typeface
import android.graphics.text.LineBreaker
import android.os.Build
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.view.View
import android.widget.TextView
import androidx.compose.ui.text.font.FontStyle
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
import androidx.compose.ui.text.font.FontWeight.Companion.SemiBold
import androidx.compose.ui.text.style.TextAlign
import androidx.core.view.doOnNextLayout

fun TextView.applyFontWeight(fontWeight: FontWeight) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
typeface = Typeface.create(typeface, fontWeight.weight, false)
} else {
val weight = when(fontWeight) {
ExtraBold, Bold, SemiBold -> Typeface.BOLD
else -> Typeface.NORMAL
}
setTypeface(typeface, weight)
}
}

fun TextView.applyFontStyle(fontStyle: FontStyle) {
val type = when(fontStyle) {
FontStyle.Italic -> Typeface.ITALIC
FontStyle.Normal -> Typeface.NORMAL
else -> Typeface.NORMAL
}
setTypeface(typeface, type)
}

fun TextView.applyTextAlign(align: TextAlign) {
textAlignment = when (align) {
TextAlign.Left, TextAlign.Start -> View.TEXT_ALIGNMENT_TEXT_START
TextAlign.Right, TextAlign.End -> View.TEXT_ALIGNMENT_TEXT_END
TextAlign.Center -> View.TEXT_ALIGNMENT_CENTER
else -> View.TEXT_ALIGNMENT_TEXT_START
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && align == TextAlign.Justify) {
justificationMode = LineBreaker.JUSTIFICATION_MODE_INTER_WORD
}
}

fun TextView.enableTextOverflow() {
doOnNextLayout {
if (maxLines != -1 && lineCount > maxLines) {
val endOfLastLine = layout.getLineEnd(maxLines - 1)
val spannedDropLast3Chars = text.subSequence(0, endOfLastLine - 3) as? Spanned
if (spannedDropLast3Chars != null) {
val spannableBuilder = SpannableStringBuilder()
.append(spannedDropLast3Chars)
.append("")

text = spannableBuilder
}
}
}
}

0 comments on commit 64322fd

Please sign in to comment.