سبد (0)

آموزش سفارشی سازی فونت در اندروید

آموزش سفارشی سازی فونت در اندرید

در اندروید می توانید برای رشته ها در برنامه فونتهای خود را تعریف کنید. فقط لازم است فونتها را از اینترنت دانلود کنید و سپس آن را در فولدر assets/fonts  قرار دهید.

پس از قرار دادن فونت ها در فولدر assets زیر فولدر فونت ها، می توانید در کد جاوای خود از طریق گروه Typeface، به آن دسترسی داشته باشید. ابتدا مرجع ویوی تکست را در کد به دست آورید. ترکیب آن در زیر داده شده است .

 

TextView tx = (TextView)findViewById(R.id.textview1);

کار بعدی که باید انجام دهید، فراخواندن روش استاتیک از گروه createFromAsset() می باشد تا فونت خود را از assets به دست آورید. ترکیب آن در زیر ارائه شده است

 

Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");

آخرین کاری که باید انجام دهید تنظیم این فونت برروی پراپرتی TextView Typeface می باشد. برای انجام این کار لازم است روش setTypeface() را فرا بخوانید. ترکیب آن در زیر ارائه شده است .

 

tx.setTypeface(custom_font);

علاوه بر این روش ها، روش های دیگری وجود دارند که در گروه Typrface  تعریف شده اند که می توانید از آنها استفاده کنید تا دسترسی موثرتری به فونت ها داشته باشید.

  • create(String familyName, int style)   

    یک آبجکت Typeface  با یک نام خانوادگی و اطلاعات استایل، ایجاد کنید. 
  • create(Typeface family, int style)   

    یک آبجکت Typeface ایجاد کنید که به بهترین شکل با Typeface مشخص شده ی موجود و استایل مشخص شده هماهنگ باشد. 
  • createFromFile(String path) 

    از فایل فونت مشخص شده یک Typeface جدید ایجاد کنید. 
  • defaultFromStyle(int style) 

    بر اساس یک استایل مشخص، یکی از آبجکت های Typeface پیش فرض را بازمی گرداند. 
  • getStyle() 

    ویژگی های درونی Typeface را بازمی گرداند. 

مثال

در اینجا مثالی را می بینید که استفاده ی Typeface را در به کار گرفتن CustomFont توضیح می دهد. این برنامه یک برنامه ی پایه ایجاد می کند که فونتی را نمایش می دهد که شما در فایل فونت ها مشخص کرده اید. برای آزمایش با این مثال می توانید آن را روی یک دستگاه واقعی یا یک مقلد اجرا کنید.

  • شما برای ایجاد Eclipse IDE برای ایجاد یک برنامه ی اندروید استفاده می کنید و آن را با عنوان CustomFonts تحت پکیج com.example.customfonts نام گذاری می کنید. هنگام ایجاد این برنامه مطمئن شوید که Target SDK و Compile With در آخرین ورژن خود هستند تا از سطوح بالاتر API استفاده کنید. 
  • یک فونت از اینترنت دانلود کنید و آن را تحت فولدر assets/fonts قرار دهید. 
  • فایل src/MainActivity.java را تغییر دهید تا کد لازم را اضافه کنید. 
  • res/layout/activity_main را تغییر دهید تا مولفه های مربوط به XML را اضافه کنید.
  • res/values/string.xml را تغییر دهید تا مولفه های رشته ی مورد نیاز را اضافه کنید.
  • برنامه را اجرا کنید و یک دستگاه اجرایی اندروید انتخاب کنید و برنامه را روی آن نصب کنید و نتایج را بررسی کنید.

در زیر محتوای فایل MainAcrivity.java را مشاهده می کنید.

 

package com.example.sairamkrishna.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
   TextView tv1,tv2;
   
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      tv1=(TextView)findViewById(R.id.textView3);
      tv2=(TextView)findViewById(R.id.textView4);
      
      Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf");
      tv1.setTypeface(face);
      
      Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf");
      tv2.setTypeface(face1);
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
   }
   
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      
      int id = item.getItemId();
      
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

در زیر محتوای تغییر یافته  res/layout/activity_main.xml را مشاهده می کنید

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Typeface"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:textColor="#ff16ff01" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView3"
android:layout_centerVertical="true"
android:textSize="45dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView4"
android:layout_below="@+id/textView3"
android:layout_alignLeft="@+id/textView3"
android:layout_alignStart="@+id/textView3"
android:layout_marginTop="73dp"
android:textSize="45dp" />

</RelativeLayout>

در زیر محتوای فایل res/values/string.xml را مشاهده می کنید.

 

<resources>
<string name="app_name">My Application</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>

در زیر محتوای فایل AndroidManifest.xml را مشاهده می کنید.

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name=".MainActivity"
android:label="@string/app_name" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

</application>
</manifest>

 

اجازه دهید برنامه ی فونت خود را که کمی تغییر داده ایم، اجرا کنیم. فرض می کنیم AVD خود را در هنگام انجام تنظیمات محیط ایجاد کرده اید. برای اجرای برنامه از Eclipseیکی ازفایل های فعالیت برنامه را باز کرده و روی آیکن  Runاز تولبار کلیک کنید. 

 

تذکر

در هنگام استفاده از این فونت باید اندازه و کاراکتری را که توسط فونت پشتیبانی می شود، حفظ کنید.

تمامی محصولات و خدمات این وبسایت، حسب مورد دارای مجوزهای لازم از مراجع مربوطه می‌باشند و فعالیت‌های این سایت تابع قوانین و مقررات جمهوری اسلامی ایران است.
logo-samandehi مجوز نشر دیجیتال از وزرات فرهنگ و ارشاد اسلامی پرداخت آنلاین -  بانک ملت معرفی بیاموز در شبکه سه