fix: Use the correct font when showing inserted text (#453)

The previous code was using a bold version of the default font for
inserted text. So if the user had set a custom font it was being
ignored.

Fix this by creating a bold version of the user's typeface.

Fixes #435
This commit is contained in:
Nik Clayton 2024-02-16 21:52:05 +01:00 committed by GitHub
parent b3978c4af7
commit 59a7673149
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 4 deletions

View File

@ -1,7 +1,7 @@
package app.pachli.components.viewthread.edits package app.pachli.components.viewthread.edits
import android.content.Context import android.content.Context
import android.graphics.Typeface.DEFAULT_BOLD import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.text.Editable import android.text.Editable
@ -11,6 +11,7 @@ import android.text.SpannableStringBuilder
import android.text.Spanned import android.text.Spanned
import android.text.TextPaint import android.text.TextPaint
import android.text.style.CharacterStyle import android.text.style.CharacterStyle
import android.text.style.MetricAffectingSpan
import android.util.TypedValue import android.util.TypedValue
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
@ -296,8 +297,13 @@ class PachliTagHandler(val context: Context) : Html.TagHandler {
} }
} }
/** Span that signifies inserted text */ /**
class InsertedTextSpan(context: Context) : CharacterStyle() { * Span that signifies inserted text.
*
* Derives from [MetricAffectingSpan] as making the font bold can change
* its metrics.
*/
class InsertedTextSpan(context: Context) : MetricAffectingSpan() {
private var bgColor: Int private var bgColor: Int
init { init {
@ -305,8 +311,14 @@ class PachliTagHandler(val context: Context) : Html.TagHandler {
} }
override fun updateDrawState(tp: TextPaint) { override fun updateDrawState(tp: TextPaint) {
updateMeasureState(tp)
tp.bgColor = bgColor tp.bgColor = bgColor
tp.typeface = DEFAULT_BOLD }
override fun updateMeasureState(tp: TextPaint) {
// Try and create a bold version of the active font to preserve
// the user's custom font selection.
tp.typeface = Typeface.create(tp.typeface, Typeface.BOLD)
} }
} }