Skip to content

Commit

Permalink
更新到 v2.1:
Browse files Browse the repository at this point in the history
1. SpecialTextUnit增加useTextItalic(斜体)及setTextStyle方法 by @JinJieGu
2. SpecialClickableUnit增加tag字段方便透传 by @JinJieGu
3. SpecialLabelUnit增加useTextItalic(斜体)方法
  • Loading branch information
wanggang committed Jan 22, 2018
1 parent be05c1f commit 34e917c
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 74 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A easy-to-use and powerful Spannable library
![](https://raw.githubusercontent.com/iwgang/SimplifySpan/master/screenshot/s3.gif)

### gradle
compile 'com.github.iwgang:simplifyspan:2.0'
compile 'com.github.iwgang:simplifyspan:2.1'

### Support Units
* SpecialTextUnit
Expand All @@ -19,10 +19,12 @@ A easy-to-use and powerful Spannable library
* **convertMode** (setConvertMode(convertMode | int)) SpecialConvertMode.ONLY_FIRST, SpecialConvertMode.ALL, SpecialConvertMode.ONLY_LAST
* **textColor** (Constructor Or setTextColor(int color))
* **textSize** (Constructor Or setTextSize(float size)) ps
* **textBackgroundColor** (setTextBackgroundColor(int color))
* **isShowUnderline** (showUnderline())
* **isShowStrikeThrough** (showStrikeThrough())
* **isTextBold** (useTextBold())
* **backgroundColor** (setTextBackgroundColor(int color))
* **showUnderline** (showUnderline())
* **showStrikeThrough** (showStrikeThrough())
* **textBold** (useTextBold())
* **textItalic** (useTextItalic())
* **textStyle** (setTextStyle(Typeface style))
* **clickableUnit** (setClickableUnit(SpecialClickableUnit))
* SpecialLabelUnit
* **text** (Constructor | String)
Expand All @@ -38,7 +40,8 @@ A easy-to-use and powerful Spannable library
* **paddingLeft** (setPaddingLeft(int padding)) px
* **paddingRight** (setPaddingRight(int padding)) px
* **labelBgBorderColor** And **borderSize** (showBorder(int labelBgBorderColor, float borderSize | px))
* **isTextBold** (useTextBold())
* **textBold** (useTextBold())
* **textItalic** (useTextItalic())
* **clickable** See SimplifySpanBuild.appendMultiClickable() Or SimplifySpanBuild.appendMultiClickableToFirst()
* SpecialImageUnit
* **text** (Constructor | String)
Expand Down
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
compileSdkVersion 25
buildToolsVersion "25.0.2"

defaultConfig {
applicationId "cn.iwgang.simplifyspandemo"
minSdkVersion 14
targetSdkVersion 26
minSdkVersion 10
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand All @@ -21,6 +21,6 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support:appcompat-v7:25.4.0'
compile project(':library')
}
24 changes: 7 additions & 17 deletions app/src/main/java/cn/iwgang/simplifyspandemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import cn.iwgang.simplifyspan.SimplifySpanBuild;
import cn.iwgang.simplifyspan.customspan.CustomClickableSpan;
import cn.iwgang.simplifyspan.other.OnClickableSpanListener;
import cn.iwgang.simplifyspan.other.SpecialConvertMode;
import cn.iwgang.simplifyspan.other.SpecialGravity;
import cn.iwgang.simplifyspan.unit.SpecialClickableUnit;
import cn.iwgang.simplifyspan.unit.SpecialImageUnit;
Expand All @@ -31,7 +29,6 @@ protected void onCreate(Bundle savedInstanceState) {
TextView tvText11 = (TextView) findViewById(R.id.tv_text11);
TextView tvText2 = (TextView) findViewById(R.id.tv_text2);
TextView tvText21 = (TextView) findViewById(R.id.tv_text21);
TextView tvText22 = (TextView) findViewById(R.id.tv_text22);
TextView tvText3 = (TextView) findViewById(R.id.tv_text3);
TextView tvText4 = (TextView) findViewById(R.id.tv_text4);
TextView tvText41 = (TextView) findViewById(R.id.tv_text41);
Expand Down Expand Up @@ -76,13 +73,15 @@ protected void onCreate(Bundle savedInstanceState) {
.append(new SpecialLabelUnit("原创", Color.RED, sp2px(10), Color.TRANSPARENT).showBorder(Color.BLACK, 2).setPadding(5).setPaddingLeft(10).setPaddingRight(10).setGravity(SpecialGravity.CENTER));
tvText11.setText(simplifySpanBuild11.build());

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

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


SimplifySpanBuild simplifySpanBuild22 = new SimplifySpanBuild("正常底部正常居中正常顶部正常",
new SpecialTextUnit("底部").setTextSize(30).setTextColor(Color.BLUE),
new SpecialTextUnit("居中").setTextSize(30).setGravity(tvText22, SpecialGravity.CENTER).setTextColor(0xFFB03060),
new SpecialTextUnit("顶部").setTextSize(30).setGravity(tvText22, SpecialGravity.TOP).setTextColor(0xFFB0C4DE)

);
tvText22.setText(simplifySpanBuild22.build());

SimplifySpanBuild simplifySpanBuild3 = new SimplifySpanBuild();
simplifySpanBuild3.append(new SpecialImageUnit(this, BitmapFactory.decodeResource(getResources(), R.drawable.ic_bulletin), 50, 50).setGravity(SpecialGravity.CENTER))
.append("正常")
Expand Down Expand Up @@ -132,7 +122,7 @@ protected void onCreate(Bundle savedInstanceState) {
simplifySpanBuild4.append("完整文章见 ");
simplifySpanBuild4.appendMultiClickable(new SpecialClickableUnit(tvText4, this).setNormalTextColor(linkNorTextColor).setPressBgColor(linkPressBgColor),
new SpecialImageUnit(this, BitmapFactory.decodeResource(getResources(), R.drawable.timeline_card_small_article), 30, 30).setGravity(SpecialGravity.CENTER),
new SpecialTextUnit("LOL超强攻略,不见绝对后悔 ").showUnderline());
new SpecialTextUnit("LOL超强攻略,不看绝对后悔 ").showUnderline());
simplifySpanBuild4.append(" 更多好玩的内容请点击 ");
simplifySpanBuild4.appendMultiClickable(new SpecialClickableUnit(tvText4, this).setNormalTextColor(linkNorTextColor).setPressBgColor(linkPressBgColor),
new SpecialImageUnit(this, BitmapFactory.decodeResource(getResources(), R.drawable.timeline_card_small_web), 42, 42).setGravity(SpecialGravity.CENTER),
Expand Down
9 changes: 0 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,6 @@
android:paddingLeft="10dp"
android:textSize="30sp" />

<TextView
android:id="@+id/tv_text22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="12dp"
android:layout_marginTop="10dp"
android:textSize="12sp" />

<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'

classpath 'com.android.tools.build:gradle:3.0.0-beta6'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
8 changes: 4 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
compileSdkVersion 25
buildToolsVersion "25.0.2"

defaultConfig {
minSdkVersion 14
targetSdkVersion 26
minSdkVersion 10
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,13 @@ public SpannableStringBuilder build() {
spannableStringBuilder.setSpan(new StyleSpan(Typeface.BOLD), startPos, startPos + specialTextLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

//Set Text Style
if (specialTextUnit.getTextStyle() != Typeface.NORMAL){
// Set Text Italic
if (specialTextUnit.isTextItalic()) {
spannableStringBuilder.setSpan(new StyleSpan(Typeface.ITALIC), startPos, startPos + specialTextLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

// Set Text Style
if (specialTextUnit.getTextStyle() != Typeface.NORMAL) {
spannableStringBuilder.setSpan(new StyleSpan(specialTextUnit.getTextStyle()), startPos, startPos + specialTextLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

Expand All @@ -28,13 +27,13 @@ public class CustomClickableSpan extends ClickableSpan {
private int mTextColorPre;
private int mBgColorNor;
private int mBgColorPre;
private Object tag;
private String clickText;
private int startSpanIndex;
private int endSpanIndex;
private Object mTag;
private String mClickText;
private int mStartSpanIndex;
private int mEndSpanIndex;

public CustomClickableSpan(SpecialClickableUnit specialClickableUnit) {
setTag(specialClickableUnit.getTag());
mTag = specialClickableUnit.getTag();
mTextColorNor = specialClickableUnit.getNormalTextColor();
mTextColorPre = specialClickableUnit.getPressTextColor();
mBgColorNor = specialClickableUnit.getNormalBgColor();
Expand All @@ -47,11 +46,11 @@ public CustomClickableSpan(SpecialClickableUnit specialClickableUnit) {
@Override
public void onClick(View widget) {
if (null != mOnClickableSpanListener) {
TextView tv = (TextView)widget;
Spanned spanned = (Spanned)tv.getText();
startSpanIndex = spanned.getSpanStart(this);
endSpanIndex = spanned.getSpanEnd(this);
clickText = spanned.subSequence(startSpanIndex, endSpanIndex).toString();
TextView tv = (TextView) widget;
Spanned spanned = (Spanned) tv.getText();
mStartSpanIndex = spanned.getSpanStart(this);
mEndSpanIndex = spanned.getSpanEnd(this);
mClickText = spanned.subSequence(mStartSpanIndex, mEndSpanIndex).toString();
mOnClickableSpanListener.onClick(tv, this);
}
}
Expand Down Expand Up @@ -91,27 +90,32 @@ public void updateDrawState(TextPaint ds) {
}
}

/**
* get tag
*/
public Object getTag() {
return tag;
return mTag;
}

public void setTag(Object tag) {
this.tag = tag;
/**
* get current click text
*/
public String getClickText() {
return mClickText;
}

//get current click text
public String getClickText(){
return clickText;
}

//get current click text span start index in total
/**
* get current click text span start index in total
*/
public int getStartSpanIndex() {
return startSpanIndex;
return mStartSpanIndex;
}

//get current click text span start index in end
/**
* get current click text span start index in end
*/
public int getEndSpanIndex() {
return endSpanIndex;
return mEndSpanIndex;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.media.ThumbnailUtils;
import android.text.style.ReplacementSpan;

Expand Down Expand Up @@ -64,7 +65,8 @@ public CustomLabelSpan(String normalSizeText, SpecialLabelUnit specialLabelUnit)
}

private void initPadding() {
if (mSpecialLabelUnit.getLabelBgHeight() > 0 || mSpecialLabelUnit.getLabelBgWidth() > 0) return ;
if (mSpecialLabelUnit.getLabelBgHeight() > 0 || mSpecialLabelUnit.getLabelBgWidth() > 0)
return;

int allPadding = mSpecialLabelUnit.getPadding();

Expand All @@ -85,7 +87,8 @@ private void initPadding() {
mPaddingRight = allPadding;
}

if (mPaddingTop > 0 || mPaddingBottom > 0 || mPaddingLeft > 0 || mPaddingRight > 0) isLabelBgCenter = false;
if (mPaddingTop > 0 || mPaddingBottom > 0 || mPaddingLeft > 0 || mPaddingRight > 0)
isLabelBgCenter = false;
}

@Override
Expand Down Expand Up @@ -203,7 +206,13 @@ public void draw(Canvas canvas, CharSequence text, int start, int end, float x,
} else {
newTextX = x + mPaddingLeft;
}
if (mSpecialLabelUnit.isTextBold()) paint.setFakeBoldText(true);

if (mSpecialLabelUnit.isTextBold())
paint.setFakeBoldText(true);

if (mSpecialLabelUnit.isTextItalic())
paint.setTypeface(Typeface.create((String) null, Typeface.ITALIC));

canvas.drawText(mSpecialText, newTextX, newTextY, paint);
}

Expand Down
Loading

0 comments on commit 34e917c

Please sign in to comment.