I am trying things out by creating a small notepad project. And now i am having a hard time finding a solution to remove the underline on text. I can set it but i cant remove it.
Heres my code:
Heres how i set it
I use span to get the selected text
selectedString.setSpan(UnderlineSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
Heres how i am trying to unset it
selectedString.setSpan(UnderlineSpan().updateDrawState(TextPaint().apply { isUnderlineText = false }), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
I tried to search for a solution but no luck can someone help me? thanks in advance!
CodePudding user response:
You can declare a class like this
class NoUnderlineSpan : UnderlineSpan() {
override fun updateDrawState(ds: TextPaint) {
ds.color = ds.linkColor
ds.isUnderlineText = false
}
}
Then use NoUnderlineSpan just like you use UnderlineSpan
Or you can try this lib I wrote :SpannableStringDslExtension
CodePudding user response:
If you want to remove all styling of the text
, you could just use toString()
on the spannable
and it would remove everything.
If you just want to remove the styling of a part of the spannable
, just make an anonymous object
of UnderlineSpan
and override
its updateDrawState
method
val underlineRemoveSpan = object : UnderlineSpan() {
override fun updateDrawState(ds: TextPaint) {
ds.isUnderlineText = false
}
}
selectedString.setSpan(underlineRemoveSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
selectedString.setSpan(UnderlineSpan().updateDrawState(TextPaint().apply { isUnderlineText = false }), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
This isn't working because, on calling updateDrawState
with a new TextPaint
after setting isUnderlineText
to false
. The implementation of updateDrawState
in UnderlineSpan
class, sets it to true
, which is why underline is not removing.