-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #66 - Add font weight from TextStyle
- Loading branch information
1 parent
5278317
commit 64322fd
Showing
2 changed files
with
71 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
markdowntext/src/main/java/dev/jeziellago/compose/markdowntext/TextAppearanceExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |