Skip to content

Commit

Permalink
fix: Use the correct font when showing inserted text (#453)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nikclayton authored Feb 16, 2024
1 parent b3978c4 commit 59a7673
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app.pachli.components.viewthread.edits

import android.content.Context
import android.graphics.Typeface.DEFAULT_BOLD
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.text.Editable
Expand All @@ -11,6 +11,7 @@ import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.style.CharacterStyle
import android.text.style.MetricAffectingSpan
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -296,17 +297,28 @@ 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

init {
bgColor = context.getColor(DR.color.view_edits_background_insert)
}

override fun updateDrawState(tp: TextPaint) {
updateMeasureState(tp)
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)
}
}

Expand Down

0 comments on commit 59a7673

Please sign in to comment.