1212import android .text .style .AbsoluteSizeSpan ;
1313import android .text .style .CharacterStyle ;
1414import android .text .style .ForegroundColorSpan ;
15+ import android .text .style .StyleSpan ;
1516import android .util .AttributeSet ;
1617import android .util .TypedValue ;
1718import android .view .Gravity ;
@@ -66,6 +67,9 @@ public class EasyTextView extends TextView {
6667 private float mRightSize ;
6768 private List <SpanContainer > leftContainer ;
6869 private List <SpanContainer > rightContainer ;
70+ private int mTextLeftStyle ;
71+ private int mTextRightStyle ;
72+ private int mTextCenterStyle ;
6973 private TypedValue textValue ;//左右文字支持xml中设置iconFont
7074 //icon的index
7175 private int iconIndex = 0 ;
@@ -136,77 +140,18 @@ private void initIconFont() {
136140 }
137141
138142 }
139-
140- /*
141- * ==============
142- * 设置左边字的颜色
143- * ==============
144- */
145- if (!TextUtils .isEmpty (mTextLeft ) && mLeftColor != null ) {
146- int color = mLeftColor .getColorForState (getDrawableState (), 0 );
147- if (color != mCurLeftColor ) {
148- mCurLeftColor = color ;
149- }
150- int end = mTextPadding == 0 ? iconIndex : iconIndex - 1 ;
151- ForegroundColorSpan foregroundLeftColor = new ForegroundColorSpan (mCurLeftColor );
152- stringBuilder .setSpan (foregroundLeftColor , 0 , end , Spanned
153- .SPAN_EXCLUSIVE_EXCLUSIVE );
154- } else {
155- mCurLeftColor = getCurrentTextColor ();
156- }
157-
158143 /*
159144 * ==============
160- * 设置右边字的颜色
145+ * 设置左边文字样式
161146 * ==============
162147 */
163- if (!TextUtils .isEmpty (mTextRight ) && mRightColor != null ) {
164- int color = mRightColor .getColorForState (getDrawableState (), 0 );
165- if (color != mCurRightColor ) {
166- mCurRightColor = color ;
167- }
168- int start = mTextPadding == 0 ? iconIndex + centerSize : iconIndex + centerSize + 1 ;
169- ForegroundColorSpan foregroundRightColor = new ForegroundColorSpan (mCurRightColor );
170- stringBuilder .setSpan (foregroundRightColor , start , stringBuilder .length ()
171- , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
172- } else {
173- mCurRightColor = getCurrentTextColor ();
174- }
175-
148+ setLeftTextAttr (stringBuilder );
176149 /*
177150 * ==============
178- * 设置左边字的大小
151+ * 设置右边文字样式
179152 * ==============
180153 */
181- if (!TextUtils .isEmpty (mTextLeft ) && mLeftSize != 0 ) {
182- int end = mTextPadding == 0 ? iconIndex : iconIndex - 1 ;
183- CharacterStyle sizeSpan ;
184- final int gravity = getGravity () & Gravity .VERTICAL_GRAVITY_MASK ;
185- if (gravity == Gravity .CENTER_VERTICAL ) {
186- sizeSpan = new EasyVerticalCenterSpan (mLeftSize , mCurLeftColor );
187- } else {
188- sizeSpan = new AbsoluteSizeSpan ((int ) mLeftSize );
189- }
190- stringBuilder .setSpan (sizeSpan , 0 , end , Spanned
191- .SPAN_EXCLUSIVE_EXCLUSIVE );
192- }
193- /*
194- * ==============
195- * 设置右边字的大小
196- * ==============
197- */
198- if (!TextUtils .isEmpty (mTextRight ) && mRightSize != 0 ) {
199- int start = mTextPadding == 0 ? iconIndex + centerSize : iconIndex + centerSize + 1 ;
200- CharacterStyle sizeSpan ;
201- final int gravity = getGravity () & Gravity .VERTICAL_GRAVITY_MASK ;
202- if (gravity == Gravity .CENTER_VERTICAL ) {
203- sizeSpan = new EasyVerticalCenterSpan (mRightSize , mCurRightColor );
204- } else {
205- sizeSpan = new AbsoluteSizeSpan ((int ) mRightSize );
206- }
207- stringBuilder .setSpan (sizeSpan , start , stringBuilder .length (), Spanned
208- .SPAN_EXCLUSIVE_EXCLUSIVE );
209- }
154+ setRightTextAttr (centerSize , stringBuilder );
210155 }
211156 /*
212157 * ==============
@@ -224,6 +169,12 @@ private void initIconFont() {
224169 } else {
225170 mCurIconColor = getCurrentTextColor ();
226171 }
172+ /*
173+ * ==============
174+ * 设置icon的字的样式
175+ * ==============
176+ */
177+ initTextStyle (mTextCenterStyle , stringBuilder , iconIndex , iconIndex + centerSize );
227178 /*
228179 * ==============
229180 * 设置左右Span,记得调用前在**所有方法**前先clearSpan(),不然直接build,上一次的span任然保留着
@@ -256,6 +207,104 @@ private void initIconFont() {
256207 setText (stringBuilder );
257208 }
258209
210+ private void setRightTextAttr (int centerSize , SpannableStringBuilder stringBuilder ) {
211+ if (!TextUtils .isEmpty (mTextRight ) && mRightSize != 0 ) {
212+ int start = mTextPadding == 0 ? iconIndex + centerSize : iconIndex + centerSize + 1 ;
213+ /*
214+ * ==============
215+ * 设置右边字的粗体和斜体
216+ * ==============
217+ */
218+ initTextStyle (mTextRightStyle , stringBuilder , start , stringBuilder .length ());
219+ /*
220+ * ==============
221+ * 设置右边字的颜色
222+ * ==============
223+ */
224+ initTextRightColor (stringBuilder , start );
225+ /*
226+ * ==============
227+ * 设置右边字的大小
228+ * ==============
229+ */
230+ initTextSize (stringBuilder , start , stringBuilder .length (), mRightSize , mCurRightColor );
231+ }
232+ }
233+
234+ private void initTextRightColor (SpannableStringBuilder stringBuilder , int start ) {
235+ if (mRightColor != null ) {
236+ int color = mRightColor .getColorForState (getDrawableState (), 0 );
237+ if (color != mCurRightColor ) {
238+ mCurRightColor = color ;
239+ }
240+ ForegroundColorSpan foregroundRightColor = new ForegroundColorSpan (mCurRightColor );
241+ stringBuilder .setSpan (foregroundRightColor , start , stringBuilder .length ()
242+ , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
243+ } else {
244+ mCurRightColor = getCurrentTextColor ();
245+ }
246+ }
247+
248+ private void setLeftTextAttr (SpannableStringBuilder stringBuilder ) {
249+ if (!TextUtils .isEmpty (mTextLeft )) {
250+ int end = mTextPadding == 0 ? iconIndex : iconIndex - 1 ;
251+ /*
252+ * ==============
253+ * 设置左边字的粗体和斜体
254+ * ==============
255+ */
256+ initTextStyle (mTextLeftStyle , stringBuilder , 0 , end );
257+ /*
258+ * ==============
259+ * 设置左边字的颜色
260+ * ==============
261+ */
262+ initTextLeftColor (stringBuilder , end );
263+ /*
264+ * ==============
265+ * 设置左边字的大小
266+ * ==============
267+ */
268+ initTextSize (stringBuilder , 0 , end , mLeftSize , mCurLeftColor );
269+ }
270+ }
271+
272+ private void initTextSize (SpannableStringBuilder stringBuilder , int start , int end , float textSize , int mCurColor ) {
273+ if (textSize != 0 ) {
274+ CharacterStyle sizeSpan ;
275+ final int gravity = getGravity () & Gravity .VERTICAL_GRAVITY_MASK ;
276+ if (gravity == Gravity .CENTER_VERTICAL ) {
277+ sizeSpan = new EasyVerticalCenterSpan (textSize , mCurColor );
278+ } else {
279+ sizeSpan = new AbsoluteSizeSpan ((int ) textSize );
280+ }
281+ stringBuilder .setSpan (sizeSpan , start , end , Spanned
282+ .SPAN_EXCLUSIVE_EXCLUSIVE );
283+ }
284+ }
285+
286+ private void initTextLeftColor (SpannableStringBuilder stringBuilder , int end ) {
287+ if (mLeftColor != null ) {
288+ int color = mLeftColor .getColorForState (getDrawableState (), 0 );
289+ if (color != mCurLeftColor ) {
290+ mCurLeftColor = color ;
291+ }
292+ ForegroundColorSpan foregroundLeftColor = new ForegroundColorSpan (mCurLeftColor );
293+ stringBuilder .setSpan (foregroundLeftColor , 0 , end , Spanned
294+ .SPAN_EXCLUSIVE_EXCLUSIVE );
295+ } else {
296+ mCurLeftColor = getCurrentTextColor ();
297+ }
298+ }
299+
300+ private void initTextStyle (int textStyle , SpannableStringBuilder stringBuilder , int start , int end ) {
301+ StyleSpan span ;
302+ if (textStyle != Typeface .NORMAL ) {
303+ span = new StyleSpan (textStyle );
304+ stringBuilder .setSpan (span , start , end , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
305+ }
306+ }
307+
259308 private void initShape () {
260309 if (mRadius == -0 && mRadiusTopLeft == 0 && mRadiusTopRight == 0 && mRadiusBottomLeft == 0
261310 && mRadiusBottomRight == 0 && mStrokeColor == -1 && mStrokeWidth == 0 && mSoild ==
@@ -302,7 +351,7 @@ private void initAttr(Context context, AttributeSet attrs) {
302351 if (textValue .type == TypedValue .TYPE_REFERENCE ) {
303352 //文字引用
304353 mTextLeft = mContext .getResources ().getText (textValue .resourceId );
305- }else {
354+ } else {
306355 //纯文字
307356 mTextLeft = textValue .string ;
308357 }
@@ -312,7 +361,7 @@ private void initAttr(Context context, AttributeSet attrs) {
312361 if (textValue .type == TypedValue .TYPE_REFERENCE ) {
313362 //文字引用
314363 mTextRight = mContext .getResources ().getText (textValue .resourceId );
315- }else {
364+ } else {
316365 //纯文字
317366 mTextRight = textValue .string ;
318367 }
@@ -324,6 +373,9 @@ private void initAttr(Context context, AttributeSet attrs) {
324373 mRightColor = array .getColorStateList (R .styleable .EasyTextView_textRightColor );
325374 mLeftSize = array .getDimensionPixelSize (R .styleable .EasyTextView_textLeftSize , 0 );
326375 mRightSize = array .getDimensionPixelSize (R .styleable .EasyTextView_textRightSize , 0 );
376+ mTextLeftStyle = array .getInt (R .styleable .EasyTextView_textLeftStyle , Typeface .NORMAL );
377+ mTextRightStyle = array .getInt (R .styleable .EasyTextView_textRightStyle , Typeface .NORMAL );
378+ mTextCenterStyle = array .getInt (R .styleable .EasyTextView_textCenterStyle , Typeface .NORMAL );
327379 array .recycle ();
328380 }
329381
0 commit comments