最近在做车联网的产品,主打的是语音交互和导航功能,UI给的导航界面可真是够酷炫的。但麻烦的事情也来了,里面的一句话居然用到了三种字体。界面如图所示:
从图中可以看出 500m左前方行驶 居然使用了三种字体,数字一种、英文一种、汉字一种,(这里不讨论拆分三个textview能不能实现的问题,如果能实现也是最迫不得已的办法,何况你解决了这个,上面那个 -2h30m 你要拆成4个textview吗?显然这不合理)我们知道spannableString是个 很强大的类,可以通过new typefacespan(family)设置字体,但他们支持的是系统的三种字体,但我还从没有使用过自定义的字体。为了解决这个问题我仔细看了关于spannableString的介绍。然而这类文章真的不多,只是从一篇文章中得知可以通过自定义typefacespan来使用自定义字体。(文章地址:http://www.cnblogs.com/jisheng/archive/2013/01/10/2854088.html)
如何自定义typefacespan,这东西也没别人做过先例,无奈只好自己去看源码:
/*
* Copyright (C) 2006 The Android Open Source Project
*
* 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.
*/ package android.text.style; import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Parcel;
import android.text.ParcelableSpan;
import android.text.TextPaint;
import android.text.TextUtils; /**
* Changes the typeface family of the text to which the span is attached.
*/
public class TypefaceSpan extends MetricAffectingSpan implements ParcelableSpan {
private final String mFamily; /**
* @param family The font family for this typeface. Examples include
* "monospace", "serif", and "sans-serif".
*/
public TypefaceSpan(String family) {
mFamily = family;
} public TypefaceSpan(Parcel src) {
mFamily = src.readString();
} public int getSpanTypeId() {
return TextUtils.TYPEFACE_SPAN;
} public int describeContents() {
return 0;
} public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mFamily);
} /**
* Returns the font family name.
*/
public String getFamily() {
return mFamily;
} @Override
public void updateDrawState(TextPaint ds) {
apply(ds, mFamily);
} @Override
public void updateMeasureState(TextPaint paint) {
apply(paint, mFamily);
} private static void apply(Paint paint, String family) {
int oldStyle; Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
} Typeface tf = Typeface.create(family, oldStyle);
int fake = oldStyle & ~tf.getStyle(); if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
} if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
} paint.setTypeface(tf);
}
}
从源码中我们可以看到构造函数(36~38行)传入了一个family的字符串,这个是用来找系统字体的,然后我们往下看,哪里用到了这个family(83行):
我们通过Typeface.create(family,oldStyle)得到了一个typeface,然后从86~92行是设置粗体和斜体。也就是说这个地方才是设置字体的真谛。而我们知道可以通过读取文件的方式得到自定义的typeface,因此完全可以通过掉包的方式实现自定义字体。于是我仿照Typefacespan实现了自己的一个MyTypefaceSpan的类,如下:
package com.justenjoy.view; import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan; /**
* 类名:MyTypefaceSpan <br/>
* 作者 :王洪贺 <br/>
* 描述: <br/>
* 2015年7月15日
*/
public class MyTypefaceSpan extends MetricAffectingSpan { private final Typeface typeface; public MyTypefaceSpan(final Typeface typeface) {
this.typeface = typeface;
} @Override
public void updateDrawState(final TextPaint drawState) {
apply(drawState);
} @Override
public void updateMeasureState(final TextPaint paint) {
apply(paint);
} private void apply(final Paint paint) {
final Typeface oldTypeface = paint.getTypeface();
final int oldStyle = oldTypeface != null ? oldTypeface.getStyle() : 0;
final int fakeStyle = oldStyle & ~typeface.getStyle();
if ((fakeStyle & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fakeStyle & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(typeface);
} }
使用方法也很简单,之前的TypefaceSpan不是传family吗?咱这个传typeface就可以了。为了方便使用,我做了一个单例,因为字体文件在一个程序中会多次使用,使用的时候放到内存中还是比较好的,公共类如下:
package com.justenjoy.util; import com.justenjoy.view.MyTypefaceSpan; import android.content.Context;
import android.graphics.Typeface; /**
* 类名:FontsUtil <br/>
* 作者 :王洪贺 <br/>
* 描述:获取自定义字体typefacespan的单例 <br/>
* 2015年7月15日
*/
public class FontsUtil { public static FontsUtil fontsUtil; private Context mContext;
private static Typeface numTypeface;
private static Typeface charTypeface; public FontsUtil(Context context) {
this.mContext = context;
// 字体资源放在内存中,避免反复读取浪费资源
numTypeface = Typeface.createFromAsset(mContext.getAssets(),
"fonts/290-CAI978.ttf");
charTypeface = Typeface.createFromAsset(mContext.getAssets(),
"fonts/048-CAT978.ttf"); } /**
* <br/>
* 概述:字体单例,避免反复读取 <br/>
*
* @param context
* <br/>
* @return
*/
public static FontsUtil getInstance(Context context) {
if (fontsUtil == null) {
fontsUtil = new FontsUtil(context);
}
return fontsUtil;
} /**
* <br/>
* 概述:获取英文字母的字体typefacespan <br/>
*
* @param context
* <br/>
* @return
*/
public MyTypefaceSpan getMyCharTypefaceSpan() {
return new MyTypefaceSpan(charTypeface);
} /**
* <br/>
* 概述:获取数字的字体typefacespan <br/>
*
* @param context
* <br/>
* @return
*/
public MyTypefaceSpan getMyNumTypefaceSpan() {
return new MyTypefaceSpan(numTypeface);
} }
在spannableString的使用就是:
spannableString.setSpan(FontsUtil.getInstance(this).getMyNumTypefaceSpan(), 0, stringsize,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
其中的两个字体文件是数字和英文的自定义字体,我就不上传了。有什么不明白可以联系我的qq或者邮箱,留言也可以。
我的github地址:https://github.com/dongweiq/study
欢迎关注,欢迎star o(∩_∩)o 。有什么问题请邮箱联系 dongweiqmail@gmail.com qq714094450