简体   繁体   中英

How to set part of text in textView “bold” (or) “italics” style?

I want to style few parts of texts in textView with bold (or) italics . (through strings.xml)

Create a class StringUtil

class StringUtil {

    lateinit var ss: SpannableString
    fun addForegroundColorSpan(color: Int, start: Int, end: Int): StringUtil {
        ss.setSpan(ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return ssu
    }
    fun addStyleSpan(style: Int, start: Int, end: Int): StringUtil {
        ss.setSpan(StyleSpan(style), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return ssu
    }

    fun addClickableSpan(cs: ClickableSpan, start: Int, end: Int): StringUtil {
        ss.setSpan(cs, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return ssu
    }

    fun setMovementMethod(textview: TextView): StringUtil {
        textview.setMovementMethod(LinkMovementMethod.getInstance())
        return ssu
    }

    fun getClickableSpan(url: String,context: Context): ClickableSpan {
        return object : ClickableSpan() {
            override fun onClick(widget: View) {
                val uri: Uri = Uri.parse(url)
                val intent = Intent(Intent.ACTION_VIEW, uri)
                context.startActivity(intent);
            }
        }
    }

    fun setSpannableString(textview: TextView) {
        textview.text = ss
    }

    fun getSS(text: String): StringUtil {
        ss = SpannableString(text)
        return ssu
    }

    fun setPrefixString(edt: EditText, prefixString: String) {
        edt.setText(prefixString)
        Selection.setSelection(edt.getText(), edt.getText().length);

        edt.addTextChangedListener(object : TextWatcher {

            override fun afterTextChanged(s: Editable) {
                if(!s.toString().startsWith(prefixString)){
                    edt.setText(prefixString);
                    Selection.setSelection(edt.getText(), edt.getText().length);

                }

            }

            override fun beforeTextChanged(
                s: CharSequence, start: Int,
                count: Int, after: Int
            ) {
            }

            override fun onTextChanged(
                s: CharSequence, start: Int,
                before: Int, count: Int
            ) {

            }
        })
    }

    companion object {
        private var ssu: StringUtil = StringUtil()
        fun getInstance(): StringUtil {
            return ssu
        }

    }
}

Use it like

StringUtil.getInstance().getSS("Here your text. You can set color Span , Style Span, Click Span for the specific part of your string.")
        .addStyleSpan(Typeface.BOLD,34,39)
        .addClickableSpan(StringUtil.getInstance().getClickableSpan("http://www.google.com",this), 34, 39)
        .addForegroundColorSpan(Color.YELLOW, 34, 39)
        .addStyleSpan(Typeface.BOLD,43,text.length-1)
        .addClickableSpan(StringUtil.getInstance().getClickableSpan("http://www.facebook.com",this), 43, text.length-1)
        .addForegroundColorSpan(Color.YELLOW, 43, text.length-1 )
        .setMovementMethod(binding.tvWarringPrivacyPolicy)
        .setSpannableString(binding.tvWarringPrivacyPolicy)

Android textView supports lightweight HTML markup: <b>, <u>, <i>

<resources>
    <string name="b">This has <b>bold</b> in it.</string>
    <string name="i">Whereas this has <i>italics</i>!</string>
</resources>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM