Skip to content

Commit

Permalink
Added textColor, textSize and isUnderlineText
Browse files Browse the repository at this point in the history
  • Loading branch information
zerobranch committed May 27, 2020
1 parent 9350cbf commit 91053e8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lightspanner/src/main/java/zerobranch/lightspanner/Spans.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ object Spans {
textView: TextView,
clickListener: (() -> Unit),
touchListener: ((Boolean, TextPaint) -> Unit)? = null,
@ColorInt highlightColor: Int = Color.TRANSPARENT
@ColorInt highlightColor: Int = Color.TRANSPARENT,
isUnderlineText: Boolean = true
): ClickableSpan {
textView.movementMethod = LinkMovementMethod.getInstance()
textView.highlightColor = highlightColor
return SimpleClickableSpan(textView, clickListener, touchListener)
return SimpleClickableSpan(textView, clickListener, touchListener, isUnderlineText)
}

fun url(url: String, textView: TextView? = null): URLSpan {
Expand Down Expand Up @@ -163,6 +164,11 @@ object Spans {
dimensionType: DimensionType = DimensionType.PX
) = AbsoluteSizeSpan(dimensionType.toPx(size), false)

fun textSize(
context: Context,
@DimenRes dimenResId: Int
) = AbsoluteSizeSpan(context.resources.getDimensionPixelSize(dimenResId), false)

fun relativeSize(@FloatRange(from = 0.0) proportion: Float) = RelativeSizeSpan(proportion)

fun foregroundColor(@ColorInt colorInt: Int) = ForegroundColorSpan(colorInt)
Expand All @@ -172,6 +178,13 @@ object Spans {
@ColorRes colorResId: Int
) = ForegroundColorSpan(ContextCompat.getColor(context, colorResId))

fun textColor(@ColorInt colorInt: Int) = foregroundColor(colorInt)

fun textColor(
context: Context,
@ColorRes colorResId: Int
) = foregroundColor(context, colorResId)

fun backgroundColor(@ColorInt colorInt: Int) = BackgroundColorSpan(colorInt)

fun backgroundColor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import android.widget.TextView
class SimpleClickableSpan(
private val textView: TextView,
private val clickListener: (() -> Unit),
private val touchListener: ((Boolean, TextPaint) -> Unit)?
private val touchListener: ((Boolean, TextPaint) -> Unit)?,
private val isUnderlineText: Boolean
) : ClickableSpan() {
override fun onClick(textView: View) {
clickListener.invoke()
}

override fun updateDrawState(textPaint: TextPaint) {
super.updateDrawState(textPaint)
textPaint.isUnderlineText = isUnderlineText
touchListener?.invoke(textView.isPressed, textPaint)
textView.invalidate()
}
Expand Down

0 comments on commit 91053e8

Please sign in to comment.