Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config to disable link underline #105

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.jeziellago.compose.markdowntext

import androidx.compose.ui.graphics.Color

data class LinkStyle(
val disableUnderline: Boolean = false,
val color: Color = Color.Unspecified
) {
companion object {
val Default = LinkStyle()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ fun MarkdownText(
fun MarkdownText(
markdown: String,
modifier: Modifier = Modifier,
linkColor: Color = Color.Unspecified,
linkStyle: LinkStyle = LinkStyle.Default,
Copy link
Owner

@jeziellago jeziellago Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you take the advantage of having the new LinkStyle and use the color from there, using the linkColor as fallback, to avoid inconsistence and keeping backward compatibility?
Something like:

val currentLinkColor = if (linkStyle.color != Color.Unspecified) {
    linkStyle.color
} else {
    linkColor
}

linkColor: Color = linkStyle.color,
truncateOnTextOverflow: Boolean = false,
maxLines: Int = Int.MAX_VALUE,
isTextSelectable: Boolean = false,
Expand Down Expand Up @@ -172,6 +173,9 @@ fun MarkdownText(
onTextLayout(textView.lineCount)
}
}
if (linkStyle.disableUnderline) {
textView.applyRemoveUrlUnderlines()
}
textView.maxLines = maxLines
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.text.LineBreaker
import android.os.Build
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.style.URLSpan
import android.text.style.UnderlineSpan
import android.util.TypedValue
import android.view.Gravity
import android.widget.TextView
Expand All @@ -22,6 +26,7 @@ import androidx.compose.ui.text.font.resolveAsTypeface
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.core.content.res.ResourcesCompat
import androidx.core.text.getSpans
import androidx.core.view.doOnNextLayout
import androidx.core.widget.TextViewCompat

Expand Down Expand Up @@ -68,6 +73,22 @@ fun TextView.applyTextDecoration(textStyle: TextStyle) {
}
}

fun TextView.applyRemoveUrlUnderlines() {
val noUnderlineSpan = object : UnderlineSpan() {
override fun updateDrawState(textPaint: TextPaint) {
textPaint.isUnderlineText = false
super.updateDrawState(textPaint)
}
}
when (val text = this.text) {
is Spannable -> {
text.getSpans<URLSpan>(0, text.length).forEach { span ->
text.setSpan(noUnderlineSpan, text.getSpanStart(span), text.getSpanEnd(span), 0)
}
}
}
}

fun TextView.applyLineHeight(textStyle: TextStyle) {
if (textStyle.lineHeight.isSp) {
TextViewCompat.setLineHeight(
Expand Down
Loading