[英]FB Litho: How to set custom font for Text
My question is how to set a custom font for Text
component in Facebook's UI framework Litho ? 我的问题是如何在Facebook的UI框架Litho中为Text
组件设置自定义字体?
The following is my Text
component: 以下是我的Text
组件:
Text.create(componentContext)
.flexGrow(1f)
.verticalGravity(VerticalGravity.CENTER)
.text("MY APP")
.textSizeDip(25)
.textAlignment(Layout.Alignment.ALIGN_CENTER)
To use the typeface
prop, you must first obtain a Typeface
object, which is significantly easier if you can use Support Library v26, which introduces Fonts in XML ... it's pretty simple to do so from a ComponentContext
even inside your onCreateLayout
method (though presumably you may wish to cache the Typeface
): 要使用typeface
道具,必须首先获取一个Typeface
对象,如果可以使用支持库v26(它以XML引入字体)的话,这会非常容易。即使在onCreateLayout
方法内部,也可以从ComponentContext
做到这一点非常简单(尽管大概您可能希望缓存Typeface
):
Text.create(c)
.typeface(ResourcesCompat.getFont(
c.getApplicationContext, R.font.my_cool_font)
.text("Check out my cool font")
.build()
There is also a Typeface.Builder
class available if you do not have access to Support v26, which gives you a few options about how to specify the desired font. 如果您无权访问Support v26,则还有一个Typeface.Builder
类可用,它为您提供了一些有关如何指定所需字体的选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.