I highlight the text, a context menu appears and I select italics. The text becomes italicized. Then I select the text again, let’s say half of the text and click on the italics again in the context menu, I expect that half of the text will stop being italics, but instead the whole text becomes normal.
The code that doesn’t work correctly, but I tried
R.id.context_menu_cursive -> {
val start = editText.selectionStart
val end = editText.selectionEnd
val spannable = editText.text as Spannable
val spans = spannable.getSpans(start, end, StyleSpan::class.java)
if (spans.any { it.style == Typeface.ITALIC }) {
val italicSpans = spannable.getSpans(start, end, StyleSpan::class.java)
italicSpans.forEach {
if(it.style == Typeface.ITALIC) spannable.removeSpan(it)
}
spannable.setSpan(StyleSpan(Typeface.ITALIC), end, spannable.length, 0)
} else {
spannable.setSpan(StyleSpan(Typeface.ITALIC), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
}
mode.finish()
true
}