简体   繁体   中英

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

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

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
   }
}

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
}

Try

textView.getTypeface()

then check if it is equal to

  • Typeface.BOLD
  • Typeface.ITALIC
  • Typeface.BOLD_ITALIC

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