44import android .content .res .ColorStateList ;
55import android .content .res .TypedArray ;
66import android .graphics .Typeface ;
7+ import android .support .annotation .ColorInt ;
78import android .support .annotation .Nullable ;
89import android .text .SpannableStringBuilder ;
910import android .text .Spanned ;
3940public class EasyTextView extends TextView {
4041 private static final String EMPTY_SPACE = "\u3000 " ;
4142 private Context mContext ;
43+ private int type = RECTANGLE ;
4244 private float mRadius ;
4345 private float mRadiusTopLeft , mRadiusTopRight , mRadiusBottomLeft , mRadiusBottomRight ;
4446 private int mStrokeColor ;
@@ -214,19 +216,31 @@ private void initIconFont() {
214216 } else {
215217 mCurIconColor = getCurrentTextColor ();
216218 }
217-
219+ /*
220+ * ==============
221+ * 设置左右Span,记得调用前在**所有方法**前先clearSpan(),不然直接build,上一次的span任然保留着
222+ * ==============
223+ */
218224 if (leftContainer != null ) {
219225 for (SpanContainer container : leftContainer ) {
220226 for (Object o : container .spans ) {
221- stringBuilder .setSpan (o , container .start , container .end , container .flag );
227+ try {
228+ stringBuilder .setSpan (o , container .start , container .end , container .flag );
229+ } catch (Exception e ) {
230+ //please check invoke clearSpan() method first
231+ }
222232 }
223233 }
224234 }
225235 if (rightContainer != null ) {
226236 int start = mTextPadding == 0 ? iconIndex + 1 : iconIndex + 2 ;
227237 for (SpanContainer container : rightContainer ) {
228238 for (Object o : container .spans ) {
229- stringBuilder .setSpan (o , start + container .start , start + container .end , container .flag );
239+ try {
240+ stringBuilder .setSpan (o , start + container .start , start + container .end , container .flag );
241+ } catch (Exception e ) {
242+ //please check invoke clearSpan() method first
243+ }
230244 }
231245 }
232246 }
@@ -246,10 +260,10 @@ private void initShape() {
246260
247261 private void setShape () {
248262 if (mRadius != 0 ) {
249- ShapeBuilder .create ().Type (RECTANGLE ).Radius (mRadius ).Soild (mSoild ).Stroke
263+ ShapeBuilder .create ().Type (type ).Radius (mRadius ).Soild (mSoild ).Stroke
250264 (mStrokeWidth , mStrokeColor ).build (this );
251265 } else {
252- ShapeBuilder .create ().Type (RECTANGLE ).Radius (mRadiusTopLeft ,
266+ ShapeBuilder .create ().Type (type ).Radius (mRadiusTopLeft ,
253267 mRadiusTopRight , mRadiusBottomLeft , mRadiusBottomRight ).Soild (mSoild ).Stroke
254268 (mStrokeWidth , mStrokeColor ).build (this );
255269 }
@@ -262,6 +276,7 @@ private void clearText() {
262276
263277 private void initAttr (Context context , AttributeSet attrs ) {
264278 TypedArray array = context .obtainStyledAttributes (attrs , R .styleable .EasyTextView );
279+ type = array .getInteger (R .styleable .EasyTextView_shapeType , 0 );
265280 mRadius = array .getDimensionPixelOffset (R .styleable .EasyTextView_totalRadius , 0 );
266281 mRadiusTopLeft = array .getDimensionPixelSize (R .styleable .EasyTextView_radiusTopLeft , 0 );
267282 mRadiusTopRight = array .getDimensionPixelSize (R .styleable .EasyTextView_radiusTopRight , 0 );
@@ -294,10 +309,34 @@ protected void drawableStateChanged() {
294309 super .drawableStateChanged ();
295310 }
296311
312+ /**
313+ * 设置Shape Type
314+ */
315+ public void setType (int type ) {
316+ this .type = type ;
317+ setShape ();
318+ }
319+
320+ /**
321+ * 设置边线的宽度
322+ */
323+ public void setStrokeWidth (int value ) {
324+ this .mStrokeWidth = value ;
325+ setShape ();
326+ }
327+
328+ /**
329+ * 设置边线的颜色
330+ */
331+ public void setStrokeColor (@ ColorInt int color ) {
332+ this .mStrokeColor = color ;
333+ setShape ();
334+ }
335+
297336 /**
298337 * 设置shape背景颜色
299338 */
300- public void setBackgroundSold (int soild ) {
339+ public void setSolid (int soild ) {
301340 this .mSoild = soild ;
302341 setShape ();
303342 }
@@ -366,6 +405,19 @@ public void setIcon(String iconText) {
366405 build ();
367406 }
368407
408+
409+ /**
410+ * span之前需要首先clear
411+ */
412+ public void clearSpan () {
413+ if (leftContainer != null ) {
414+ leftContainer .clear ();
415+ }
416+ if (rightContainer != null ) {
417+ rightContainer .clear ();
418+ }
419+ }
420+
369421 /**
370422 * 设置左边文字为多个span
371423 */
@@ -399,6 +451,39 @@ public void addSpanRight(Object object, int start, int end, int flags) {
399451 }
400452 //=================================链式调用##需要最后调用build()==================================
401453
454+ /**
455+ * 设置Shape type
456+ */
457+ public EasyTextView type (int type ) {
458+ this .type = type ;
459+ return this ;
460+ }
461+
462+ /**
463+ * 设置边线的宽度
464+ */
465+ public EasyTextView strokeWidth (int width ) {
466+ this .mStrokeWidth = width ;
467+ return this ;
468+ }
469+
470+ /**
471+ * 设置边线的宽度
472+ */
473+ public EasyTextView strokeColor (@ ColorInt int color ) {
474+ this .mStrokeColor = color ;
475+ return this ;
476+ }
477+
478+ /**
479+ * 设置填充的颜色
480+ */
481+ public EasyTextView solid (@ ColorInt int color ) {
482+ this .mSoild = color ;
483+ return this ;
484+ }
485+
486+
402487 /**
403488 * 设置icon颜色
404489 */
@@ -512,7 +597,8 @@ public EasyTextView spanLeft(Object object, int start, int end, int flags) {
512597 */
513598 public EasyTextView build () {
514599 clearText ();
515- initIconFont ();
600+ //initIconFont();
601+ init ();
516602 return this ;
517603 }
518604}
0 commit comments