安卓如何在绘图画笔中设置自定义字体

115
我想将文本绘制到画布上。如何使用自定义字体(例如Helvetica)以及加粗字体?我更愿意使用系统字体而不是从资源中创建。谢谢。

"paint": 你是指 Canvas 吗? - Tony the Pony
是的,我需要这个画笔来设置一些样式... - Buda Gavril
如何根据语言环境设置字体,例如对于英语,我们想使用arial.ttf,而对于韩语,我想使用gothic_B.ttf。在Android中的画布上绘制时如何实现? - Ashish Dwivedi
1
@DwivediJi:你是否尝试将这个问题发布到StackOverflow上,而不是作为别人问题的评论? - Michael Scheper
7个回答

183
如果您所说的“自定义字体”是指提供为资产的字体,则以下代码应该可以正常工作:
Typeface plain = Typeface.createFromAsset(assetManager, pathToFont); 
Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD)
Paint paint = new Paint();
paint.setTypeface(bold);
canvas.drawText("Sample text in bold",0,0,paint);

6
尝试:Typeface plain = Typeface.createFromAsset(assetManager, pathToFont); Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD) - Tony the Pony
2
@TonythePony,你的代码对我不起作用。 Typeface fontFace = Typeface.createFromAsset(getAssets(), "fonts/comic.TTF"); Typeface face = Typeface.create(fontFace, Typeface.BOLD); Paint paint = new Paint(); paint.setTextAlign(Paint.Align.CENTER); paint.setColor(Color.WHITE); paint.setTextSize(10); paint.setTypeface(face);paint.setFlags(Paint.ANTI_ALIAS_FLAG); - Ashish Dwivedi
1
你尝试过 getContext().getAssets() 吗? - Wesley
2
Typeface.DEFAULT_BOLD 也给我带来了问题,但改为 Typeface.BOLD 就可以了。 - CrandellWS
我该如何使用新的支持库功能加载字体:https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html? - android developer
显示剩余4条评论

67

如果你正在使用Android的新的Fonts in XML来处理字体,那么为了获取绘制所用的字体,你可以使用以下代码:

val customTypeface = ResourcesCompat.getFont(context, R.font.myfont)

或者如果您的最低Android API版本大于等于26

val customTypeface = resources.getFont(R.font.myfont)

然后将其应用到您的绘画对象:

mTextPaint.typeface = customTypeface

欲了解更多信息,请查看https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml#fonts-in-code


1
这是目前最好的答案,请点赞以便其他人能够首先看到它。 - rwozniak

19

使用这个来进行绘画课:

 Paint paint = new Paint();
   paint.setTypeface(Typeface.create("Arial",Typeface.ITALIC));

11

如果您已经使用了某种字体,并想要使用其粗体版本,可以这样做。

currentPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
currentPainter.setColor(Color.WHITE);
currentPainter.setTextSize(Utils.sp2px(getResources(), 14)); // set font size
Typeface currentTypeFace =   currentPainter.getTypeface();
Typeface bold = Typeface.create(currentTypeFace, Typeface.BOLD);
currentPainter.setTypeface(bold);

我使用了上面的答案,不过对我来说需要做出修改 - 所以想提一下


5
如果你想使用资源中的字体(Kotlin):
val textPaint = TextPaint()
textPaint.typeface = resources.getFont(R.font.font_name)

这可能与问题无关,但这正是我在寻找的 - 也许有人也需要它。


1
这是最简单的解决方案,但它只适用于SDK 26+。当针对旧版Android时,您也可以使用ResourcesCompat.getFont(context, R.font.font_name) - Slav

3

自定义字体必须放置在资产文件夹中。

以下代码可能会对您有所帮助

Paint p = new Paint();
//Set font
Typeface plain = Typeface.createFromAsset(context.getAssets(), "custom_font.ttf");
p.setTypeface(plain);

2
使用FontUtils Kotlin对象
object FontUtils {

    private const val FONT_PATH_LATO_REGULAR = "lato_regular.ttf"

    fun getDefaultTypeface(context: Context): Typeface {
        return Typeface.createFromAsset(context.assets, FONT_PATH_LATO_REGULAR)
    }
}

然后您可以将其用作:

paint.typeface = FontUtils.getDefaultTypeface(context)

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