I want to set spans while user is typing. I have configured interface for choosing which span to apply, but I'm having problems with spans setting.
- When I'm setting span on the whole word it works properly, but I can't remove text style by putting on it StyleSpan(NORMAL).
- For some strange reasons inserting raw text between two words written with some span makes it "spanned" too.
- Inserting long WORDS makes app work so slow that it's not possible to type
I have checked with debugger so I can say that viewModel works properly
Is it possible to solve this bugs still using spans?
class TextEditor(private val activity: AppCompatActivity) : TextWatcher {
private var _ignore = true
private var _backspace = true
private var inputtedStart = 0
private var inputtedEnd = 0
private var inputtedBefore = 0
val viewModel: TextEditorViewModel =
ViewModelProvider(activity)[TextEditorViewModel::class.java]
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
//checking if some text was deleted not inserted
_backspace = before <= count
inputtedStart = start
inputtedBefore = before
inputtedEnd = inputtedStart + count
}
override fun afterTextChanged(s: Editable) {
if (_ignore && _backspace) {
_ignore = false
if (viewModel.bold && viewModel.italic) {
s.setSpan(StyleSpan(Typeface.BOLD_ITALIC),
inputtedStart,
inputtedEnd,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
} else {
s.setSpan(StyleSpan(Typeface.NORMAL),
inputtedStart,
inputtedEnd,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
if (viewModel.bold) {
s.setSpan(StyleSpan(Typeface.BOLD),
inputtedStart,
inputtedEnd,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
if (viewModel.italic) {
s.setSpan(StyleSpan(Typeface.ITALIC),
inputtedStart,
inputtedEnd,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
_ignore = true
}
}
}