TextAppearanceSpan.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/* 2 * Copyright (C) 2008 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 17package android.text.style; 18 19import android.content.Context; 20import android.content.res.ColorStateList; 21import android.content.res.TypedArray; 22import android.graphics.Paint; 23import android.graphics.Typeface; 24import android.text.TextPaint; 25 26/** 27 * Sets the text color, size, style, and typeface to match a TextAppearance 28 * resource. 29 */ 30public class TextAppearanceSpan extends MetricAffectingSpan { 31 private String mTypeface; 32 private int mStyle; 33 private int mTextSize; 34 private ColorStateList mTextColor; 35 private ColorStateList mTextColorLink; 36 37 /** 38 * Uses the specified TextAppearance resource to determine the 39 * text appearance. The <code>appearance</code> should be, for example, 40 * <code>android.R.style.TextAppearance_Small</code>. 41 */ 42 public TextAppearanceSpan(Context context, int appearance) { 43 this(context, appearance, -1); 44 } 45 46 /** 47 * Uses the specified TextAppearance resource to determine the 48 * text appearance, and the specified text color resource 49 * to determine the color. The <code>appearance</code> should be, 50 * for example, <code>android.R.style.TextAppearance_Small</code>, 51 * and the <code>colorList</code> should be, for example, 52 * <code>android.R.styleable.Theme_textColorDim</code>. 53 */ 54 public TextAppearanceSpan(Context context, int appearance, 55 int colorList) { 56 TypedArray a = 57 context.obtainStyledAttributes(appearance, 58 com.android.internal.R.styleable.TextAppearance); 59 60 mTextColor = a.getColorStateList(com.android.internal.R.styleable. 61 TextAppearance_textColor); 62 mTextColorLink = a.getColorStateList(com.android.internal.R.styleable. 63 TextAppearance_textColorLink); 64 mTextSize = a.getDimensionPixelSize(com.android.internal.R.styleable. 65 TextAppearance_textSize, -1); 66 67 mStyle = a.getInt(com.android.internal.R.styleable.TextAppearance_textStyle, 0); 68 int tf = a.getInt(com.android.internal.R.styleable.TextAppearance_typeface, 0); 69 70 switch (tf) { 71 case 1: 72 mTypeface = "sans"; 73 break; 74 75 case 2: 76 mTypeface = "serif"; 77 break; 78 79 case 3: 80 mTypeface = "monospace"; 81 break; 82 } 83 84 a.recycle(); 85 86 if (colorList >= 0) { 87 a = context.obtainStyledAttributes(com.android.internal.R.style.Theme, 88 com.android.internal.R.styleable.Theme); 89 90 mTextColor = a.getColorStateList(colorList); 91 a.recycle(); 92 } 93 } 94 95 /** 96 * Makes text be drawn with the specified typeface, size, style, 97 * and colors. 98 */ 99 public TextAppearanceSpan(String family, int style, int size, 100 ColorStateList color, ColorStateList linkColor) { 101 mTypeface = family; 102 mStyle = style; 103 mTextSize = size; 104 mTextColor = color; 105 mTextColorLink = linkColor; 106 } 107 108 /** 109 * Returns the typeface family specified by this span, or <code>null</code> 110 * if it does not specify one. 111 */ 112 public String getFamily() { 113 return mTypeface; 114 } 115 116 /** 117 * Returns the text color specified by this span, or <code>null</code> 118 * if it does not specify one. 119 */ 120 public ColorStateList getTextColor() { 121 return mTextColor; 122 } 123 124 /** 125 * Returns the link color specified by this span, or <code>null</code> 126 * if it does not specify one. 127 */ 128 public ColorStateList getLinkTextColor() { 129 return mTextColorLink; 130 } 131 132 /** 133 * Returns the text size specified by this span, or <code>-1</code> 134 * if it does not specify one. 135 */ 136 public int getTextSize() { 137 return mTextSize; 138 } 139 140 /** 141 * Returns the text style specified by this span, or <code>0</code> 142 * if it does not specify one. 143 */ 144 public int getTextStyle() { 145 return mStyle; 146 } 147 148 @Override 149 public void updateDrawState(TextPaint ds) { 150 updateMeasureState(ds); 151 152 if (mTextColor != null) { 153 ds.setColor(mTextColor.getColorForState(ds.drawableState, 0)); 154 } 155 156 if (mTextColorLink != null) { 157 ds.linkColor = mTextColor.getColorForState(ds.drawableState, 0); 158 } 159 } 160 161 @Override 162 public void updateMeasureState(TextPaint ds) { 163 if (mTypeface != null || mStyle != 0) { 164 Typeface tf = ds.getTypeface(); 165 int style = 0; 166 167 if (tf != null) { 168 style = tf.getStyle(); 169 } 170 171 style |= mStyle; 172 173 if (mTypeface != null) { 174 tf = Typeface.create(mTypeface, style); 175 } else if (tf == null) { 176 tf = Typeface.defaultFromStyle(style); 177 } else { 178 tf = Typeface.create(tf, style); 179 } 180 181 int fake = style & ~tf.getStyle(); 182 183 if ((fake & Typeface.BOLD) != 0) { 184 ds.setFakeBoldText(true); 185 } 186 187 if ((fake & Typeface.ITALIC) != 0) { 188 ds.setTextSkewX(-0.25f); 189 } 190 191 ds.setTypeface(tf); 192 } 193 194 if (mTextSize > 0) { 195 ds.setTextSize(mTextSize); 196 } 197 } 198} 199