[英]How to add font style to textview android kotlin
I need help, i am stuck in this.我需要帮助,我陷入了困境。 My requirement is, I need to add text inside a tooltip.
我的要求是,我需要在工具提示中添加文本。 I did that actually.
我实际上是这样做的。 But the text include multiple line text also some lines having bold style other line having regular style with custom font style.
但是文本包括多行文本,还有一些行具有粗体样式,而另一些行具有常规样式和自定义字体样式。 By the below code which i am using, i can able to add bold, italic and regular style.
通过我正在使用的以下代码,我可以添加粗体、斜体和常规样式。 But i need to add below custom font style into the text.
但我需要在文本中添加以下自定义字体样式。 Please have a look into my below code.
请查看我的以下代码。
textViewToShow.setOnClickListener { view ->
val tfBold = Typeface.createFromAsset(
context!!.assets,
"fonts/myriad_pro_bold.ttf"
)
val tfRegular = Typeface.createFromAsset(
context!!.assets,
"fonts/myriad_pro_regular.ttf"
)
var string1 = “Item show in Bold style”
var string2 = “Item show in Regular style”
var string3 = “Item show in Regular style with item description”
val holesTextView = Html.fromHtml("<b>" + string1 + "</b> <br><br> <b><i>"+ string2 + "</i></b> " + string3)
showToolTip(view, holesTextView)
}
private fun showToolTip(view: View, holesTextView: Spanned) {
val builder: Tooltip.Builder =
Tooltip.Builder(view, R.style.Tooltip)
.setText(holesTextView)
builder.show()
}
Any solution/suggestions are highly appreciated非常感谢任何解决方案/建议
I would create a font family我会创建一个字体系列
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/myriad_pro_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/myriad_pro_italic" />
<font
android:fontStyle="normal"
android:fontWeight="700"
android:font="@font/myriad_pro_bold" />
</font-family>
following the answer of How to create custom font family with bold font for the bold font.遵循如何为粗体字体创建自定义字体系列的答案。
And then you can assign the font family in the layout:然后您可以在布局中分配字体系列:
<EditText
android:fontFamily="@font/mycustomfont"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
Or Programatically:或以编程方式:
val typeface = ResourcesCompat.getFont(context, R.font.mycustomfont)
holesTextView.setTypeface(typeface)
Or in the case of a tooltip using its setTypeface()
method:或者在工具提示使用其
setTypeface()
方法的情况下:
val typeface = ResourcesCompat.getFont(context, R.font.mycustomfont)
val builder: Tooltip.Builder = Tooltip.Builder(view, R.style.Tooltip)
.setText(holesTextView)
.setTypeface(typeface)
builder.show()
And use HtmlCompat.fromHtml as Html.fromHtml is deprecated:并使用HtmlCompat.fromHtml作为Html.fromHtml已弃用:
val holesTextView = HtmlCompat.fromHtml("<b>" + string1 + "</b> <br><br> <b><i>"+ string2 + "</i></b> " + string3, HtmlCompat.FROM_HTML_MODE_LEGACY)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.