android 将自定义字体设置为油漆
我想在油漆上绘制文字。如何使用自定义字体(ex Helvetica)和粗体来绘制它?我更愿意使用系统字体而不是从资产创建它。谢谢。
I want to draw a text to a paint. How to draw it with a custom font (ex Helvetica ) and bold also? I would preffer to use a system font and not create it from assets. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您已经使用了某种字体并且想要使用该字体的粗体版本,则可以执行此操作。
我使用了上面的答案,但这种修改对我来说是必要的 - 所以我想提一下
If you already have a font in use and want to use a bold version of that you can do this.
I used the answer above, but this modification was necessary for me - so just thought I'd mention it
如果您想使用资源中的字体(Kotlin):
这可能与问题无关,但这就是我正在寻找的 - 也许有人也需要它。
If you want to use a font from resources (Kotlin):
This might not be related to the question, but this is what I was looking for - maybe somebody would need it too.
自定义字体必须放置在 asset 文件夹中。
也许下面的代码可以帮助你
The custom font must be placed in the assets folder.
Maybe the following code can help you
使用 FontUtils kotlin 对象
,您可以将其用作:
with FontUtils kotlin object
then you can use it as:
如果您在 XML 中使用 Android 的新字体作为您的字体,那么要获取用于绘制的字体,您可以使用:
或者如果您的最小 Android API >= 26
那么将其应用到您的绘制对象:
有关更多信息,请查看 < a href="https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml#fonts-in-code" rel="noreferrer">https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml#fonts-in-code
If you are using Android's new Fonts in XML for your fonts, then to get the typeface used for paint you can use:
or if your min Android API >= 26
Then to apply it to your paint object:
For more info check out https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml#fonts-in-code
将其用于油漆类:
Use this for paint class:
如果“自定义字体”是指作为资源提供的字体,则以下代码应该有效:
If by "custom font" you mean a font that you are supplying as an asset, the following code should work: