getTypeFace from a character

前端 未结 1 700
野性不改
野性不改 2021-01-26 10:47

Formatted text appears in a edittext.But the string contains multiple styling .

Is it possible to get the styling used on a particular character in a string , using getT

相关标签:
1条回答
  • 2021-01-26 10:56

    The first question is how to you set that styling?

    1. If your doing something like Html.fromHtml("hello") calling getTypeFace() will return 0 (TypeFace.NORMAL). For this I would say that you might need to make the parse yourself and create substrings according to the HTML tags you've found.

    2. If you your using the TextView attributes for this - android:textStyle="bold"

    You can call directly:

    Log.d(TAG, "has typeface=${tv_typeface.typeface.style}")
    
    1. Alternatively if you're using SpannableStrings to edit how your text should look

    You can do something like:

    //Set an italic style to the word "hello"
    val spannableString = SpannableString("hello world")
    spannableString.setSpan(StyleSpan(Typeface.ITALIC), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    tv_typeface.text = spannableString
    
    //Get the style italic used
    val spannedString = tv_typeface.text as SpannedString
    val spans = spannedString.getSpans(0, tv_typeface.length(), StyleSpan::class.java)
    
    for (span in spans) {
      Log.d(TAG, "StyleSpan between: ${spannedString.getSpanStart(span)} and ${spannedString.getSpanEnd(span)} with style ${span.style}")
    }
    
    0 讨论(0)
提交回复
热议问题