Home | History | Annotate | Download | only in style
      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 lower.
     27  * <p>
     28  * The span can be used like this:
     29  * <pre>{@code
     30  *  SpannableString string = new SpannableString("- C8H10N4O2\n");
     31  *string.setSpan(new SubscriptSpan(), 4, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
     32  *string.setSpan(new SubscriptSpan(), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
     33  *string.setSpan(new SubscriptSpan(), 9, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
     34  *string.setSpan(new SubscriptSpan(), 11, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre>
     35  * <img src="{@docRoot}reference/android/images/text/style/subscriptspan.png" />
     36  * <figcaption>Text with <code>SubscriptSpan</code>.</figcaption>
     37  * Note: Since the span affects the position of the text, if the text is on the last line of a
     38  * TextView, it may appear cut.
     39  */
     40 public class SubscriptSpan extends MetricAffectingSpan implements ParcelableSpan {
     41 
     42     /**
     43      * Creates a {@link SubscriptSpan}.
     44      */
     45     public SubscriptSpan() {
     46     }
     47 
     48     /**
     49      * Creates a {@link SubscriptSpan} from a parcel.
     50      */
     51     public SubscriptSpan(@NonNull Parcel src) {
     52     }
     53 
     54     @Override
     55     public int getSpanTypeId() {
     56         return getSpanTypeIdInternal();
     57     }
     58 
     59     /** @hide */
     60     @Override
     61     public int getSpanTypeIdInternal() {
     62         return TextUtils.SUBSCRIPT_SPAN;
     63     }
     64 
     65     @Override
     66     public int describeContents() {
     67         return 0;
     68     }
     69 
     70     @Override
     71     public void writeToParcel(Parcel dest, int flags) {
     72         writeToParcelInternal(dest, flags);
     73     }
     74 
     75     /** @hide */
     76     @Override
     77     public void writeToParcelInternal(Parcel dest, int flags) {
     78     }
     79 
     80     @Override
     81     public void updateDrawState(@NonNull TextPaint textPaint) {
     82         textPaint.baselineShift -= (int) (textPaint.ascent() / 2);
     83     }
     84 
     85     @Override
     86     public void updateMeasureState(@NonNull TextPaint textPaint) {
     87         textPaint.baselineShift -= (int) (textPaint.ascent() / 2);
     88     }
     89 }
     90