【发布时间】:2014-10-29 10:00:07
【问题描述】:
我有一个自定义类如下:
public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;
public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
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);
}
}
我按如下方式应用跨度:
Spannable spannable = new SpannableString(dispStatsPart1()+"\n"+dispStatswords()+"Months");
spannable.setSpan( new CustomTypefaceSpan("sans-serif",Helv_cond_bold), 0, dispStatsPart1().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif",helv_light), dispStatsPart1().length(), dispStatsPart1().length() +(dispStatswords()+"Months").length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.numberText.setText(spannable);
我想要第一行不同的字体大小和第二行不同的字体大小,如何实现?
到目前为止,我已经尝试将我的代码修改为:
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
if(flag == 0){
paint.setTextSize(50);
}else{
paint.setTextSize(50);
}
paint.setTypeface(tf);
}
我将标志传递给构造函数:
public CustomTypefaceSpan(String family, Typeface type, int flg) {
super(family);
newType = type;
flag = flg;
}
但上述方法似乎不起作用,字体大小增加或减少,但两个字符串都会发生这种情况。
【问题讨论】:
-
虽然您的回答很有帮助,但我正在使用自定义类,我想知道在该上下文中的实现。
标签: java android spannablestring