Skip to content

Commit b574401

Browse files
committed
fix: allow custom colors for links on Android
- Modify UrlSpanNoUnderline to not apply default link color - Apply color span after URLSpan to ensure custom colors take precedence - Support both custom link colors and default blue when no color specified
1 parent 012592d commit b574401

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

android/src/main/java/com/nitrotext/NitroTextImpl.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,14 @@ class NitroTextImpl(private val view: AppCompatTextView) {
195195
val end = builder.length
196196
if (start == end) continue
197197

198-
frag.fontColor?.let { parseColorSafe(it)?.let { c ->
199-
builder.setSpan(ForegroundColorSpan(c), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
200-
}}
198+
// Apply font color for non-link fragments, or store for links to apply after URLSpan
199+
val hasLink = frag.linkUrl?.isNotEmpty() == true
200+
if (!hasLink) {
201+
frag.fontColor?.let { parseColorSafe(it)?.let { c ->
202+
builder.setSpan(ForegroundColorSpan(c), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
203+
}}
204+
}
205+
201206
frag.fragmentBackgroundColor?.let { parseColorSafe(it)?.let { c ->
202207
builder.setSpan(BackgroundColorSpan(c), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
203208
}}
@@ -224,11 +229,16 @@ class NitroTextImpl(private val view: AppCompatTextView) {
224229
if (url.isNotEmpty()) {
225230
builder.setSpan(UrlSpanNoUnderline(url), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
226231
hasLinks = true
227-
// Apply default link color if fragment doesn't have explicit color
228-
if (frag.fontColor == null) {
229-
// Use system link color (typically blue)
230-
val linkColor = android.graphics.Color.parseColor("#007AFF")
231-
builder.setSpan(ForegroundColorSpan(linkColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
232+
// Apply link color - use custom color if provided, otherwise use default
233+
// This must be applied AFTER the URLSpan to ensure it takes precedence
234+
val linkColor = if (frag.fontColor != null) {
235+
parseColorSafe(frag.fontColor)
236+
} else {
237+
// Use system link color (typically blue) as default
238+
android.graphics.Color.parseColor("#007AFF")
239+
}
240+
linkColor?.let { c ->
241+
builder.setSpan(ForegroundColorSpan(c), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
232242
}
233243
}
234244
}

android/src/main/java/com/nitrotext/spans/UrlSpanNoUnderline.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import android.text.style.URLSpan
55

66
internal class UrlSpanNoUnderline(url: String) : URLSpan(url) {
77
override fun updateDrawState(ds: TextPaint) {
8-
super.updateDrawState(ds)
8+
// Don't call super.updateDrawState() to avoid default link color
9+
// This allows custom colors from ForegroundColorSpan to be applied
910
ds.isUnderlineText = false
1011
}
1112
}

0 commit comments

Comments
 (0)