getTypeFace 从一个字符 [英] getTypeFace from a character
问题描述
格式化文本出现在编辑文本中.但字符串包含多个样式.
Formatted text appears in a edittext.But the string contains multiple styling .
是否可以使用 getTypeFace 命令获取用于字符串中特定字符的样式.
Is it possible to get the styling used on a particular character in a string , using getTypeFace command.
推荐答案
第一个问题是如何设置样式?
The first question is how to you set that styling?
如果您执行类似 Html.fromHtml("hello") 的操作,则调用 getTypeFace() 将返回 0 (TypeFace.NORMAL).为此,我想说您可能需要自己进行解析并根据您找到的 HTML 标签创建子字符串.
If your doing something like Html.fromHtml("hello") calling getTypeFace() will return 0 (TypeFace.NORMAL). For this I would say that you might need to make the parse yourself and create substrings according to the HTML tags you've found.
如果您为此使用 TextView 属性 - android:textStyle="bold"
If you your using the TextView attributes for this - android:textStyle="bold"
您可以直接拨打:
Log.d(TAG, "has typeface=${tv_typeface.typeface.style}")
- 或者,如果您使用 SpannableStrings 来编辑文本的外观
- Alternatively if you're using SpannableStrings to edit how your text should look
您可以执行以下操作:
//Set an italic style to the word "hello"
val spannableString = SpannableString("hello world")
spannableString.setSpan(StyleSpan(Typeface.ITALIC), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
tv_typeface.text = spannableString
//Get the style italic used
val spannedString = tv_typeface.text as SpannedString
val spans = spannedString.getSpans(0, tv_typeface.length(), StyleSpan::class.java)
for (span in spans) {
Log.d(TAG, "StyleSpan between: ${spannedString.getSpanStart(span)} and ${spannedString.getSpanEnd(span)} with style ${span.style}")
}
这篇关于getTypeFace 从一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!