خطوط مخصصة في Android بطريقة طيب.
هل سئمت من المشاهدات المخصصة لضبط الخطوط؟ أو اجتياز ViewTree للعثور على TextViews؟ نعم أنا أيضا.
قم بتضمين تنزيل التبعية (.Aar):
dependencies {
compile ' uk.co.chrisjenx:calligraphy:2.3.0 '
}
أضف خطوطك المخصصة إلى assets/
. جميع تعريفات الخطوط تتعلق بهذا المسار.
على افتراض أنك تستخدم Gradle ، يجب عليك إنشاء دليل الأصول بموجب src/main/
في دليل المشروع الخاص بك إذا لم يكن موجودًا بالفعل. نظرًا لأنه من الشائع استخدام Build Multi-Project مع Gradle ، عادةً ما يكون المسار app/src/main/assets/
، حيث يكون app
هو اسم المشروع.
قد تفكر في إنشاء fonts/
دليل فرعي في دليل الأصول (كما في الأمثلة).
< TextView fontPath = " fonts/MyFont.ttf " />
ملاحظة: مساحة الاسم المفقودة ، هذا مقصود.
حدد خطك الافتراضي باستخدام CalligraphyConfig
، في فئة Application
الخاصة بك بطريقة #onCreate()
.
@ 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's (Android Studio ، Intellij) هذا كخطأ على الرغم من صحة. قد ترغب في إضافة tools:ignore="MissingPrefix"
إما إلى العرض نفسه أو مجموعة ViewGroup الأصل لتجنب ذلك. ستحتاج إلى إضافة مساحة اسم الأدوات للوصول إلى سمة "تجاهل". 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.
View
XML - ATTRENT هنا سيأخذ دائمًا الأولوية.Style
XML - تم تحديد ATTRENT هنا بعد ذلك.TextAppearance
XML - يتم التحقق من ATTR بعد ذلك ، التحذير الوحيد لذلك هو إذا كان لديك خط محدد في Style
و TextAttribute
المحددة في View
يتم اختيار سمة Style
أولاً!Theme
- إذا تم تعريفه يتم استخدام هذا.Default
- إذا تم تحديده في CalligraphyConfig
فسيتم العثور على أي مما سبق أو إذا قام أحد ما أعلاه بإرجاع خط غير صالح.لقد فعلنا في الأصل ، ولكنه يتعارض مع المستخدمين الذين يرغبون في استخدام هذه السمة بالفعل ، عليك الآن تحديد سمة مخصصة.
كنا بحاجة إلى شحن معرف مخصص مع الخط لتحسين تدفق حقن الخط. هذا يعني للأسف أنه يجب أن يكون 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
التي تحدد المحرف داخل المفتاح (على سبيل المثال ON/OFF ، نعم/لا). SetTypeface
يعين محرف التسمية. ستحتاج إلى إنشاء الفئة الفرعية الخاصة بك التي تتجاوز setTypeface
وتستدعي super.setTypeface
و super.setSwitchTypeface
.
تم إنشاء هذه المكتبة لأنه من غير الممكن حاليًا إعلان خط مخصص في ملفات XML في Android.
إذا شعرت أن هذا يجب أن يكون من الممكن القيام به ، فالرجاء قيادة هذه المشكلة على Tracker الرسمي 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.