hanggrian / socialview

Android TextView and EditText with hashtag, mention, and hyperlink support
https://hendraanggrian.github.io/socialview/
Apache License 2.0
322 stars 81 forks source link

Differents Spans Doesn't Apply #109

Closed samuel2629 closed 2 years ago

samuel2629 commented 2 years ago

What I want to do is apply a span to a text that is going to show up inside a SocialTextView. For example:

Username_here Hello @Someone and good morning to @someone_else.

I want to make the "Username_here" part of the string be bold (which I can do via a Spannable) and the rest of the text (Hello @Someone and good morning to @someone_else) to behave like it should since it is a SocialTextView (meaning being able to handle clicks on mentions and links).

But the bold part is not showing bold is showing regular.

Any ideas what we can do to solve this issue?

hanggrian commented 2 years ago

As I was saying in #78, put this code in TextWatcher.

Matcher matcher = textView.getMentionPattern().matcher(spannable);
while (matcher.find()) {
    int start = matcher.start();
    int end = matcher.end();
    StyleSpan span = new StyleSpan(android.graphics.Typeface.BOLD);
    spannable.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
samuel2629 commented 2 years ago

Thank you for answering me ! I just did it by my own means though ! You can close this issue.

guizot commented 2 years ago

Bro, ini saya sudah pakai seperti ini tapi kok tetap tidak BOLD ya? kenapa ya bro?

textView.addTextChangedListener(object : TextWatcher {
    override fun afterTextChanged(value: Editable?) {
        val spannable = SpannableString(value)
        val matcher: Matcher = textView.mentionPattern.matcher(spannable)
        while (matcher.find()) {
            val start: Int = matcher.start()
            val end: Int = matcher.end()
            spannable.setSpan(StyleSpan(Typeface.BOLD), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
    }
    override fun beforeTextChanged(value: CharSequence?, p1: Int, p2: Int, p3: Int) {}
    override fun onTextChanged(value: CharSequence?, p1: Int, p2: Int, p3: Int) {}
})