Skip to content

Commit 34e917c

Browse files
author
wanggang
committed
更新到 v2.1:
1. SpecialTextUnit增加useTextItalic(斜体)及setTextStyle方法 by @JinJieGu 2. SpecialClickableUnit增加tag字段方便透传 by @JinJieGu 3. SpecialLabelUnit增加useTextItalic(斜体)方法
1 parent be05c1f commit 34e917c

File tree

12 files changed

+119
-74
lines changed

12 files changed

+119
-74
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A easy-to-use and powerful Spannable library
1010
![](https://raw.githubusercontent.com/iwgang/SimplifySpan/master/screenshot/s3.gif)
1111

1212
### gradle
13-
compile 'com.github.iwgang:simplifyspan:2.0'
13+
compile 'com.github.iwgang:simplifyspan:2.1'
1414

1515
### Support Units
1616
* SpecialTextUnit
@@ -19,10 +19,12 @@ A easy-to-use and powerful Spannable library
1919
* **convertMode** (setConvertMode(convertMode | int)) SpecialConvertMode.ONLY_FIRST, SpecialConvertMode.ALL, SpecialConvertMode.ONLY_LAST
2020
* **textColor** (Constructor Or setTextColor(int color))
2121
* **textSize** (Constructor Or setTextSize(float size)) ps
22-
* **textBackgroundColor** (setTextBackgroundColor(int color))
23-
* **isShowUnderline** (showUnderline())
24-
* **isShowStrikeThrough** (showStrikeThrough())
25-
* **isTextBold** (useTextBold())
22+
* **backgroundColor** (setTextBackgroundColor(int color))
23+
* **showUnderline** (showUnderline())
24+
* **showStrikeThrough** (showStrikeThrough())
25+
* **textBold** (useTextBold())
26+
* **textItalic** (useTextItalic())
27+
* **textStyle** (setTextStyle(Typeface style))
2628
* **clickableUnit** (setClickableUnit(SpecialClickableUnit))
2729
* SpecialLabelUnit
2830
* **text** (Constructor | String)
@@ -38,7 +40,8 @@ A easy-to-use and powerful Spannable library
3840
* **paddingLeft** (setPaddingLeft(int padding)) px
3941
* **paddingRight** (setPaddingRight(int padding)) px
4042
* **labelBgBorderColor** And **borderSize** (showBorder(int labelBgBorderColor, float borderSize | px))
41-
* **isTextBold** (useTextBold())
43+
* **textBold** (useTextBold())
44+
* **textItalic** (useTextItalic())
4245
* **clickable** See SimplifySpanBuild.appendMultiClickable() Or SimplifySpanBuild.appendMultiClickableToFirst()
4346
* SpecialImageUnit
4447
* **text** (Constructor | String)

app/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 26
5-
buildToolsVersion "26.0.2"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
66

77
defaultConfig {
88
applicationId "cn.iwgang.simplifyspandemo"
9-
minSdkVersion 14
10-
targetSdkVersion 26
9+
minSdkVersion 10
10+
targetSdkVersion 25
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -21,6 +21,6 @@ android {
2121

2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
24-
compile 'com.android.support:appcompat-v7:26.+'
24+
compile 'com.android.support:appcompat-v7:25.4.0'
2525
compile project(':library')
2626
}

app/src/main/java/cn/iwgang/simplifyspandemo/MainActivity.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
import android.graphics.Typeface;
66
import android.os.Bundle;
77
import android.support.v7.app.AppCompatActivity;
8-
import android.text.Spanned;
9-
import android.text.style.ClickableSpan;
10-
import android.util.Log;
118
import android.widget.TextView;
129
import android.widget.Toast;
1310

1411
import cn.iwgang.simplifyspan.SimplifySpanBuild;
1512
import cn.iwgang.simplifyspan.customspan.CustomClickableSpan;
1613
import cn.iwgang.simplifyspan.other.OnClickableSpanListener;
14+
import cn.iwgang.simplifyspan.other.SpecialConvertMode;
1715
import cn.iwgang.simplifyspan.other.SpecialGravity;
1816
import cn.iwgang.simplifyspan.unit.SpecialClickableUnit;
1917
import cn.iwgang.simplifyspan.unit.SpecialImageUnit;
@@ -31,7 +29,6 @@ protected void onCreate(Bundle savedInstanceState) {
3129
TextView tvText11 = (TextView) findViewById(R.id.tv_text11);
3230
TextView tvText2 = (TextView) findViewById(R.id.tv_text2);
3331
TextView tvText21 = (TextView) findViewById(R.id.tv_text21);
34-
TextView tvText22 = (TextView) findViewById(R.id.tv_text22);
3532
TextView tvText3 = (TextView) findViewById(R.id.tv_text3);
3633
TextView tvText4 = (TextView) findViewById(R.id.tv_text4);
3734
TextView tvText41 = (TextView) findViewById(R.id.tv_text41);
@@ -76,13 +73,15 @@ protected void onCreate(Bundle savedInstanceState) {
7673
.append(new SpecialLabelUnit("原创", Color.RED, sp2px(10), Color.TRANSPARENT).showBorder(Color.BLACK, 2).setPadding(5).setPaddingLeft(10).setPaddingRight(10).setGravity(SpecialGravity.CENTER));
7774
tvText11.setText(simplifySpanBuild11.build());
7875

76+
SimplifySpanBuild simplifySpanBuild2 = new SimplifySpanBuild("替换所有张字的颜色及字体大小并加粗,张歆艺、张馨予、张嘉倪、张涵予、张含韵、张韶涵、张嘉译、张佳宁、大张伟", new SpecialTextUnit("张").useTextBold().setTextSize(20).setTextColor(0xFFFFA500).setConvertMode(SpecialConvertMode.ALL));
77+
tvText2.setText(simplifySpanBuild2.build());
7978

8079
SimplifySpanBuild simplifySpanBuild21 = new SimplifySpanBuild();
8180
simplifySpanBuild21
8281
.append(new SpecialTextUnit("正常字体").setTextSize(18).setTextStyle(Typeface.NORMAL))
83-
.append(new SpecialTextUnit("粗体").setTextSize(18).setTextStyle(Typeface.BOLD))
84-
.append(new SpecialTextUnit("斜体").setTextSize(18).setTextStyle(Typeface.ITALIC))
85-
.append(new SpecialTextUnit("粗斜体").setTextSize(18).setTextStyle(Typeface.BOLD_ITALIC))
82+
.append(new SpecialTextUnit("粗体").setTextSize(18).useTextBold())
83+
.append(new SpecialTextUnit("斜体").setTextSize(18).useTextItalic())
84+
.append(new SpecialTextUnit("粗斜体").setTextSize(18).useTextBold().useTextItalic())
8685
.append(new SpecialTextUnit("黑体").setTextSize(18).setTextStyle(Typeface.DEFAULT_BOLD.getStyle()))
8786
.append(new SpecialTextUnit("等宽1字体").setTextSize(18).setTextStyle(Typeface.MONOSPACE.getStyle()))
8887
.append(new SpecialTextUnit("Sans Serif字体").setTextSize(18).setTextStyle(Typeface.SANS_SERIF.getStyle()))
@@ -95,15 +94,6 @@ protected void onCreate(Bundle savedInstanceState) {
9594
.append(new SpecialTextUnit("底部").setTextSize(12).setTextColor(0xFF8B658B));
9695
tvText21.setText(simplifySpanBuild21.build());
9796

98-
99-
SimplifySpanBuild simplifySpanBuild22 = new SimplifySpanBuild("正常底部正常居中正常顶部正常",
100-
new SpecialTextUnit("底部").setTextSize(30).setTextColor(Color.BLUE),
101-
new SpecialTextUnit("居中").setTextSize(30).setGravity(tvText22, SpecialGravity.CENTER).setTextColor(0xFFB03060),
102-
new SpecialTextUnit("顶部").setTextSize(30).setGravity(tvText22, SpecialGravity.TOP).setTextColor(0xFFB0C4DE)
103-
104-
);
105-
tvText22.setText(simplifySpanBuild22.build());
106-
10797
SimplifySpanBuild simplifySpanBuild3 = new SimplifySpanBuild();
10898
simplifySpanBuild3.append(new SpecialImageUnit(this, BitmapFactory.decodeResource(getResources(), R.drawable.ic_bulletin), 50, 50).setGravity(SpecialGravity.CENTER))
10999
.append("正常")
@@ -132,7 +122,7 @@ protected void onCreate(Bundle savedInstanceState) {
132122
simplifySpanBuild4.append("完整文章见 ");
133123
simplifySpanBuild4.appendMultiClickable(new SpecialClickableUnit(tvText4, this).setNormalTextColor(linkNorTextColor).setPressBgColor(linkPressBgColor),
134124
new SpecialImageUnit(this, BitmapFactory.decodeResource(getResources(), R.drawable.timeline_card_small_article), 30, 30).setGravity(SpecialGravity.CENTER),
135-
new SpecialTextUnit("LOL超强攻略,不见绝对后悔 ").showUnderline());
125+
new SpecialTextUnit("LOL超强攻略,不看绝对后悔 ").showUnderline());
136126
simplifySpanBuild4.append(" 更多好玩的内容请点击 ");
137127
simplifySpanBuild4.appendMultiClickable(new SpecialClickableUnit(tvText4, this).setNormalTextColor(linkNorTextColor).setPressBgColor(linkPressBgColor),
138128
new SpecialImageUnit(this, BitmapFactory.decodeResource(getResources(), R.drawable.timeline_card_small_web), 42, 42).setGravity(SpecialGravity.CENTER),

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,6 @@
7272
android:paddingLeft="10dp"
7373
android:textSize="30sp" />
7474

75-
<TextView
76-
android:id="@+id/tv_text22"
77-
android:layout_width="wrap_content"
78-
android:layout_height="wrap_content"
79-
android:layout_marginLeft="10dp"
80-
android:layout_marginRight="12dp"
81-
android:layout_marginTop="10dp"
82-
android:textSize="12sp" />
83-
8475
<TextView
8576
android:layout_width="match_parent"
8677
android:layout_height="30dp"

build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22

33
buildscript {
44
repositories {
5+
google()
56
jcenter()
67
}
78
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.3.0'
9-
9+
classpath 'com.android.tools.build:gradle:3.0.0-beta6'
1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files
1212
}
1313
}
1414

1515
allprojects {
1616
repositories {
17+
google()
1718
jcenter()
1819
}
1920
}

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

library/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 26
5-
buildToolsVersion "26.0.2"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
66

77
defaultConfig {
8-
minSdkVersion 14
9-
targetSdkVersion 26
8+
minSdkVersion 10
9+
targetSdkVersion 25
1010
versionCode 1
1111
versionName "1.0"
1212
}

library/src/main/java/cn/iwgang/simplifyspan/SimplifySpanBuild.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,13 @@ public SpannableStringBuilder build() {
349349
spannableStringBuilder.setSpan(new StyleSpan(Typeface.BOLD), startPos, startPos + specialTextLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
350350
}
351351

352-
//Set Text Style
353-
if (specialTextUnit.getTextStyle() != Typeface.NORMAL){
352+
// Set Text Italic
353+
if (specialTextUnit.isTextItalic()) {
354+
spannableStringBuilder.setSpan(new StyleSpan(Typeface.ITALIC), startPos, startPos + specialTextLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
355+
}
356+
357+
// Set Text Style
358+
if (specialTextUnit.getTextStyle() != Typeface.NORMAL) {
354359
spannableStringBuilder.setSpan(new StyleSpan(specialTextUnit.getTextStyle()), startPos, startPos + specialTextLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
355360
}
356361

library/src/main/java/cn/iwgang/simplifyspan/customspan/CustomClickableSpan.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.text.Spanned;
55
import android.text.TextPaint;
66
import android.text.style.ClickableSpan;
7-
import android.util.Log;
87
import android.view.View;
98
import android.widget.TextView;
109

@@ -28,13 +27,13 @@ public class CustomClickableSpan extends ClickableSpan {
2827
private int mTextColorPre;
2928
private int mBgColorNor;
3029
private int mBgColorPre;
31-
private Object tag;
32-
private String clickText;
33-
private int startSpanIndex;
34-
private int endSpanIndex;
30+
private Object mTag;
31+
private String mClickText;
32+
private int mStartSpanIndex;
33+
private int mEndSpanIndex;
3534

3635
public CustomClickableSpan(SpecialClickableUnit specialClickableUnit) {
37-
setTag(specialClickableUnit.getTag());
36+
mTag = specialClickableUnit.getTag();
3837
mTextColorNor = specialClickableUnit.getNormalTextColor();
3938
mTextColorPre = specialClickableUnit.getPressTextColor();
4039
mBgColorNor = specialClickableUnit.getNormalBgColor();
@@ -47,11 +46,11 @@ public CustomClickableSpan(SpecialClickableUnit specialClickableUnit) {
4746
@Override
4847
public void onClick(View widget) {
4948
if (null != mOnClickableSpanListener) {
50-
TextView tv = (TextView)widget;
51-
Spanned spanned = (Spanned)tv.getText();
52-
startSpanIndex = spanned.getSpanStart(this);
53-
endSpanIndex = spanned.getSpanEnd(this);
54-
clickText = spanned.subSequence(startSpanIndex, endSpanIndex).toString();
49+
TextView tv = (TextView) widget;
50+
Spanned spanned = (Spanned) tv.getText();
51+
mStartSpanIndex = spanned.getSpanStart(this);
52+
mEndSpanIndex = spanned.getSpanEnd(this);
53+
mClickText = spanned.subSequence(mStartSpanIndex, mEndSpanIndex).toString();
5554
mOnClickableSpanListener.onClick(tv, this);
5655
}
5756
}
@@ -91,27 +90,32 @@ public void updateDrawState(TextPaint ds) {
9190
}
9291
}
9392

93+
/**
94+
* get tag
95+
*/
9496
public Object getTag() {
95-
return tag;
97+
return mTag;
9698
}
9799

98-
public void setTag(Object tag) {
99-
this.tag = tag;
100+
/**
101+
* get current click text
102+
*/
103+
public String getClickText() {
104+
return mClickText;
100105
}
101106

102-
//get current click text
103-
public String getClickText(){
104-
return clickText;
105-
}
106-
107-
//get current click text span start index in total
107+
/**
108+
* get current click text span start index in total
109+
*/
108110
public int getStartSpanIndex() {
109-
return startSpanIndex;
111+
return mStartSpanIndex;
110112
}
111113

112-
//get current click text span start index in end
114+
/**
115+
* get current click text span start index in end
116+
*/
113117
public int getEndSpanIndex() {
114-
return endSpanIndex;
118+
return mEndSpanIndex;
115119
}
116120

117121
}

library/src/main/java/cn/iwgang/simplifyspan/customspan/CustomLabelSpan.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.graphics.Paint;
66
import android.graphics.Rect;
77
import android.graphics.RectF;
8+
import android.graphics.Typeface;
89
import android.media.ThumbnailUtils;
910
import android.text.style.ReplacementSpan;
1011

@@ -64,7 +65,8 @@ public CustomLabelSpan(String normalSizeText, SpecialLabelUnit specialLabelUnit)
6465
}
6566

6667
private void initPadding() {
67-
if (mSpecialLabelUnit.getLabelBgHeight() > 0 || mSpecialLabelUnit.getLabelBgWidth() > 0) return ;
68+
if (mSpecialLabelUnit.getLabelBgHeight() > 0 || mSpecialLabelUnit.getLabelBgWidth() > 0)
69+
return;
6870

6971
int allPadding = mSpecialLabelUnit.getPadding();
7072

@@ -85,7 +87,8 @@ private void initPadding() {
8587
mPaddingRight = allPadding;
8688
}
8789

88-
if (mPaddingTop > 0 || mPaddingBottom > 0 || mPaddingLeft > 0 || mPaddingRight > 0) isLabelBgCenter = false;
90+
if (mPaddingTop > 0 || mPaddingBottom > 0 || mPaddingLeft > 0 || mPaddingRight > 0)
91+
isLabelBgCenter = false;
8992
}
9093

9194
@Override
@@ -203,7 +206,13 @@ public void draw(Canvas canvas, CharSequence text, int start, int end, float x,
203206
} else {
204207
newTextX = x + mPaddingLeft;
205208
}
206-
if (mSpecialLabelUnit.isTextBold()) paint.setFakeBoldText(true);
209+
210+
if (mSpecialLabelUnit.isTextBold())
211+
paint.setFakeBoldText(true);
212+
213+
if (mSpecialLabelUnit.isTextItalic())
214+
paint.setTypeface(Typeface.create((String) null, Typeface.ITALIC));
215+
207216
canvas.drawText(mSpecialText, newTextX, newTextY, paint);
208217
}
209218

0 commit comments

Comments
 (0)