ふわふわぷかぷか

最近はイラレとAeにはまってます。

フォントを変更したTextCrockを作成する方法。

フォントを変更したTextCrockを作成する方法。

テキストビューにも応用できました。

ウィジェットでフォントを変えたいなーと思ってやってみて、結局ウィジェットでは表示できなかったけど、うまく使えば役立ちそうなのでメモ。

 

参考サイト

http://www.androiddevelopersolutions.com/2015/03/android-applying-custom-fonts.html

 

①values/strings.xmlに <string name="●●●">●●●.TTF</string>を増やす

<resources>
<string name="app_name">FuwapukaWatch</string>
<string name="FONT_fuwa">フォント.TTF</string>
</resources>
string.xml#sthash.9BlMnV91.dpuf
string.xml#sthash.9BlMnV91.dpuf
string.xml#sthash.9BlMnV91.dpuf

 

 ②values/styles.xmlに↓を増やす

<style name="CustomDigital">
<item name="font">@string/FONT_fuwa</item>
</style>
values/styles.xml

 

③valuesにattrs.xmlを作る(↓内容)

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Fontify">
<attr name="font" format="string" />
</declare-styleable>

<!-- Allows attribute use in an AppTheme definition -->
<declare-styleable name="FontifyTheme">
<attr name="fontifyStyle" format="reference"/>
</declare-styleable>
</resources>

 

④TextClockstyle="@style/CustomDigital"を増やして、クラスの内容を反映させる

<jp.memorylovers.fuwapukawatch.TimeTextClock
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="50dp"
android:timeZone="GMT+900"
android:format24Hour="HH:mm"
android:format12Hour="hh:mm"
tools:targetApi="jelly_bean_mr1"
style="@style/CustomDigital"/>

 

⑤クラスFontManagerを作る(↓内容)

package jp.memorylovers.fuwapukawatch;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;


public class FontManager {
private static FontManager sInstance;
private Map<String, Typeface> mCache;
private static int sDefaultStyle = R.attr.fontifyStyle;

private FontManager() {
mCache = new HashMap<String, Typeface>();
}

public static FontManager getInstance() {
if (sInstance == null) {
sInstance = new FontManager();
}

return sInstance;
}

public void setFont(TextView tv, AttributeSet attrs) {
String fontName = getFontName(tv.getContext(), attrs);
setFont(tv, fontName);
}

public void setFont(TextView tv, String fontName) {
if (fontName == null) return;

Typeface typeface = mCache.get(fontName);
if (typeface == null) {
typeface = Typeface.createFromAsset(tv.getContext().getAssets(), fontName);
mCache.put(fontName, typeface);
}

tv.setTypeface(typeface);
}

public static String getFontName(Context c, AttributeSet attrs) {
TypedArray arr = c.obtainStyledAttributes(
attrs,
R.styleable.Fontify,
sDefaultStyle,
0
);

String fontName = arr.getString(R.styleable.Fontify_font);
arr.recycle();
return fontName;
}
}

 

⑥クラスTimeTextClockを作る(↓内容)

package jp.memorylovers.fuwapukawatch;

import android.content.Context;
import android.util.AttributeSet;

//@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public class TimeTextClock extends android.widget.TextClock {
public TimeTextClock(Context context) {
super(context);
}

public TimeTextClock(Context context, AttributeSet attrs) {
super(context, attrs);

// return early for eclipse preview mode
if (isInEditMode()) return;

FontManager.getInstance().setFont(this, attrs);
}

public void setFont(String fontPath) {
FontManager.getInstance().setFont(this, fontPath);
}

public void setFont(int resId) {
String fontPath = getContext().getString(resId);
setFont(fontPath);
}
}

 

これでOK。

普通のActivityでは表示できました。

 

Androidウィジェット&ビュープログラミングガイド

Androidウィジェット&ビュープログラミングガイド