Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TextAppearanceSpan

i can make smaller last 2 number but i want them to show top as shown below picture.

TextView TV = (TextView)findViewById(R.id.txt);

Spannable WordtoSpan = new SpannableString("13.500,27");        

WordtoSpan.setSpan( new TextAppearanceSpan(null, 0, 12, null, null), WordtoSpan.length()-2, WordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
TV.setText(WordtoSpan);

Thanks talha

enter image description here

like image 244
Talha Avatar asked May 17 '13 12:05

Talha


1 Answers

Edit:

You can use SuperscriptSpan()

Spannable WordtoSpan = new SpannableString("13.500,27");        
WordtoSpan.setSpan( new TextAppearanceSpan(null, 0, 17, null, null), WordtoSpan.length()-2, WordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
WordtoSpan.setSpan( new SuperscriptSpan(), WordtoSpan.length()-2, WordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  

Increase font size to see the effect

enter image description here


Try this I don't find any other way, but to append the superscript text to original spannalbe string

Apply Spans what ever you want for the SpannableStringBuilder

  SpannableStringBuilder builder=new SpannableStringBuilder("13.500,");
  builder.append(Html.fromHtml("X<sup>27</sup>"))

Edit 2

If you create custom span like below, it align supertext to top absolutely. But if you use default SuperscriptSpan ,it couldnt make the text alignt to top.

public class SuperscriptSpan2 extends MetricAffectingSpan implements
            ParcelableSpan {
        public SuperscriptSpan2() {
        }

    public int getSpanTypeId() {
        return TextUtils.CAP_MODE_CHARACTERS;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.baselineShift += (int) (tp.ascent() / 1.25);
    }

    @Override
    public void updateMeasureState(TextPaint tp) {
        tp.baselineShift += (int) (tp.ascent() / 1.25);
    }
}
like image 63
Pragnani Avatar answered Oct 25 '22 18:10

Pragnani