自定義字體在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.