android 字体编程,Android编程之Calligraphy:Android 自定义字体库

Calligraphy是android 自定义字体库

添加依赖

Download from Maven Central (.jar)

OR

Java

dependencies {

compile 'uk.co.chrisjenx:calligraphy:1.2.0'

}

123

dependencies {    compile 'uk.co.chrisjenx:calligraphy:1.2.0'}

使用

字体

把你的所有的自定义字体都添加到assets/fonts 目录下, 所有的字体引用的都是这个目录

自定义属性

我们没有在Jar包中打包  R.attr,所以你需要自己添加你自己的Attr,下面是一个例子  res/values/attrs.xml

Java

1234

配置

在Application类中,使用 CalligraphyConfig定义你默认的字体, 不幸的是Activity#onCreate(Bundle) 在 Activity#attachBaseContext(Context)之后调用, 所以这个配置文件要在之前定义

Java

protected void onCreate() {

super.onCreate();

CalligraphyConfig.initDefault("fonts/Roboto-Regular.ttf", R.attr.fontPath);

//....

}

12345

protected void onCreate() {    super.onCreate();    CalligraphyConfig.initDefault("fonts/Roboto-Regular.ttf", R.attr.fontPath);    //....}

注入到 Context

Java

@Override

protected void attachBaseContext(Context newBase) {

super.attachBaseContext(new CalligraphyContextWrapper(newBase));

}

1234

@Overrideprotected void attachBaseContext(Context newBase) {    super.attachBaseContext(new CalligraphyContextWrapper(newBase));}

给TextView自定义字体

Java

android:text="@string/hello_world"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

fontPath="fonts/Roboto-Bold.ttf"/>

12345

在TextAppearance中自定义字体

Java

fonts/RobotoCondensed-Regular.ttf

android:text="@string/hello_world"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="@style/TextAppearance.FontPath"/>

123456789

在styles中自定义字体

Java

fonts/RobotoCondensed-Regular.ttf

123

Custom font defined in Theme

Java

@style/AppTheme.Widget.TextView

fonts/Roboto-ThinItalic.ttf

  

开源地址:https://github.com/chrisjenx/Calligraphy

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值