Android: How to check if TextView is bold or italic programatically

前端 未结 3 1914
南旧
南旧 2020-12-20 23:45

I would like to know how to construct an if statement to check if a TextView is bold or italic. Please help. Thank you.

相关标签:
3条回答
  • 2020-12-21 00:14

    To check if textview is BOLD and ITALIC, use following code:

     if((textView.getTypeface().getStyle() & Typeface.BOLD)!=0 &&(textView.getTypeface().getStyle() & Typeface.ITALIC)!=0){
                      //do your stuff
    

    }

    To check if textview is either BOLD or ITALIC, use following code:

     if((textView.getTypeface().getStyle() & Typeface.BOLD)!=0 ||(textView.getTypeface().getStyle() & Typeface.ITALIC)!=0){
                          //do your stuff
    }
    
    0 讨论(0)
  • 2020-12-21 00:30

    Try

    textView.getTypeface()
    

    then check if it is equal to

    • Typeface.BOLD
    • Typeface.ITALIC
    • Typeface.BOLD_ITALIC
    0 讨论(0)
  • 2020-12-21 00:33

    http://developer.android.com/reference/android/widget/TextView.html#getTypeface()

    public Typeface getTypeface ()
    

    returns:

    the current typeface and style in which the text is being displayed.

    if(yourTextViewInstance.getTypeface()!=null){
       if(yourTextViewInstance.getTypeface().getStyle()==Typeface.BOLD || yourTextViewInstance.getTypeface().getStyle()==Typeface.ITALIC){
          //do your stuff
       }
    }
    
    0 讨论(0)
提交回复
热议问题