1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.text.style; 18 19 import android.annotation.NonNull; 20 import android.os.Parcel; 21 import android.text.ParcelableSpan; 22 import android.text.TextPaint; 23 import android.text.TextUtils; 24 25 /** 26 * The span that moves the position of the text baseline higher. 27 * <p> 28 * The span can be used like this: 29 * <pre>{@code 30 * SpannableString string = new SpannableString("1st example"); 31 *string.setSpan(new SuperscriptSpan(), 1, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre> 32 * <img src="{@docRoot}reference/android/images/text/style/superscriptspan.png" /> 33 * <figcaption>Text with <code>SuperscriptSpan</code>.</figcaption> 34 * Note: Since the span affects the position of the text, if the text is on the first line of a 35 * TextView, it may appear cut. This can be avoided by decreasing the text size with an {@link 36 * AbsoluteSizeSpan} 37 */ 38 public class SuperscriptSpan extends MetricAffectingSpan implements ParcelableSpan { 39 /** 40 * Creates a {@link SuperscriptSpan}. 41 */ SuperscriptSpan()42 public SuperscriptSpan() { 43 } 44 45 /** 46 * Creates a {@link SuperscriptSpan} from a parcel. 47 */ SuperscriptSpan(@onNull Parcel src)48 public SuperscriptSpan(@NonNull Parcel src) { 49 } 50 51 @Override getSpanTypeId()52 public int getSpanTypeId() { 53 return getSpanTypeIdInternal(); 54 } 55 56 /** @hide */ 57 @Override getSpanTypeIdInternal()58 public int getSpanTypeIdInternal() { 59 return TextUtils.SUPERSCRIPT_SPAN; 60 } 61 62 @Override describeContents()63 public int describeContents() { 64 return 0; 65 } 66 67 @Override writeToParcel(@onNull Parcel dest, int flags)68 public void writeToParcel(@NonNull Parcel dest, int flags) { 69 writeToParcelInternal(dest, flags); 70 } 71 72 /** @hide */ 73 @Override writeToParcelInternal(@onNull Parcel dest, int flags)74 public void writeToParcelInternal(@NonNull Parcel dest, int flags) { 75 } 76 77 @Override updateDrawState(@onNull TextPaint textPaint)78 public void updateDrawState(@NonNull TextPaint textPaint) { 79 textPaint.baselineShift += (int) (textPaint.ascent() / 2); 80 } 81 82 @Override updateMeasureState(@onNull TextPaint textPaint)83 public void updateMeasureState(@NonNull TextPaint textPaint) { 84 textPaint.baselineShift += (int) (textPaint.ascent() / 2); 85 } 86 87 @Override toString()88 public String toString() { 89 return "SuperscriptSpan{}"; 90 } 91 } 92