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

发布于 2024-12-01 10:54:59 字数 756 浏览 1 评论 0原文

我将 Span 设置为文本的一部分。 Span 本身效果很好。但是,文本是由 Resources 中的 String.format 创建的,我不知道部分的 startend在文本中我将设置 Span 为。

我尝试在 strings.xml 中使用自定义 HTML 标记,但 getTextgetString 删除了它们。我可以使用类似这样的 getString(R.string.text, "" + arg + ""),然后使用 Html.fromHtml(),因为 arg 正是我想要设置 Span 的位置。

我看到 这种方法使用格式为“普通文本##span此处##普通文本”的文本。它解析字符串,删除标签并设置 Span。

有没有更好的方法将 Span 设置为格式化字符串,例如 "something %s some" 或者我应该使用上述方法之一?

I'm setting Span to part of the text. Span itself works well. However, the text is created by String.format from Resources and I do not know start and end of part in the text I'm going to set Span to.

I tryed to use custom HTML tags in strings.xml, but either getText or getString remove them. I could use something like this getString(R.string.text, "<nb>" + arg + "</nb>"), then Html.fromHtml(), because the arg is exactly where i want to set the Span.

I seen this approach that used text formatted "normal text ##span here## normal text". It parses the string removes tags and sets Span.

Is there a better way to set Span into a formatted string like "something %s something" or should I use one of the above approaches?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木槿暧夏七纪年 2024-12-08 10:54:59

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

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

getText() will return SpannedString objects that contain the formatting defined in strings.xml. I have created a custom version of String.format that will preserve any spans in the format string, even of they enclose format specifiers (spans in SpannedString arguments are also preserved). Use it like this:

Spanned toDisplay = SpanFormatter.format(getText(R.string.foo), bar, baz, quux);
虚拟世界 2024-12-08 10:54:59

我决定编写一个由 George 提供的 此处 的 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)
    }
}

I've decided to write a Kotlin version of what was offered here by George, in case the link goes away some day:

/*
* 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)
    }
}
山田美奈子 2024-12-08 10:54:59

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

有一个小惊喜。如果您有类似 "somethingSomething" 的文本,Html.fromHtml 会将该文本读取为 "something Something< /xx>”。我必须在整个文本周围添加标签 来防止这种情况发生。

I solved this by introducing TaggedArg class, instances of this class expands to <tag>value</tag>. Then I created object that is responsible for reading text containing tags and replacing these tags by spans. Different spans are registered in map tag->factory.

There was one little surprise. If you have text like "<xx>something</xx> something", Html.fromHtml reads this text as "<xx>something something</xx>". I had to add tags <html> around whole text to prevent this.

許願樹丅啲祈禱 2024-12-08 10:54:59

乔治认为的 getText() 方式很有趣。但不需要额外编写一个类。 getText() 返回一个 CharSequence。因此,使用 SpannableStringBuilder 将其设置为 TextView:

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

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

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

这种标记对我有用手动将其设置为 textView (如上)或将其分配到布局中。

The way by getText() that George supposed is interesting. But there is no need of writing an extra class. getText() returns a CharSequence. So use SpannableStringBuilder to set this to a TextView:

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

In your strings.xml you can write it with html-tags (do not forget the quotations before and after the text):

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

This kind of tagging worked for me either setting it manually to a textView (like above) or assigning it in a layout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文