设置自定义字体在字符串中

3

我有一个自定义字体BebasNeue.otf,我想将其设置在字符串变量中。我搜索了很多但没有得到任何解决方案。

我知道我们可以在视图中设置自定义字体,比如textView.setTypeface(font);。但现在我想将字体设置到以下Strings中:

String Name,Contact;
 Name="ABCD";
 Contact="97696538";

这些字符串都包含一些值。现在我想将自定义字体设置为以下所有字符串:

 Typeface fontString = Typeface.createFromAsset(m_context.getAssets(),
            "BebasNeue.otf");

有人可以告诉我如何在我的字符串变量中设置上述字体吗?

任何帮助都会受到赞赏。

谢谢。


你好,你想在哪里显示这些字符串? - ASP
我正在画布上绘制图像,并在图像上显示这些字符串。 - GrIsHu
请查看我的回答,我已解决了这个问题。感谢@ASP的帮助。 - GrIsHu
2个回答

5

我已解决我的问题,方法如下,并得到了@ASP提供的评论帮助。

 Typeface fontString = Typeface.createFromAsset(m_context.getAssets(),
                "BebasNeue.otf");
   Paint paint = new Paint();
   paint.setTypeface(fontString);
   mCanvas.drawText("ID :" + m_sId, dw - 450, dh - 350, paint);

感谢大家的帮助和迅速响应。

0

或许这可以帮助你。

public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache = new LruCache<String, Typeface>(
        12);

private Typeface mTypeface;

/**
 * Load the {@link Typeface} and apply to a {@link Spannable}.
 */
public TypefaceSpan(Context context, String typefaceName) {
    mTypeface = sTypefaceCache.get(typefaceName);

    if (mTypeface == null) {
        mTypeface = Typeface.createFromAsset(context.getApplicationContext().getAssets(), 
                                             String.format("%s", typefaceName));

        // Cache the loaded Typeface
        sTypefaceCache.put(typefaceName, mTypeface);
    }
}

@Override
public void updateMeasureState(TextPaint p) {
    p.setTypeface(mTypeface);

    // Note: This flag is required for proper typeface rendering
    p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}

@Override
public void updateDrawState(TextPaint tp) {
    tp.setTypeface(mTypeface);

    // Note: This flag is required for proper typeface rendering
    tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}

并像这样使用它

SpannableString s = new SpannableString("My Title");
s.setSpan(new TypefaceSpan(this, "MyTypeface.otf"), 0, s.length(),
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接