Android                
            사용자 정의 글꼴
        
        
            
    수색…
앱에 사용자 정의 글꼴 넣기
- 프로젝트 폴더로 이동합니다.
 - 그런 다음 app -> src -> main.
 - 기본 폴더에 'assets -> fonts'폴더를 만듭니다.
 - 'fontfile.ttf'를 fonts 폴더에 넣으십시오.
 
글꼴 초기화 중
private Typeface myFont;
// A good practice might be to call this in onCreate() of a custom
// Application class and pass 'this' as Context. Your font will be ready to use
// as long as your app lives
public void initFont(Context context) {
    myFont = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
}
        TextView에서 사용자 정의 글꼴 사용
public void setFont(TextView textView) {
    textView.setTypeface(myFont);    
}
        xml로 TextView에서 글꼴 적용 (필수 Java 코드)
TextViewPlus.java :
public class TextViewPlus extends TextView {
    private static final String TAG = "TextView";
    public TextViewPlus(Context context) {
        super(context);
    }
    public TextViewPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }
    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }
    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
        String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }
    public boolean setCustomFont(Context ctx, String asset) {
        Typeface typeface = null;
        try {
            typeface = Typeface.createFromAsset(ctx.getAssets(), asset);
        } catch (Exception e) {
            Log.e(TAG, "Unable to load typeface: "+e.getMessage());
            return false;
        }
        setTypeface(typeface);
        return true;
    }
}
 attrs.xml : ( res / values 를 저장할 위치)
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlus">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>
 사용하는 방법:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:foo="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.mypackage.TextViewPlus
        android:id="@+id/textViewPlus1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:text="@string/showingOffTheNewTypeface"
        foo:customFont="my_font_name_regular.otf">
    </com.mypackage.TextViewPlus>
</LinearLayout>
        캔버스 텍스트의 사용자 정의 글꼴
애셋에서 글꼴로 캔버스에 텍스트 그리기.
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/SomeFont.ttf");
Paint textPaint = new Paint();
textPaint.setTypeface(typeface);
canvas.drawText("Your text here", x, y, textPaint);
        효율적인 서체 반입
사용자 정의 글꼴을로드하면 성능이 저하 될 수 있습니다. 이미 사용 된 글꼴을 Hashtable에 저장 /로드하는 작은 조력자를 사용하는 것이 좋습니다.
public class TypefaceUtils {
private static final Hashtable<String, Typeface> sTypeFaces = new Hashtable<>();
/**
 * Get typeface by filename from assets main directory
 *
 * @param context
 * @param fileName the name of the font file in the asset main directory
 * @return
 */
public static Typeface getTypeFace(final Context context, final String fileName) {
    Typeface tempTypeface = sTypeFaces.get(fileName);
    if (tempTypeface == null) {
        tempTypeface = Typeface.createFromAsset(context.getAssets(), fileName);
        sTypeFaces.put(fileName, tempTypeface);
    }
    return tempTypeface;
}
 }
용법:
Typeface typeface = TypefaceUtils.getTypeface(context, "RobotoSlab-Bold.ttf");
setTypeface(typeface);
        전체 활동에 맞춤 글꼴
public class ReplaceFont {
public static void changeDefaultFont(Context context, String oldFont, String assetsFont) {
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), assetsFont);
    replaceFont(oldFont, typeface);
}
private static void replaceFont(String oldFont, Typeface typeface) {
    try {
        Field myField = Typeface.class.getDeclaredField(oldFont);
        myField.setAccessible(true);
        myField.set(null, typeface);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}
  그런 다음 onCreate() 메소드에서 액티비티를 수행하십시오. 
// Put your font to assets folder...
ReplaceFont.changeDefaultFont(getApplication(), "DEFAULT", "LinLibertine.ttf");
        Android O에서 글꼴 작업
Android O는 글꼴 작업 방법을 변경합니다.
Android O에는 글꼴을 XML 로 사용하여 글꼴을 자원으로 사용할 수있는 새로운 기능이 도입되었습니다. 즉, 글꼴을 자산으로 묶을 필요가 없습니다. 글꼴은 이제 R 파일로 컴파일되며 자원으로 시스템에서 자동으로 사용할 수 있습니다.
새 글꼴 을 추가하려면 다음을 수행해야합니다.
-  
res/font라는 새 리소스 디렉토리를 만듭니다. -  글꼴 파일을이 글꼴 폴더에 추가하십시오. 예를 들어 
myfont.ttf를 추가하면myfont.ttf를R.font.myfont글꼴을 사용할 수 있습니다. 
 다음 XML 파일을 res/font 디렉토리에 추가하여 자신 만의 글꼴 패밀리 를 만들 수도 있습니다. 
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>
 글꼴 파일과 글꼴 패밀리 파일을 같은 방식으로 사용할 수 있습니다.
XML 파일 에서
android:fontFamily속성을 사용하면android:fontFamily과 같이됩니다.<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/myfont"/>또는 다음과 같이하십시오.
<style name="customfontstyle" parent="@android:style/TextAppearance.Small"> <item name="android:fontFamily">@font/myfont</item> </style>코드 에서 다음 코드 줄을 사용합니다.
Typeface typeface = getResources().getFont(R.font.myfont); textView.setTypeface(typeface);
Modified text is an extract of the original Stack Overflow Documentation
        아래 라이선스 CC BY-SA 3.0
        와 제휴하지 않음 Stack Overflow