【问题标题】:How to detect/define string style using kotlin?如何使用 kotlin 检测/定义字符串样式?
【发布时间】:2019-11-24 06:51:37
【问题描述】:

我想知道如何检测来自 textView 的字符串是否以粗体书写。

例如:

textView.text = "I love Stackoverflow" // 你应该在 TextView 中看到的内容

它必须返回一个只包含 BOLD TEXT 的字符串:

if ( textView.text.style is BOLD) then //that's just an Algorithm to explain
myString = "Stackoverflow"

【问题讨论】:

  • A String 没有样式。 String 只是一个字符序列,仅此而已。 TextView 可以设置为以粗体显示整个文本,SpannedString 可以有粗体部分(使用StyleSpan)。
  • @WIKOSO 请不要多次问同一个问题

标签: android file intellij-idea kotlin styles


【解决方案1】:

尝试使用跨度。

        val tv = TextView(this)
        tv.text = Html.fromHtml("I love <b>Stack</b><i>Overflow</i><b>!!!</b>", Html.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE)
        val spannedString = tv.text as SpannedString
        val allSpans = spannedString.getSpans(0, tv.text.length, Any::class.java)

        //You can check spans; Please check that your span is StyleSpan
        ((allSpans as Array<Object>)[0] as StyleSpan).style == Typeface.BOLD //<b>Stack</b>
        ((allSpans as Array<Object>)[1] as StyleSpan).style == Typeface.ITALIC //<i>Overflow</i>
        ((allSpans as Array<Object>)[2] as StyleSpan).style == Typeface.BOLD //<b>!!!</b>

        //or get start and end
        spannedString.getSpanStart(((allSpans as Array<Object>)[0] as StyleSpan)) //start index of "Stack"
        spannedString.getSpanEnd(((allSpans as Array<Object>)[0] as StyleSpan)) //end index of "Stack"

最后,你可以得到开始和结束之间的子字符串

更新

                val spannedString = Html.fromHtml("" +
                "<b>1. Quels sont les facteurs de risque de la maladie athéromateuse ?</b><br>" + //BOLD 0
                "<b>a. Tabac</b><br>" + //BOLD 1
                "<b>b. hypertension artérielle</b><br>" + //BOLD 2
                "c. alcool<br>" +
                "d. Hypertriglycéridémie<br>" +
                "e. diabète<br>" +
                "<br>" +
                "<b>2. Parmi les facteurs de risque cardio-vasculaire, on ne retrouve pas :</b><br>" + //BOLD 3
                "a. Hypercholestérolémie<br>" +
                "b. <b>Insuffisance rénale</b><br>" + //BOLD 4
                "c. Tabagisme actif<br>" +
                "d. Sédentarité<br>" +
                "e. Sexe masculin<br>" +
                "3. Le tableau de dissection aiguë de l&#39;aorte ne comporte pas de :<br>" +
                "a. Hémopéricarde<br>" +
                "b. insuffisance mitrale aiguë<br>" +
                "c. Hémothorax<br>" +
                "d. Accident vasculaire cérébral<br>" +
                "e. Paraplégie", Html.TO_HTML_PARAGRAPH_LINES_CONSECUTIVE)


        // Find All bold substrings
        val allBoldSpans = spannedString.getSpans(0, spannedString.length, Any::class.java).filter {
//This is BOLD span?
            it is StyleSpan && it.style == Typeface.BOLD
        }

        val ONLY_BOLD_SUBSTRINGS = allBoldSpans.map {
//Extract BOLD text into output collection
            spannedString.substring(spannedString.getSpanStart(it), spannedString.getSpanEnd(it))
        }

it will return array with

    0 = "1. Quels sont les facteurs de risque de la maladie athéromateuse ?"
    1 = "a. Tabac"
    2 = "b. hypertension artérielle"
    3 = "2. Parmi les facteurs de risque cardio-vasculaire, on ne retrouve pas :"
    4 = "Insuffisance rénale"

【讨论】:

  • 检查此file 以了解我想检测此文档中的粗体文本我知道如何提取等,但我现在不知道如何检测粗体文本
  • 也许我听错了,你能解释一下你的问题吗? “我现在不知道如何检测粗体文本”。我回答了如何查找粗体子字符串的出现(完整示例请参见更新)。你的跨度不是 StyleSpan?或者您有什么问题?
  • 我正在创建一个多选应用程序,所以在我发送给您的文档中......有问题+选择(包括用粗体写的正确答案)我知道如何获取所有文本,但我的问题是如何将正确答案(用粗体写成)与其他选择分开......所以如果你有任何想法请帮助我!
  • 我想帮助你。没有代码,这不是很容易。我试图理解,你有什么格式的文本。我的例子错了吗?这个例子有什么问题?当您将数据替换为示例中的代码时会发生什么?结果不正确(并非所有粗体字符串都被提取或有任何问题)?据我了解,您已经有一个带有跨文本的文本视图,对吧?如果文本有跨度作为我的例子,一切都应该正常工作。
  • 否则,如果您的文本不是“跨区字符串”,我们需要找到任何粗体标记。或者也许你使用了“split”函数(这个函数将删除跨度,你不能将字符串转换为跨度)。或者您有来自 docx 库的“自定义”格式...如果我们了解哪些源数据以及其中的哪些标记,我们可以更快地找到解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-30
  • 2021-09-04
  • 1970-01-01
  • 2012-07-24
  • 2020-09-11
  • 2016-03-02
  • 1970-01-01
相关资源
最近更新 更多