diff --git a/README.md b/README.md index 61acf3d..e89fdb3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) @@ -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) diff --git a/app/build.gradle b/app/build.gradle index 52e9648..b13b4b4 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" } @@ -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') } diff --git a/app/src/main/java/cn/iwgang/simplifyspandemo/MainActivity.java b/app/src/main/java/cn/iwgang/simplifyspandemo/MainActivity.java index 209fc2c..fddcfc8 100755 --- a/app/src/main/java/cn/iwgang/simplifyspandemo/MainActivity.java +++ b/app/src/main/java/cn/iwgang/simplifyspandemo/MainActivity.java @@ -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; @@ -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); @@ -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())) @@ -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("正常") @@ -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), diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 243da2c..d17fa78 100755 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -72,15 +72,6 @@ android:paddingLeft="10dp" android:textSize="30sp" /> - - 0 || mSpecialLabelUnit.getLabelBgWidth() > 0) return ; + if (mSpecialLabelUnit.getLabelBgHeight() > 0 || mSpecialLabelUnit.getLabelBgWidth() > 0) + return; int allPadding = mSpecialLabelUnit.getPadding(); @@ -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 @@ -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); } diff --git a/library/src/main/java/cn/iwgang/simplifyspan/unit/SpecialLabelUnit.java b/library/src/main/java/cn/iwgang/simplifyspan/unit/SpecialLabelUnit.java index 07883ad..5cf7851 100755 --- a/library/src/main/java/cn/iwgang/simplifyspan/unit/SpecialLabelUnit.java +++ b/library/src/main/java/cn/iwgang/simplifyspan/unit/SpecialLabelUnit.java @@ -1,6 +1,7 @@ package cn.iwgang.simplifyspan.unit; import android.graphics.Bitmap; +import android.graphics.Typeface; /** * Label Special Unit @@ -8,6 +9,7 @@ * https://github.com/iwgang/SimplifySpan */ public class SpecialLabelUnit extends BaseSpecialUnit { + private int textStyle = Typeface.NORMAL; private int labelTextColor; private float labelTextSize; // sp private Bitmap mBitmap; @@ -22,6 +24,7 @@ public class SpecialLabelUnit extends BaseSpecialUnit { private int labelBgBorderColor; private boolean isShowBorder; private boolean isTextBold; + private boolean isTextItalic; private boolean isClickable; private int bgColor; @@ -128,6 +131,22 @@ public SpecialLabelUnit useTextBold() { return this; } + public SpecialLabelUnit useTextItalic() { + isTextItalic = true; + return this; + } + + /** + * Use TextView support textStyle + * + * @param textStyle please see {@link Typeface} + * @return SpecialTextUnit + */ + public SpecialLabelUnit setTextStyle(int textStyle) { + this.textStyle = textStyle; + return this; + } + public SpecialLabelUnit setBgColor(int bgColor) { this.bgColor = bgColor; return this; @@ -233,4 +252,13 @@ public int getBgColor() { return bgColor; } + public boolean isTextItalic() { + return isTextItalic; + } + + public int getTextStyle() { + return textStyle; + } + + } \ No newline at end of file diff --git a/library/src/main/java/cn/iwgang/simplifyspan/unit/SpecialTextUnit.java b/library/src/main/java/cn/iwgang/simplifyspan/unit/SpecialTextUnit.java index 3c79465..7e48c8f 100755 --- a/library/src/main/java/cn/iwgang/simplifyspan/unit/SpecialTextUnit.java +++ b/library/src/main/java/cn/iwgang/simplifyspan/unit/SpecialTextUnit.java @@ -16,6 +16,7 @@ public class SpecialTextUnit extends BaseSpecialUnit { private boolean isShowUnderline; private boolean isShowStrikeThrough; private boolean isTextBold; + private boolean isTextItalic; private SpecialClickableUnit specialClickableUnit; private TextView curTextView; @@ -82,19 +83,28 @@ public SpecialTextUnit showUnderline() { } /** - * Deprecated, please use {@link #setTextStyle} * Use Text Bold * * @return SpecialTextUnit */ - @Deprecated public SpecialTextUnit useTextBold() { isTextBold = true; return this; } + /** + * Use Text Italic + * + * @return SpecialTextUnit + */ + public SpecialTextUnit useTextItalic() { + isTextItalic = true; + return this; + } + /** * Use TextView support textStyle + * * @param textStyle please see {@link Typeface} * @return SpecialTextUnit */ @@ -195,6 +205,10 @@ public boolean isTextBold() { return isTextBold; } + public boolean isTextItalic() { + return isTextItalic; + } + public int getTextStyle() { return textStyle; }