Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Small Caps on TextViews, EditTexts, and Buttons in Android

Is there something I can do to make the text look in small caps/capital? As described here: http://en.wikipedia.org/wiki/Small_caps. I used a converter but some characters are missing.

like image 701
naz Avatar asked Apr 12 '13 04:04

naz


People also ask

How do I do small caps on Android?

Just double-tap the shift key and a blue indicator will light up. You'll know you're in all caps because the letter keys will change to uppercase. When you're ready to switch back to lowercase, just tap the shift key once again.

How do you make the first letter capital and other small in Android?

If you are using an Android phone and Gboard, you can capitalize the first letter of any word with three touches. First, double-tap the word in question to highlight the word, then tap the shift button (the up arrow) on the keyboard to capitalize the first letter. Done!

How do you change text to all caps in Android?

You can add the android:textAllCaps="true" property to your xml file in the EditText. This will enforce the softinput keyboard to appear in all caps mode. The value you enter will appear in Uppercase.

What is text all caps in Android Studio?

This filter will capitalize all the lowercase and titlecase letters that are added through edits. (Note that if there are no lowercase or titlecase letters in the input, the text would not be transformed, even if the result of capitalization of the string is different from the string.)


2 Answers

EDIT 2015-08-02: As of API 21 (Lollipop) you can simply add:

android:fontFeatureSettings="smcp"

to your TextView declaration in XML, or at runtime, invoke:

textView.setFontFeatureSettings("smcp");

Of course, this only works for API 21 and up, so you'd still have to handle the old solution manually until you are only supporting Lollipop and above.


Being a bit of a typography geek at heart, this seemed like a really good question. I got to learn some more about Unicode today, as well as an answer for your question. :)

First, you'll need to have a font that includes "actual" small-caps characters. I'm assuming you know that since you're asking, but typically most professional fonts include these. Unfortunately most professional fonts are not licensed for distribution, so you may not be able to use them in your application. Anyway, in the event that you do find one (I used Chaparral Pro as an example here), this is how you can get small caps.

From this answer I found that the small caps characters (for A-Z) are located starting at Unicode-UF761. So I built a mapping of these characters:

private static char[] smallCaps = new char[]
{
        '\uf761', //A
        '\uf762',
        '\uf763',
        '\uf764',
        '\uf765',
        '\uf766',
        '\uf767',
        '\uf768',
        '\uf769',
        '\uf76A',
        '\uf76B',
        '\uf76C',
        '\uf76D',
        '\uf76E',
        '\uf76F',
        '\uf770',
        '\uf771',
        '\uf772',
        '\uf773',
        '\uf774',
        '\uf775',
        '\uf776',
        '\uf777',
        '\uf778',
        '\uf779',
        '\uf77A'   //Z
};

Then added a helper method to convert an input string to one whose lowercase letters have been replaced by their Small Caps equivalents:

private static String getSmallCapsString (String input) {
    char[] chars = input.toCharArray();
    for(int i = 0; i < chars.length; i++) {
        if(chars[i] >= 'a' && chars[i] <= 'z') {
            chars[i] = smallCaps[chars[i] - 'a'];
        }
    }
    return String.valueOf(chars);
}

Then just use that anywhere:

String regularCase = "The quick brown fox jumps over the lazy dog.";
textView.setText(getSmallCapsString(regularCase));

For which I got the following result:

Example of Small Caps

like image 133
Kevin Coppock Avatar answered Sep 29 '22 20:09

Kevin Coppock


Apologies for dragging up a very old question.

I liked @kcoppock's approach to this, but unfortunately the font I'm using is missing the small-cap characters. I suspect many others will find themselves in this situation.

That inspired me to write a little util method that will take a mixed-case string (e.g. Small Caps) and create a formatted spannable string that looks like Sᴍᴀʟʟ Cᴀᴘs but only uses the standard A-Z characters.

  • It works with any font that has the A-Z characters - nothing special required
  • It is easily useable in a TextView (or any other text-based view, for that matter)
  • It doesn't require any HTML
  • It doesn't require any editing of your original strings

I've posed the code here: https://gist.github.com/markormesher/3e912622d339af01d24e

like image 27
Mark Ormesher Avatar answered Sep 29 '22 22:09

Mark Ormesher