在运行时为 Textview 更改字体
Change Typefaces at runtime for Textview
我有一个自定义 TextView
并且我从我的服务器获取所有文本,所以我永远不知道会出现什么样式。例如,这可以包括 bold
、italic
和更多 Textstyles
。但我不太确定如何在运行时处理它。
我创建了一个 assets
文件夹,其中包含我想使用的所有字体:
在我的 CustomTextView 中,我尝试了这样的操作:
public class CustomTextView extends TextView {
private static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android";
public CustomTextView(Context context) {
super(context);
applyCustomFont(context, null);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyCustomFont(context, attrs);
}
private void applyCustomFont(Context context, AttributeSet attrs) {
//Workaround for Preview Mode
if (!isInEditMode()) {
int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);
Typeface customFont = selectTypeface(context, textStyle);
setTypeface(customFont);
} else {
this.setTypeface(null, Typeface.NORMAL);
}
}
private Typeface selectTypeface(Context context, int textStyle) {
switch (textStyle) {
case Typeface.BOLD: // bold
return FontCache.getTypeface("fonts/OpenSans-Bold.ttf", context);
case Typeface.ITALIC: // italic
return FontCache.getTypeface("fonts/OpenSans-Italic.ttf", context);
default:
return FontCache.getTypeface("fonts/OpenSans-Regular.ttf", context);
}
}
}
这是我的 FontCache Class:
public class FontCache {
//This caches the fonts while minimizing the number of accesses to the assets
private static final HashMap<String, Typeface> fontCache = new HashMap<>();
public static Typeface getTypeface(String fontname, Context context)
{
Typeface typeface = fontCache.get(fontname);
if (typeface == null)
{
try {
typeface = Typeface.createFromAsset(context.getAssets(), fontname);
} catch (Exception e) {
return null;
}
fontCache.put(fontname, typeface);
}
return typeface;
}
}
但这不是它的工作原理,有什么想法可以实现吗?
谢谢!
你可以覆盖 setTypeface(Typeface tf, int style)
@Override
public void setTypeface(Typeface tf, int style) {
Typeface customFont = selectTypeface(context, textStyle)
super.setTypeface(customFont, style);
}
从外面你可以这样称呼它
mTextView.setTypeface(null, Typeface.BOLD);
我有一个自定义 TextView
并且我从我的服务器获取所有文本,所以我永远不知道会出现什么样式。例如,这可以包括 bold
、italic
和更多 Textstyles
。但我不太确定如何在运行时处理它。
我创建了一个 assets
文件夹,其中包含我想使用的所有字体:
在我的 CustomTextView 中,我尝试了这样的操作:
public class CustomTextView extends TextView {
private static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android";
public CustomTextView(Context context) {
super(context);
applyCustomFont(context, null);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyCustomFont(context, attrs);
}
private void applyCustomFont(Context context, AttributeSet attrs) {
//Workaround for Preview Mode
if (!isInEditMode()) {
int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);
Typeface customFont = selectTypeface(context, textStyle);
setTypeface(customFont);
} else {
this.setTypeface(null, Typeface.NORMAL);
}
}
private Typeface selectTypeface(Context context, int textStyle) {
switch (textStyle) {
case Typeface.BOLD: // bold
return FontCache.getTypeface("fonts/OpenSans-Bold.ttf", context);
case Typeface.ITALIC: // italic
return FontCache.getTypeface("fonts/OpenSans-Italic.ttf", context);
default:
return FontCache.getTypeface("fonts/OpenSans-Regular.ttf", context);
}
}
}
这是我的 FontCache Class:
public class FontCache {
//This caches the fonts while minimizing the number of accesses to the assets
private static final HashMap<String, Typeface> fontCache = new HashMap<>();
public static Typeface getTypeface(String fontname, Context context)
{
Typeface typeface = fontCache.get(fontname);
if (typeface == null)
{
try {
typeface = Typeface.createFromAsset(context.getAssets(), fontname);
} catch (Exception e) {
return null;
}
fontCache.put(fontname, typeface);
}
return typeface;
}
}
但这不是它的工作原理,有什么想法可以实现吗? 谢谢!
你可以覆盖 setTypeface(Typeface tf, int style)
@Override
public void setTypeface(Typeface tf, int style) {
Typeface customFont = selectTypeface(context, textStyle)
super.setTypeface(customFont, style);
}
从外面你可以这样称呼它
mTextView.setTypeface(null, Typeface.BOLD);