How formatted string and then change style by annotations

android string resource with parameter
android string format
kotlin get string resource
android string placeholder
android textview html css style
android string escape
android text annotation
string array android

i have 3 strings localizations

<string name="tests" formatted="true">Test<annotation font="bold"> testBold %1$s</annotation> end</string>
<string name="tests" formatted="true">Тест<annotation font="bold"> тестБолд %1$s</annotation> конец</string>
<string name="tests" formatted="true">Тест<annotation font="bold"> тестБолд %1$s</annotation> кінець</string>

How i can add some argument and modified text by annotation then. The maximum that I get is to do this one thing

CharSequence t = getResources().getString(R.string.tests, "myValue");//in this case i lose my annotation, but set my argument
//OR
CharSequence t = getText(R.string.tests);//in this case i lose my argument but get style BOLD

public SpannableString textFormattingByTags(CharSequence t) {
        SpannedString titleText = new SpannedString(t);
        SpannedString titleText = (SpannedString) getText(R.string.tests);
        Annotation[] annotations = titleText.getSpans(0, titleText.length(), Annotation.class);
        SpannableString spannableString = new SpannableString(titleText);
        for (Annotation annotation : annotations) {
            if (annotation.getKey().equals("font")) {
                String fontName = annotation.getValue();
                if (fontName.equals("bold")) {
                    spannableString.setSpan(new CustomTypefaceSpan("",fontBold),
                            titleText.getSpanStart(annotation),
                            titleText.getSpanEnd(annotation),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return spannableString;
    }

result in first case i get "Test testBold MyValue end" in second "Test testBold %1$s end". Who had some ideas?


Typeface fontBold = Typeface.createFromAsset(getAssets(), "fonts/Trebuchet_MS_Bold.ttf");
    String s = getResources().getString(R.string.error_date, "20.02.2019", "25.02.2019");
    SpannableStringBuilder spannableString = new SpannableStringBuilder(s);

    Integer first1 = null;
    Integer first2 = null;
    Integer last1 = null;
    Integer last2 = null;
    int digits = 0;
    char[] crs = s.toCharArray();
    for (int i = 0; i < crs.length; i++) {
        if (Character.isDigit(crs[i]) && digits != 8) {
            if (first1 == null) {
                first1 = i;
            }
            last1 = i;
            digits++;
            continue;
        }
        if (Character.isDigit(crs[i])) {
            if (first2 == null) {
                first2 = i;
            }
            last2 = i;
        }

    }
    spannableString.setSpan(new CustomTypefaceSpan("", fontBold), first1, last1 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableString.setSpan(new CustomTypefaceSpan("", fontBold), first2, last2 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(getInstance());
    builder.setTitle("test");
    builder.setMessage(spannableString);
    builder.setCancelable(false);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            getLoaderManager().destroyLoader(LOADER_SOE_BILLING_ID);
        }
    });
    android.support.v7.app.AlertDialog alert = builder.create();
    alert.show();

How formatted string and then change style by annotations, How formatted string and then change style by annotations. 发布于 2020-06-26 18:27:42. i have 3 strings localizations <string name="tests"� Change Style Properties. Once you have chosen the annotations desired, you are ready to make the style changes. Again, there are three ways to do this: Press the F3 key on your keyboard. Click Edit, Edit Style on the menu. Right-click on the selected annotations and choose Edit Style from the pop up menu.


1. Convert arguments to annotations

Your string:

<string name="tests" formatted="true">Test<annotation font="bold"> testBold %1$s</annotation> end</string>

Becomes:

<string name="tests">Test<annotation font="bold"> testBold <annotation arg="0">%1$s</annotation></annotation> end</string>

2. Create SpannableStringBuilder from resource

val text = context.getText(R.string.tests) as SpannedString
val spannableText = SpannableStringBuilder(text)

3. Apply ALL arg annotations FIRST

An example implementation:

fun SpannableStringBuilder.applyArgAnnotations(vararg args: Any) {
    val annotations = this.getSpans(0, this.length, Annotation::class.java)
    annotations.forEach { annotation ->
        when (annotation.key) {
            "arg" -> {
                val argIndex = Integer.parseInt(annotation.value)
                when (val arg = args[argIndex]) {
                    is String -> {
                        this.replace(
                            this.getSpanStart(annotation),
                            this.getSpanEnd(annotation),
                            arg
                        )
                    }
                }
            }
        }
    }
}

Pass in arguments:

spannableText.applyArgAnnotations("myValue")

4. Apply remaining annotations

spannableText.applyAnnotations()
textView.text = spannableText

5. Result

Test testBold myValue end

Styling internationalized text in Android, We end up having to style different words, “text” and “Texto”, that have If you only want to apply basic styling, use HTML tags in the string We can then parse the list of annotations attached to the text and add the right span to our text. Let's say that we want to set a custom typeface, by applying a� Converts the value of objects to strings based on the formats specified and inserts them into another string. If you are new to the String.Format method, see the Get started with the String.Format method section for a quick overview. See the Remarks section for general documentation for the String.Format method.


How about change this code

<string name="tests" formatted="true">Test<annotation font="bold"> testBold %1$s</annotation> end</string>
<string name="tests" formatted="true">Тест<annotation font="bold"> тестБолд %1$s</annotation> конец</string>
<string name="tests" formatted="true">Тест<annotation font="bold"> тестБолд %1$s</annotation> кінець</string>

into

<string name="tests" formatted="true">Test<b> testBold %1$s</b> end</string>
<string name="tests" formatted="true">Тест<b> тестБолд %1$s</b> конец</string>
<string name="tests" formatted="true">Тест<b> тестБолд %1$s</b> кінець</string>

and you can use it like this

Spanned result = Html.fromHtml(getString(R.string.tests, "testing"));
textView.setText(result);

other styles include:

Tags                Format
--------------------------
b, strong           Bold
i, em, cite, dfn    Italics
u                   Underline
sub                 Subtext
sup                 Supertext
big                 Big
small               Small
tt                  Monospace
h1 ... h6           Headlines
img                 Image
font                Font face and color
blockquote          For longer quotes
a                   Link
div, p              Paragraph
br                  Linefeed

Turns out you cannot use getText(), you can find about this in the documentation.

String resources, Styling with spannables; Styling with annotations. Example - adding a String; String array; Quantity strings (plurals); Format and style. Handle special The full set supported by Android is zero , one , two , few , many , and other . The rules for Then, format the string by calling getString(int, Object) . C++ <iostream> classes, functions, and operators support formatted string I/O. For example, the following code shows how to set cout to format an integer to output in hexadecimal. First, it saves the current state to reset it afterwards, because once format state is passed to cout , it stays that way until changed.


Edit annotation text—ArcGIS Pro, When you edit an annotation feature, you can click and drag the pointer to select all or part of the text string, and use the formatting toolbar to change the font type, style, and size of the text. The Modify Features pane appears. The tool opens in the pane. and select the annotation feature. Text Style Click Annotate tabText panel. In the Styles list, select a style to modify. Under Size, click Annotative. Dimension Style Click Annotate tabDimensions paneldialog box launcher. Find In the Styles list box, select a style and click Modify. On the Fit tab, under Scale for Dimension Features, click Annotative. Multileader Style Click Annotate tabLeaders paneldialog box launcher. Find


Web Annotation Data Model, The media type of this format is defined in Section 3 of the Annotation Protocol If there is only one value, then it MUST be provided as a string. For example, HTML has a specific set of semantics regarding the meaning of how to retrieve the correct representation, add style information, or associate a� Java String format() method is used for formatting the String. There are so many things you can do with this method, for example you can concatenate the strings using this method and at the same time, you can format the output of concatenated string.


AutoCAD� P&ID: Understanding Annotations, A simple annotation carries the Format String “TargetObject. default Annotation Style specified Auto Insert, then the annotation will automatically be This dialog lets you set up the Attribute Definition to display properties of� The formatting string can be any literal string and usually includes a placeholder for the field's value. For example, in the formatting string "Item Value: {0}", the field's value is substituted for the {0} placeholder when the string is displayed in the DynamicField object. The remainder of the formatting string is displayed as literal text.