自定义字体在Android中以确定的方式。
您是否厌倦了自定义视图以设置字体?还是遍历ViewTree查找文本视图?是的,我也是。
包括依赖项下载(.aar):
dependencies {
compile ' uk.co.chrisjenx:calligraphy:2.3.0 '
}
将您的自定义字体添加到assets/
。所有字体定义都相对于此路径。
假设您使用的是Gradle,则应在项目目录中使用src/main/
中的资产目录创建资产目录,如果尚不存在。由于使用gradle的多项目构建非常受欢迎,因此路径通常为app/src/main/assets/
,而app
为项目名称。
您可以考虑在资产目录中创建fonts/
子目录(如示例中)。
< TextView fontPath = " fonts/MyFont.ttf " />
注意:缺少的名称空间,这是有意的。
在#onCreate()
方法中使用CalligraphyConfig
,在Application
类中定义默认字体。
@ Override
public void onCreate () {
super . onCreate ();
CalligraphyConfig . initDefault ( new CalligraphyConfig . Builder ()
. setDefaultFontPath ( "fonts/Roboto-RobotoRegular.ttf" )
. setFontAttrId ( R . attr . fontPath )
. build ()
);
//....
}
注意:您不需要定义CalligraphyConfig
,但是库将不应用默认字体,并使用R.attr.fontPath
的默认属性。
包裹Activity
上下文:
@ Override
protected void attachBaseContext ( Context newBase ) {
super . attachBaseContext ( CalligraphyContextWrapper . wrap ( newBase ));
}
你很好!
< TextView
android : text = " @string/hello_world "
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
fontPath = " fonts/Roboto-Bold.ttf " />
注意:流行的IDE(Android Studio,Intellij)可能会将其标记为错误,尽管是正确的。您可能需要在视图本身或其父视图组中添加tools:ignore="MissingPrefix"
以避免这种情况。您需要添加工具名称空间才能访问此“忽略”属性。 xmlns:tools=" http://schemas.android.com/tools"
。请参阅https://code.google.com/p/android/issues/detail?id=65176。
< style name = " TextAppearance.FontPath " parent = " android:TextAppearance " >
<!-- Custom Attr -->
< item name = " fontPath " >fonts/RobotoCondensed-Regular.ttf</ item >
</ style >
< TextView
android : text = " @string/hello_world "
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
android : textAppearance = " @style/TextAppearance.FontPath " />
< style name = " TextViewCustomFont " >
< item name = " fontPath " >fonts/RobotoCondensed-Regular.ttf</ item >
</ style >
< style name = " AppTheme " parent = " android:Theme.Holo.Light.DarkActionBar " >
< item name = " android:textViewStyle " >@style/AppTheme.Widget.TextView</ item >
</ style >
< style name = " AppTheme.Widget " />
< style name = " AppTheme.Widget.TextView " parent = " android:Widget.Holo.Light.TextView " >
< item name = " fontPath " >fonts/Roboto-ThinItalic.ttf</ item >
</ style >
CalligraphyFactory
以相当特定的顺序寻找字体,在大多数情况下,它与Android Framework如何解决属性非常相似。
View
XML-此处定义的attr将始终优先。Style
XML-接下来检查此处定义的attr。TextAppearance
XML-接下来检查ATTR,唯一的警告是,如果您在Style
中定义了字体,并且在View
中定义的TextAttribute
首先选择Style
属性!Theme
- 如果定义,则使用此功能。Default
- 如果在CalligraphyConfig
中定义,则没有找到以上所有内容,或者上述一个返回无效的字体。我们最初这样做了,但是它与想要实际使用该属性的用户相抵触,您现在必须定义自定义属性。
我们需要运送带有书法的自定义ID,以改善字体注入流。不幸的是,这意味着它必须是aar
。但是您现在正在使用Gradle吧?
可以在TextView
中使用多个字体,这不是Android的新概念。
这可以使用以下代码之类的内容来实现。
SpannableStringBuilder sBuilder = new SpannableStringBuilder ();
sBuilder . append ( "Hello!" ) // Bold this
. append ( "I use Calligraphy" ); // Default TextView font.
// Create the Typeface you want to apply to certain text
CalligraphyTypefaceSpan typefaceSpan = new CalligraphyTypefaceSpan ( TypefaceUtils . load ( getAssets (), "fonts/Roboto-Bold.ttf" ));
// Apply typeface to the Spannable 0 - 6 "Hello!" This can of course by dynamic.
sBuilder . setSpan ( typefaceSpan , 0 , 6 , Spanned . SPAN_EXCLUSIVE_EXCLUSIVE );
setText ( sBuilder , TextView . BufferType . SPANNABLE );
当然,这只是一个例子。您的里程可能会有所不同。
据我们所知(尝试: grep -r -e "void set[^(]*(Typeface " <android source dir>
)有两个标准的android小部件,它们具有多种设置字体的方法。它们是:
两者都有一种称为setSwitchTypeface
方法,该方法设置了交换机内的字体(例如开/关,是/否)。 SetTypeface
设置标签的字体。您将需要创建自己的子类,以覆盖setTypeface
并调用super.setTypeface
和super.setSwitchTypeface
。
创建此库是因为目前无法在Android中的XML文件中声明自定义字体。
如果您认为应该做到这一点,请在官方的Android Bug Tracker上饰演此问题。
Copyright 2013 Christopher Jenkins
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.