简体   繁体   中英

Want to increase the size of unicode surrogate pair symbol, e.g., the mathematical i symbol, within a string

I am programming in Kotlin in Android Studio 3.1.4. I have strings that contain the mathematical i symbol. I want to increase the size of the symbol only, not the rest of the text in the string. The mathematical i requires a unicode surrogate pair, which I just recently learned how to display. I fear that this makes things a little more complicated than just having two separate fonts in a single string. The entire string is displayed in a viewText widget.

Note: I am experimenting using a SpannableString with a RelativeSizeSpan as suggested in the answer below. I hope to learn how this applies to unicode surrogate pairs. Thank you.

You can use a SpannableString with a RelativeSizeSpan to resize just the infinity symbol.

For example we can resize the & symbol like this:

var styledString = SpannableString("Hello & welcome")
// 6 and 7 are the start and end index of the & sign
styledString.setSpan(new RelativeSizeSpan(2f), 6, 7, 0)
textView.setText(styledString)

I found this to be a good resource that helped me understand Spannable Strings.

I needed to enlarge the mathematical script i in various locations in a string, ie, not always at the same position in the string. So, here's the code that worked:

    numerator = "Surrogate pair test: "
    var len: Int = numerator.length
    val iScript: String = "\uD835\uDCBE"
    numerator = numerator + iScript + iScript + " End of test."
    var styledString = SpannableString(numerator)
    var sizeI: Float = 4.0f
    styledString.setSpan(RelativeSizeSpan(sizeI), len, len+2, 0)
    txtV_Numerator3.setText(styledString)

Notice that I put two surrogate pairs in succession in the String "numerator" for purposes of the test. I needed "len+2" in the setSpan construct in order to print the first surrogate pair 4 times normal size. Not surprisingly, using "len+4" in the setSpan construct printed both surrogate pairs 4 times normal size. Using "len+1" or "len+3" provided ugly results with large question marks.

I also noticed that changing the size (ielargeness) of the surrogate pair changed the vertical spacing in my user interface. That is, the change in size also changed the vertical spacing between the textView widgets of my activity. Accounting for that will require additional programming.

Conclusion: You can use the above answer to generate a different size surrogate pair as long as you allow for each surrogate pair to be 2 characters in length, and as long as you don't mind changing the vertical spacing in your user interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM