基于iconfont拓展的TextView(支持左右設置Text,設置Shape,設置span等)
項目介紹
- 還在苦于iconfont左右兩邊還需要設置文字只能加兩個TextView?
- 還在苦于需要設置各種大同小異的邊框而new Shape文件嗎?
- 還在苦于設置點擊效果而多種樣式嗎?
- 還在苦于總用LinearLayout包一層?(代碼潔癖患者~)。
一個基于iconfont拓展的支持多種功能的輕量級TextView,減少布局嵌套,減少定義shape文件,基本涵蓋常規需要多個TextView實現的功能。
支持的特性
- 使用簡單
- 鏈式調用
- 支持給iconfont左右設置文字
- 支持xml中直接設置常用shape的所有屬性
- 支持分別設置iconfont,左文字,右文字的顏色(Selector也可以)、字號、樣式(粗體、斜體)
- 支持左邊文字,中間文字,右邊文字都設置iconfont
- 支持文字和iconfont居中
- 支持左右文字設置多個span
- 支持設置iconfont和左右文字的padding
- 豐富的api,所有屬性都支持xml和java調用
源碼地址
歡迎提issues和PR
Download
Step1
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step2
Step 2. Add the dependency
dependencies {
implementation 'com.github.DrownCoder:EasyTextView:v1.12'
}
效果
只有想不到沒有做不到
使用
xml屬性
<com.study.xuan.library.widget.EasyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#494949"
android:textSize="14dp"
app:iconColor="@android:color/holo_red_light"
app:strokeColor="@android:color/holo_red_light"
app:strokeWidth="1dp"
app:textPadding="5dp"
app:textRight="關注"
app:totalRadius="12dp" />
<declare-styleable name="EasyTextView">
//Shape中的Type:RECTANGLE:0(默認),OVAL(1),LINE(2)
<attr name="shapeType" format="integer" />
//四個角的圓角
<attr name="totalRadius" format="dimension"/>
//左上角
<attr name="radiusTopLeft" format="dimension"/>
//左下角
<attr name="radiusBottomLeft" format="dimension"/>
//右上角
<attr name="radiusTopRight" format="dimension"/>
//右下角
<attr name="radiusBottomRight" format="dimension"/>
//邊線的顏色
<attr name="strokeColor" format="color"/>
//邊線的寬度
<attr name="strokeWidth" format="dimension" />
//填充的顏色
<attr name="soildBac" format="color"/>
//中間的iconfont距離左右Text的內邊距
<attr name="textPadding" format="dimension"/>
//左邊的文字
<attr name="textLeft" format="string"/>
//右邊的文字
<attr name="textRight" format="string"/>
//中間的iconfont的color(注意TextColor屬性會覆蓋)
<attr name="iconColor" format="reference|color"/>
//左邊文字的color(注意TextColor屬性會覆蓋)
<attr name="textLeftColor" format="reference|color"/>
//右邊文字的color(注意TextColor屬性會覆蓋)
<attr name="textRightColor" format="reference|color"/>
//左邊文字的大小(iconfont的大小用TextSize即可,不會覆蓋)
<attr name="textLeftSize" format="dimension"/>
//右邊文字的大小(iconfont的大小用TextSize即可,不會覆蓋)
<attr name="textRightSize" format="dimension"/>
//設置左邊文字樣式
<attr name="textLeftStyle">
<enum name="bold" value="1" />
<enum name="italic" value="2" />
</attr>
//設置右邊文字樣式
<attr name="textRightStyle">
<enum name="bold" value="1" />
<enum name="italic" value="2" />
</attr>
//設置中間文字樣式
<attr name="textCenterStyle">
<enum name="bold" value="1" />
<enum name="italic" value="2" />
</attr>
//處理文字高度
<attr name="autoMaxHeight" format="boolean"/>
</declare-styleable>
java Api
//少屬性可以直接設置
etvGet.setTextRight(已關注);
//多屬性可以鏈式調用,減少重復繪制,節省性能,注意最后build()
etvGet.strokeWidth(DensityUtils.dp2px(context, 1))
.strokeColor(Color.parseColor("#ffe849"))
.solid(Color.parseColor("#e8264a"))
.icon(context.getText(R.string.icon_font_check).toString())
.textRight("關注").build();
/**
* 非鏈式調用api
*/
public void setType(int type);
public void setStrokeWidth(int value);
public void setStrokeColor(@ColorInt int color);
public void setSolid(int soild);
public void setIconColor(int color);
public void setTextLeft(CharSequence textLeft);
public void setTextRight(CharSequence textRight);
public void setTextLeftColor(int color);
public void setTextRightColor(int color);
public void setTextLeftSize(float leftSize);
public void setTextRightSize(float rightSize);
public void setIcon(String iconText);
public void clearSpan();
public void addSpanLeft(Object object, int start, int end, int flags);
public void addSpanLeft(List<Object> objects, int start, int end, int flags);
public void addSpanRight(List<Object> objects, int start, int end, int flags);
public void addSpanRight(Object object, int start, int end, int flags);
More
動態設置Shape,由SupperShape實現,可以動態通過java代碼給任何View設置Shape,不需要再寫shape.xml文件,歡迎使用。
本項目僅基于TextView進行封裝,其他View可按照需求進行封裝,原理比較簡單,大家可以查看源碼。
詳細使用說明參考Wiki
版本更新
v1.12
1.修復center_vertical時文字重疊問題
2.新增boolean型autoMaxHeight屬性,當為true時解決以下問題
-修復左右文字大小大于中間文字大小高度不準問題
-修復英文由于基線導致展示不全問題
v1.11
修復右側文字顏色失效bug
v1.1
1.中間的文字支持字符串(原來只支持iconfont)
2.左邊文字,中間文字,右邊文字分別支持粗體和斜體
3.左邊文字,中間文字,右邊文字支持xml中設置iconfont(原來xml中只支持中間文字設置)
4.優化了代碼