Android:如何将Spannable.setSpan与String.format结合起来?

时间:2011-08-26 10:36:21

标签: android resources spannable

我将Span设置为部分文字。跨度本身运作良好。但是,文本是由String.format Resources创建的,我不知道文本中的startend我将设置Span为。

我尝试在strings.xml中使用自定义HTML标记,但getTextgetString将其删除。我可以使用类似getString(R.string.text, "<nb>" + arg + "</nb>")的内容,然后使用Html.fromHtml(),因为arg正是我想要设置跨度的地方。

我看到this approach使用了格式化"normal text ##span here## normal text"的文字。它解析字符串删除标签并设置Span。

我的问题是,有没有更好的方法来完成将Span设置为格式化字符串"something %s something"或者我应该使用上述方法之一?

4 个答案:

答案 0 :(得分:2)

我通过引入TaggedArg类解决了这个问题,此类的实例扩展为<tag>value</tag>。然后我创建了一个对象,负责读取包含标签的文本并用跨度替换这些标签。在地图标记 - >工厂中注册了不同的跨度。

有一点惊喜。如果您有"<xx>something</xx> something"之类的文字,则Html.fromHtml会将此文字视为"<xx>something something</xx>"。我必须在整个文本周围添加标记<html>以防止这种情况。

答案 1 :(得分:2)

getText()将返回包含strings.xml中定义的格式的SpannedString个对象。我创建了a custom version of String.format,它将保留格式字符串中的任何跨度,即使它们包含格式说明符(SpannedString参数中的跨度也被保留)。像这样使用它:

Spanned toDisplay = SpanFormatter.format(getText(R.string.foo), bar, baz, quux);

答案 2 :(得分:0)

乔治认为getText()的方式很有趣。但是没有必要写一个额外的课程。 getText()会返回CharSequence。因此,使用SpannableStringBuilder将其设置为TextView:

textViewCell.setText(new SpannableStringBuilder(getText(R.string.foo));

在strings.xml中,您可以使用html-tags编写它(不要忘记文本前后的引用):

<string name="foo">"CO<sub><small>2</small></sub>"</string>

这种标记对我来说既可以手动设置为textView(如上所述),也可以在布局中指定。

答案 3 :(得分:0)

我决定写一本乔治here提供的Kotlin版本,以防链接有一天消失:

/*
* Copyright © 2014 George T. Steel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//https://github.com/george-steel/android-utils/blob/master/src/org/oshkimaadziig/george/androidutils/SpanFormatter.java
/**
 * Provides [String.format] style functions that work with [Spanned] strings and preserve formatting.
 *
 * @author George T. Steel
 */
object SpanFormatter {
    private val FORMAT_SEQUENCE: Pattern = Pattern.compile("%([0-9]+\\$|<?)([^a-zA-z%]*)([[a-zA-Z%]&&[^tT]]|[tT][a-zA-Z])")

    /**
     * Version of [String.format] that works on [Spanned] strings to preserve rich text formatting.
     * Both the `format` as well as any `%s args` can be Spanned and will have their formatting preserved.
     * Due to the way [android.text.Spannable]s work, any argument's spans will can only be included **once** in the result.
     * Any duplicates will appear as text only.
     *
     * @param format the format string (see [java.util.Formatter.format])
     * @param args
     * the list of arguments passed to the formatter. If there are
     * more arguments than required by `format`,
     * additional arguments are ignored.
     * @return the formatted string (with spans).
     */
    fun format(format: CharSequence?, vararg args: Any?): SpannedString {
        return format(java.util.Locale.getDefault(), format, *args)
    }

    /**
     * Version of [String.format] that works on [Spanned] strings to preserve rich text formatting.
     * Both the `format` as well as any `%s args` can be Spanned and will have their formatting preserved.
     * Due to the way [android.text.Spannable]s work, any argument's spans will can only be included **once** in the result.
     * Any duplicates will appear as text only.
     *
     * @param locale
     * the locale to apply; `null` value means no localization.
     * @param format the format string (see [java.util.Formatter.format])
     * @param args
     * the list of arguments passed to the formatter.
     * @return the formatted string (with spans).
     * @see String.format
     */
    fun format(locale: java.util.Locale, format: CharSequence?, vararg args: Any?): SpannedString {
        val out = SpannableStringBuilder(format)
        var i = 0
        var argAt: Int = -1
        while (i < out.length) {
            val m: java.util.regex.Matcher = FORMAT_SEQUENCE.matcher(out)
            if (!m.find(i))
                break
            i = m.start()
            val exprEnd: Int = m.end()
            val argTerm: String? = m.group(1)
            val modTerm: String? = m.group(2)
            val typeTerm: String? = m.group(3)
            var cookedArg: CharSequence
            when (typeTerm) {
                "%" -> cookedArg = "%"
                "n" -> cookedArg = "\n"
                else -> {
                    val argIdx: Int = when (argTerm) {
                        "" -> ++argAt
                        "<" -> argAt
                        else -> argTerm!!.substring(0, argTerm.length - 1).toInt() - 1
                    }
                    val argItem: Any? = args[argIdx]
                    cookedArg = if ((typeTerm == "s") && argItem is Spanned) {
                        argItem
                    } else {
                        String.format(locale, "%$modTerm$typeTerm", argItem)
                    }
                }
            }
            out.replace(i, exprEnd, cookedArg)
            i += cookedArg.length
        }
        return SpannedString(out)
    }
}