FontFamily 看这里就够了

TextView中字体设置

textView中有三个属性可以设置字体

  • textStyle:设置样式
  • fontFamily:设置使用的字体
  • typeface:设置使用的字体文件。

textStyle 如下样式

  • NORMAL:默认字体;
  • BOLD:粗题;
  • ITALIC:斜体;
  • BOLD_ITALIC:粗斜体;

typeface

java系统默认支持如下字体:

  • noraml (普通字体,系统默认使用的字体)
  • sans(非衬线字体)
  • serif (衬线字体)
  • monospace(等宽字体)

简单介绍三种字体的区别:在西方国家罗马字母阵营中,字体分为两大种类:Sans Serif和Serif,打字机体虽然也属于Sans Serif,但由于是等宽字体,所以另外独立出Monospace这一种类。

Serif的意思是,在字的笔划开始及结束的地方有额外的装饰,而且笔划的粗细会因直横的不同而有不同。相反的,Sans Serif则没有这些额外的装饰,笔划粗细大致差不多。如下图:

Serif和Sans-serif字体的区别

有两种方式设置字体。

一、配置设置

<TextView
    android:text="Hello,World"
    android:textSize="20sp"
    <!--  使用默认的sans字体-->
    android:typeface="sans" 
    <!--  使用默认的serifs字体-->   
    android:typeface="serif"
    <!--  使用默认的monospace字体-->
      android:typeface="monospace"
   />

代码方式设置

//设置serif字体
textView.setTypeface(Typeface.SERIF);
//设置sans字体
textView.setTypeface(Typeface.SANS_SERIF);
//设置monospace字体
textView.setTypeface(Typeface.MONOSPACE);

fontFamily

fontFamily 值为系统支持的字体名称。
根据MATERIAL DESIGN 文档中的排版一章描述,anroid可支持下图的字体样式:

新版本支持新的字体,详细见下表。

这些字体文件定义在系统文件的data/fonts/fonts.xml文件夹下android 5.0 +以上。详细信息可连接的文件。

    <!-- first font is default -->
    <family name="sans-serif">
        <font weight="100" style="normal">Roboto-Thin.ttf</font>
        <font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
        <font weight="300" style="normal">Roboto-Light.ttf</font>
        <font weight="300" style="italic">Roboto-LightItalic.ttf</font>
        <font weight="400" style="normal">Roboto-Regular.ttf</font>
        <font weight="400" style="italic">Roboto-Italic.ttf</font>
        <font weight="500" style="normal">Roboto-Medium.ttf</font>
        <font weight="500" style="italic">Roboto-MediumItalic.ttf</font>
        <font weight="900" style="normal">Roboto-Black.ttf</font>
        <font weight="900" style="italic">Roboto-BlackItalic.ttf</font>
        <font weight="700" style="normal">Roboto-Bold.ttf</font>
        <font weight="700" style="italic">Roboto-BoldItalic.ttf</font>
    </family>

android 4.4- 文件是不同的,感觉更易于理解。android 4.1 介绍中的Font families章节有较为详细的介绍

Font families
Android 4.1 adds several more variants of the Roboto font style for a total of 10 variants, and they're all usable by apps. Your apps now have access to the full set of both light and condensed variants.

The complete set of Roboto font variants available is:

Regular
Italic
Bold
Bold-italic
Light
Light-italic
Condensed regular
Condensed italic
Condensed bold
Condensed bold-italic
You can apply any one of these with the new fontFamily attribute in combination with the textStyle attribute.

Supported values for fontFamily are:

"sans-serif" for regular Roboto
"sans-serif-light" for Roboto Light
"sans-serif-condensed" for Roboto Condensed
You can then apply bold and/or italic with textStyle values "bold" and "italic". You can apply both like so: android:textStyle="bold|italic".

You can also use Typeface.create(). For example, Typeface.create("sans-serif-light", Typeface.NORMAL).

font.xml文件定义了很多字体,若字体不支持怎么办?android 5.0版本的font.xml注释中有如下一段话可以很好的说明系统的处理流程:

The first family is also the default font, which handles font request that have not specified
specific font names.

font.xml文件及其变更记录有较为完备的更新记录。

这里有一张表很好的说明了问题(来源):

Font android:fontFamily android:textStyle
Roboto Thin sans-serif-thin
Roboto Light sans-serif-light
Roboto Regular sans-serif
Roboto Bold sans-serif bold
Roboto Medium sans-serif-medium
Roboto Black sans-serif-black
Roboto Condensed Light sans-serif-condensed-light
Roboto Condensed Regular sans-serif-condensed
Roboto Condensed Medium sans-serif-condensed-medium
Roboto Condensed Bold sans-serif-condensed bold
Noto Serif serif
Noto Serif Bold serif bold
Droid Sans Mono monospace
Cutive Mono serif-monospace
Coming Soon casual
Dancing Script cursive
Dancing Script Bold cursive bold
Carrois Gothic SC sans-serif-smallcaps

总结: fontFamliy 表示android系统支持的一些列字体。每个字体都一个别名,以唯一标识这个字体。由于android各个版本的不同,会导致高版本支持的某些字体设置在低版本不生效,则使用低版本的默认字体。

文字如何实现加粗、斜体?

我们知道TextView画文字,最终都是通过Paint相关设置来完成的。

android中常用的需求是设置TextView的FontFamily属性。有两种方式设置:

  • 方式一 通过配置文件完成
 android:fontFamily="serif" 
  • 方式二:通过代码设置。
TextView.setTypeface(TypeFace typeface)。
public void setTypeface(Typeface tf) {
    //...其他代码
    mTextPaint.setTypeface(tf);
    //...更新界面代码
}

可以看下TexTView的setTypeface方法具体实现。

TextView.setTypeface(TypeFace typeface, int style)。

public void setTypeface(Typeface tf, int style) {
   if (style > 0) {
       if (tf == null) {
           tf = Typeface.defaultFromStyle(style);
       } else {
           tf = Typeface.create(tf, style);
       }

       setTypeface(tf);
       // now compute what (if any) algorithmic styling is needed
       int typefaceStyle = tf != null ? tf.getStyle() : 0;
       int need = style & ~typefaceStyle;
       mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);
       mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);
   } else {
       mTextPaint.setFakeBoldText(false);
       mTextPaint.setTextSkewX(0);
       setTypeface(tf);
   }
}
  • Paint.setFakeBoldText方法实现字体的加粗
  • Paint.setTextSkewX 实现字体斜体

三者之间的关系

TextView中有个私有方法:setTypefaceFromAttrs

    private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) {
        Typeface tf = null;
        if (familyName != null) {
            tf = Typeface.create(familyName, styleIndex);
            if (tf != null) {
                setTypeface(tf);
                return;
            }
        }
        switch (typefaceIndex) {
            case SANS:
                tf = Typeface.SANS_SERIF;
                break;

            case SERIF:
                tf = Typeface.SERIF;
                break;

            case MONOSPACE:
                tf = Typeface.MONOSPACE;
                break;
        }

        setTypeface(tf, styleIndex);
    }

结论:

  • fontFamily、typeface 都用于设置字体,d都设置是优先使用fontFamily。
  • textStyle 用于改变字体的样式

自定义字体

android O 开始支持自定义字体
https://developer.android.com/guide/topics/resources/font-resource.html

参考:

猜你喜欢

转载自blog.csdn.net/yuanxw44/article/details/80019501