├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── values
│ │ │ │ ├── colors.xml
│ │ │ │ ├── strings.xml
│ │ │ │ └── styles.xml
│ │ │ ├── layout
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── barnettwong
│ │ │ └── autofitcolortextview
│ │ │ └── MainActivity.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── barnettwong
│ │ │ └── autofitcolortextview
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── barnettwong
│ │ └── autofitcolortextview
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── autofitcolortextview_library
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ ├── strings.xml
│ │ │ │ └── attrs.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── barnettwong
│ │ │ └── autofitcolortextview_library
│ │ │ ├── DensityUtil.java
│ │ │ ├── CustomLinkMovementMethod.java
│ │ │ ├── AutofitLayout.java
│ │ │ ├── AutofitHelper.java
│ │ │ └── AutoFitColorTextView.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── barnettwong
│ │ │ └── autofitcolortextview_library
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── barnettwong
│ │ └── autofitcolortextview_library
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── screenshot
└── show.png
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── vcs.xml
├── runConfigurations.xml
├── gradle.xml
├── codeStyles
│ └── Project.xml
└── misc.xml
├── .gitignore
├── gradle.properties
├── README.md
├── gradlew.bat
├── gradlew
└── LICENSE
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/autofitcolortextview_library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':autofitcolortextview_library'
2 |
--------------------------------------------------------------------------------
/screenshot/show.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/screenshot/show.png
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/autofitcolortextview_library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AutoFitColorTextview_Library
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Winstonokk/AutoFitColorTextView/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/autofitcolortextview_library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches/build_file_checksums.ser
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | .DS_Store
9 | /build
10 | /captures
11 | .externalNativeBuild
12 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 | #FF2D2D
8 | #333333
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AutoFitColorTextView
3 |
4 | 开通%1$s元尊享会员,开通即领取%2$s元礼包
5 | 开通%1$s元尊享会员,开通即领取%2$s元礼包,免费观看VIP视频~
6 | 开通%1$s元尊享会员,开通即领取%2$s元礼包,还能够参加多种有趣的抽奖活动哦~
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/barnettwong/autofitcolortextview/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.barnettwong.autofitcolortextview;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/autofitcolortextview_library/src/test/java/com/barnettwong/autofitcolortextview_library/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.barnettwong.autofitcolortextview_library;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/autofitcolortextview_library/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/autofitcolortextview_library/src/main/java/com/barnettwong/autofitcolortextview_library/DensityUtil.java:
--------------------------------------------------------------------------------
1 | package com.barnettwong.autofitcolortextview_library;
2 |
3 | import android.content.Context;
4 |
5 |
6 | public class DensityUtil {
7 |
8 | //根据手机的分辨率从 dp 的单位 转成为 px(像素)
9 | public static int dp2px(Context context, float dpValue) {
10 | final float scale = context.getResources().getDisplayMetrics().density;
11 | return (int) (dpValue * scale + 0.5f);
12 | }
13 |
14 | //根据手机的分辨率从 px(像素) 的单位 转成为 dp
15 | public static int px2dp(Context context, float pxValue) {
16 | final float scale = context.getResources().getDisplayMetrics().density;
17 | return (int) (pxValue / scale + 0.5f);
18 | }
19 | }
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/autofitcolortextview_library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/barnettwong/autofitcolortextview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.barnettwong.autofitcolortextview;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.barnettwong.autofitcolortextview", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/autofitcolortextview_library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 |
6 |
7 |
8 | defaultConfig {
9 | minSdkVersion 15
10 | targetSdkVersion 28
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15 |
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 |
25 | }
26 |
27 | dependencies {
28 | implementation fileTree(dir: 'libs', include: ['*.jar'])
29 |
30 | implementation 'com.android.support:appcompat-v7:28.0.0'
31 | testImplementation 'junit:junit:4.12'
32 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
33 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
34 | }
35 |
--------------------------------------------------------------------------------
/autofitcolortextview_library/src/androidTest/java/com/barnettwong/autofitcolortextview_library/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.barnettwong.autofitcolortextview_library;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.barnettwong.autofitcolortextview_library.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.barnettwong.autofitcolortextview"
7 | minSdkVersion 15
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | implementation 'com.android.support:appcompat-v7:28.0.0'
24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28 |
29 | implementation project(':autofitcolortextview_library')
30 | }
31 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
21 |
32 |
43 |
44 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AutoFitColorTextView
2 | 固定边界,内容字体大小自适应并且可以指定同一字符串不同内容不同颜色的 TextView
3 |
4 | 
5 |
6 | 使用步骤
7 | ===================================
8 |
9 | step1:
10 | -------
11 |
12 | 在gradle中直接引用
13 |
14 | 在你项目根目录的build.gradle中添加
15 |
16 | allprojects {
17 |
18 | repositories {
19 |
20 | ...
21 |
22 | maven { url 'https://jitpack.io' }
23 |
24 | }
25 |
26 | }
27 |
28 | step2:
29 | -------
30 |
31 | 在module下的build.gradle中添加:
32 |
33 | dependencies {
34 |
35 | implementation 'com.github.wangfeng19930909:AutoFitColorTextView:1.0.0'
36 |
37 | }
38 |
39 | step3:
40 | -------
41 |
42 |
49 |
50 |
61 |
62 |
63 |
64 | MIT License
65 | ===================================
66 | Copyright 2019, wangfeng19930909
67 |
68 | Licensed under the Apache License, Version 2.0 (the "License");
69 | you may not use this file except in compliance with the License.
70 | You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
71 | Unless required by applicable law or agreed to in writing, software
72 | distributed under the License is distributed on an "AS IS" BASIS,
73 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74 | See the License for the specific language governing permissions and
75 | limitations under the License.
76 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/autofitcolortextview_library/src/main/java/com/barnettwong/autofitcolortextview_library/CustomLinkMovementMethod.java:
--------------------------------------------------------------------------------
1 | package com.barnettwong.autofitcolortextview_library;
2 |
3 | import android.text.Layout;
4 | import android.text.Selection;
5 | import android.text.Spannable;
6 | import android.text.method.LinkMovementMethod;
7 | import android.text.style.ClickableSpan;
8 | import android.view.MotionEvent;
9 | import android.widget.TextView;
10 |
11 | public class CustomLinkMovementMethod extends LinkMovementMethod {
12 |
13 | @Override
14 | public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
15 | int action = event.getAction();
16 |
17 | if (action == MotionEvent.ACTION_UP ||
18 | action == MotionEvent.ACTION_DOWN) {
19 | int x = (int) event.getX();
20 | int y = (int) event.getY();
21 |
22 | x -= widget.getTotalPaddingLeft();
23 | y -= widget.getTotalPaddingTop();
24 |
25 | x += widget.getScrollX();
26 | y += widget.getScrollY();
27 |
28 | Layout layout = widget.getLayout();
29 | int line = layout.getLineForVertical(y);
30 | int off = layout.getOffsetForHorizontal(line, x);
31 |
32 | ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
33 |
34 | if (link.length != 0) {
35 | if (action == MotionEvent.ACTION_UP) {
36 | link[0].onClick(widget);
37 |
38 | } else if (action == MotionEvent.ACTION_DOWN) {
39 | Selection.setSelection(buffer,
40 | buffer.getSpanStart(link[0]),
41 | buffer.getSpanEnd(link[0]));
42 | }
43 |
44 | if(widget instanceof AutoFitColorTextView){
45 | ((AutoFitColorTextView)widget).isClickSpan = true;
46 | }
47 |
48 | return true;
49 | } else {
50 | Selection.removeSelection(buffer);
51 | super.onTouchEvent(widget, buffer, event);
52 | return false;
53 | }
54 | }
55 | return super.onTouchEvent(widget, buffer, event);
56 | }
57 |
58 | public static CustomLinkMovementMethod getInstance() {
59 | if(sInstance == null){
60 | sInstance = new CustomLinkMovementMethod();
61 | }
62 | return sInstance;
63 | }
64 |
65 |
66 | private static CustomLinkMovementMethod sInstance;
67 | }
68 |
--------------------------------------------------------------------------------
/app/src/main/java/com/barnettwong/autofitcolortextview/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.barnettwong.autofitcolortextview;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 |
6 | import com.barnettwong.autofitcolortextview_library.AutoFitColorTextView;
7 |
8 | public class MainActivity extends AppCompatActivity {
9 | private AutoFitColorTextView autoFitColorTextView;
10 | private AutoFitColorTextView autoFitColorTextView2;
11 | private AutoFitColorTextView autoFitColorTextView3;
12 |
13 | @Override
14 | protected void onCreate(Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | setContentView(R.layout.activity_main);
17 |
18 | initView();
19 | }
20 |
21 | private void initView() {
22 | autoFitColorTextView=findViewById(R.id.textview);
23 | autoFitColorTextView2=findViewById(R.id.textview2);
24 | autoFitColorTextView3=findViewById(R.id.textview3);
25 |
26 | //1.设置一段文字内容(必须要先设置一段文字)
27 | String formatStr = getResources().getString(R.string.str_open_member_tip);
28 | String finalStr = String.format(formatStr, "396","456");
29 | autoFitColorTextView.setText(finalStr);
30 | //2.设置文字整体的字体颜色
31 | autoFitColorTextView.setTextColor(getResources().getColor(R.color.color_333333));
32 | //3.开始设置指定字体的颜色,大小,样式
33 | autoFitColorTextView.setTextArrColor("396",getResources().getColor(R.color.color_ff2d2d));
34 | autoFitColorTextView.setTextArrColor("456",getResources().getColor(R.color.color_ff2d2d));
35 |
36 | //1.设置一段文字内容(必须要先设置一段文字)
37 | String formatStr2 = getResources().getString(R.string.str_open_member_tip2);
38 | String finalStr2 = String.format(formatStr2, "396","456");
39 | autoFitColorTextView2.setText(finalStr2);
40 | //2.设置文字整体的字体颜色
41 | autoFitColorTextView2.setTextColor(getResources().getColor(R.color.color_333333));
42 | //3.开始设置指定字体的颜色,大小,样式
43 | autoFitColorTextView2.setTextArrColor("396",getResources().getColor(R.color.color_ff2d2d));
44 | autoFitColorTextView2.setTextArrColor("456",getResources().getColor(R.color.color_ff2d2d));
45 |
46 | //1.设置一段文字内容(必须要先设置一段文字)
47 | String formatStr3 = getResources().getString(R.string.str_open_member_tip3);
48 | String finalStr3 = String.format(formatStr3, "396","456");
49 | autoFitColorTextView3.setText(finalStr3);
50 | //2.设置文字整体的字体颜色
51 | autoFitColorTextView3.setTextColor(getResources().getColor(R.color.color_333333));
52 | //3.开始设置指定字体的颜色,大小,样式
53 | autoFitColorTextView3.setTextArrColor("396",getResources().getColor(R.color.color_ff2d2d));
54 | autoFitColorTextView3.setTextArrColor("456",getResources().getColor(R.color.color_ff2d2d));
55 |
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/autofitcolortextview_library/src/main/java/com/barnettwong/autofitcolortextview_library/AutofitLayout.java:
--------------------------------------------------------------------------------
1 | package com.barnettwong.autofitcolortextview_library;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.util.AttributeSet;
6 | import android.util.TypedValue;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.FrameLayout;
10 | import android.widget.TextView;
11 |
12 | import java.util.WeakHashMap;
13 |
14 | /**
15 | * A {@link ViewGroup} that re-sizes the text of it's children to be no larger than the width of the
16 | * view.
17 | *
18 | * @attr ref R.styleable.AutofitTextView_sizeToFit
19 | * @attr ref R.styleable.AutofitTextView_minTextSize
20 | * @attr ref R.styleable.AutofitTextView_precision
21 | */
22 | public class AutofitLayout extends FrameLayout {
23 |
24 | private boolean mEnabled;
25 | private float mMinTextSize;
26 | private float mPrecision;
27 | private WeakHashMap mHelpers = new WeakHashMap();
28 |
29 | public AutofitLayout(Context context) {
30 | super(context);
31 | init(context, null, 0);
32 | }
33 |
34 | public AutofitLayout(Context context, AttributeSet attrs) {
35 | super(context, attrs);
36 | init(context, attrs, 0);
37 | }
38 |
39 | public AutofitLayout(Context context, AttributeSet attrs, int defStyle) {
40 | super(context, attrs, defStyle);
41 | init(context, attrs, defStyle);
42 | }
43 |
44 | private void init(Context context, AttributeSet attrs, int defStyle) {
45 | boolean sizeToFit = true;
46 | int minTextSize = -1;
47 | float precision = -1;
48 |
49 | if (attrs != null) {
50 | TypedArray ta = context.obtainStyledAttributes(
51 | attrs,
52 | R.styleable.AutoFitColorTextView,
53 | defStyle,
54 | 0);
55 | sizeToFit = ta.getBoolean(R.styleable.AutoFitColorTextView_sizeToFit, sizeToFit);
56 | minTextSize = ta.getDimensionPixelSize(R.styleable.AutoFitColorTextView_minTextSize,
57 | minTextSize);
58 | precision = ta.getFloat(R.styleable.AutoFitColorTextView_precision, precision);
59 | ta.recycle();
60 | }
61 |
62 | mEnabled = sizeToFit;
63 | mMinTextSize = minTextSize;
64 | mPrecision = precision;
65 | }
66 |
67 | @Override
68 | public void addView(View child, int index, ViewGroup.LayoutParams params) {
69 | super.addView(child, index, params);
70 | TextView textView = (TextView) child;
71 | AutofitHelper helper = AutofitHelper.create(textView)
72 | .setEnabled(mEnabled);
73 | if (mPrecision > 0) {
74 | helper.setPrecision(mPrecision);
75 | }
76 | if (mMinTextSize > 0) {
77 | helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, mMinTextSize);
78 | }
79 | mHelpers.put(textView, helper);
80 | }
81 |
82 | /**
83 | * Returns the {@link AutofitHelper} for this child View.
84 | */
85 | public AutofitHelper getAutofitHelper(TextView textView) {
86 | return mHelpers.get(textView);
87 | }
88 |
89 | /**
90 | * Returns the {@link AutofitHelper} for this child View.
91 | */
92 | public AutofitHelper getAutofitHelper(int index) {
93 | return mHelpers.get(getChildAt(index));
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/autofitcolortextview_library/src/main/java/com/barnettwong/autofitcolortextview_library/AutofitHelper.java:
--------------------------------------------------------------------------------
1 | package com.barnettwong.autofitcolortextview_library;
2 |
3 | import android.content.Context;
4 | import android.content.res.Resources;
5 | import android.content.res.TypedArray;
6 | import android.os.Build;
7 | import android.text.Editable;
8 | import android.text.Layout;
9 | import android.text.StaticLayout;
10 | import android.text.TextPaint;
11 | import android.text.TextWatcher;
12 | import android.text.method.SingleLineTransformationMethod;
13 | import android.text.method.TransformationMethod;
14 | import android.util.AttributeSet;
15 | import android.util.DisplayMetrics;
16 | import android.util.Log;
17 | import android.util.TypedValue;
18 | import android.view.View;
19 | import android.widget.TextView;
20 |
21 | import java.util.ArrayList;
22 |
23 | /**
24 | * A helper class to enable automatically resizing {@link TextView}`s {@code textSize} to fit
25 | * within its bounds.
26 | *
27 | * @attr ref R.styleable.AutofitTextView_sizeToFit
28 | * @attr ref R.styleable.AutofitTextView_minTextSize
29 | * @attr ref R.styleable.AutofitTextView_precision
30 | */
31 | public class AutofitHelper {
32 |
33 | private static final String TAG = "AutoFitTextHelper";
34 | private static final boolean SPEW = false;
35 |
36 | // Minimum size of the text in pixels
37 | private static final int DEFAULT_MIN_TEXT_SIZE = 8; //sp
38 | // How precise we want to be when reaching the target textWidth size
39 | private static final float DEFAULT_PRECISION = 0.5f;
40 |
41 | /**
42 | * Creates a new instance of {@code AutofitHelper} that wraps a {@link TextView} and enables
43 | * automatically sizing the text to fit.
44 | */
45 | public static AutofitHelper create(TextView view) {
46 | return create(view, null, 0);
47 | }
48 |
49 | /**
50 | * Creates a new instance of {@code AutofitHelper} that wraps a {@link TextView} and enables
51 | * automatically sizing the text to fit.
52 | */
53 | public static AutofitHelper create(TextView view, AttributeSet attrs) {
54 | return create(view, attrs, 0);
55 | }
56 |
57 | /**
58 | * Creates a new instance of {@code AutofitHelper} that wraps a {@link TextView} and enables
59 | * automatically sizing the text to fit.
60 | */
61 | public static AutofitHelper create(TextView view, AttributeSet attrs, int defStyle) {
62 | AutofitHelper helper = new AutofitHelper(view);
63 | boolean sizeToFit = true;
64 | if (attrs != null) {
65 | Context context = view.getContext();
66 | int minTextSize = (int) helper.getMinTextSize();
67 | float precision = helper.getPrecision();
68 |
69 | TypedArray ta = context.obtainStyledAttributes(
70 | attrs,
71 | R.styleable.AutoFitColorTextView,
72 | defStyle,
73 | 0);
74 | sizeToFit = ta.getBoolean(R.styleable.AutoFitColorTextView_sizeToFit, sizeToFit);
75 | minTextSize = ta.getDimensionPixelSize(R.styleable.AutoFitColorTextView_minTextSize,
76 | minTextSize);
77 | precision = ta.getFloat(R.styleable.AutoFitColorTextView_precision, precision);
78 | ta.recycle();
79 |
80 | helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, minTextSize)
81 | .setPrecision(precision);
82 | }
83 | helper.setEnabled(sizeToFit);
84 |
85 | return helper;
86 | }
87 |
88 | /**
89 | * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
90 | */
91 | private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize,
92 | int maxLines, float precision) {
93 | if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
94 | // Don't auto-size since there's no limit on lines.
95 | return;
96 | }
97 |
98 | int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
99 | if (targetWidth <= 0) {
100 | return;
101 | }
102 |
103 | CharSequence text = view.getText();
104 | TransformationMethod method = view.getTransformationMethod();
105 | if (method != null) {
106 | text = method.getTransformation(text, view);
107 | }
108 |
109 | Context context = view.getContext();
110 | Resources r = Resources.getSystem();
111 | DisplayMetrics displayMetrics;
112 |
113 | float size = maxTextSize;
114 | float high = size;
115 | float low = 0;
116 |
117 | if (context != null) {
118 | r = context.getResources();
119 | }
120 | displayMetrics = r.getDisplayMetrics();
121 |
122 | paint.set(view.getPaint());
123 | paint.setTextSize(size);
124 |
125 | if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth)
126 | || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
127 | size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision,
128 | displayMetrics);
129 | }
130 |
131 | if (size < minTextSize) {
132 | size = minTextSize;
133 | }
134 |
135 | view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
136 | }
137 |
138 | /**
139 | * Recursive binary search to find the best size for the text.
140 | */
141 | private static float getAutofitTextSize(CharSequence text, TextPaint paint,
142 | float targetWidth, int maxLines, float low, float high, float precision,
143 | DisplayMetrics displayMetrics) {
144 | float mid = (low + high) / 2.0f;
145 | int lineCount = 1;
146 | StaticLayout layout = null;
147 |
148 | paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, mid,
149 | displayMetrics));
150 |
151 | if (maxLines != 1) {
152 | layout = new StaticLayout(text, paint, (int)targetWidth, Layout.Alignment.ALIGN_NORMAL,
153 | 1.0f, 0.0f, true);
154 | lineCount = layout.getLineCount();
155 | }
156 |
157 | if (SPEW) Log.d(TAG, "low=" + low + " high=" + high + " mid=" + mid +
158 | " target=" + targetWidth + " maxLines=" + maxLines + " lineCount=" + lineCount);
159 |
160 | if (lineCount > maxLines) {
161 | // For the case that `text` has more newline characters than `maxLines`.
162 | if ((high - low) < precision) {
163 | return low;
164 | }
165 | return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision,
166 | displayMetrics);
167 | }
168 | else if (lineCount < maxLines) {
169 | return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision,
170 | displayMetrics);
171 | }
172 | else {
173 | float maxLineWidth = 0;
174 | if (maxLines == 1) {
175 | maxLineWidth = paint.measureText(text, 0, text.length());
176 | } else {
177 | for (int i = 0; i < lineCount; i++) {
178 | if (layout.getLineWidth(i) > maxLineWidth) {
179 | maxLineWidth = layout.getLineWidth(i);
180 | }
181 | }
182 | }
183 |
184 | if ((high - low) < precision) {
185 | return low;
186 | } else if (maxLineWidth > targetWidth) {
187 | return getAutofitTextSize(text, paint, targetWidth, maxLines, low, mid, precision,
188 | displayMetrics);
189 | } else if (maxLineWidth < targetWidth) {
190 | return getAutofitTextSize(text, paint, targetWidth, maxLines, mid, high, precision,
191 | displayMetrics);
192 | } else {
193 | return mid;
194 | }
195 | }
196 | }
197 |
198 | private static int getLineCount(CharSequence text, TextPaint paint, float size, float width,
199 | DisplayMetrics displayMetrics) {
200 | paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, size,
201 | displayMetrics));
202 | StaticLayout layout = new StaticLayout(text, paint, (int)width,
203 | Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
204 | return layout.getLineCount();
205 | }
206 |
207 | private static int getMaxLines(TextView view) {
208 | int maxLines = -1; // No limit (Integer.MAX_VALUE also means no limit)
209 |
210 | TransformationMethod method = view.getTransformationMethod();
211 | if (method != null && method instanceof SingleLineTransformationMethod) {
212 | maxLines = 1;
213 | }
214 | else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
215 | // setMaxLines() and getMaxLines() are only available on android-16+
216 | maxLines = view.getMaxLines();
217 | }
218 |
219 | return maxLines;
220 | }
221 |
222 | // Attributes
223 | private TextView mTextView;
224 | private TextPaint mPaint;
225 | /**
226 | * Original textSize of the TextView.
227 | */
228 | private float mTextSize;
229 |
230 | private int mMaxLines;
231 | private float mMinTextSize;
232 | private float mMaxTextSize;
233 | private float mPrecision;
234 |
235 | private boolean mEnabled;
236 | private boolean mIsAutofitting;
237 |
238 | private ArrayList mListeners;
239 |
240 | private TextWatcher mTextWatcher = new AutofitTextWatcher();
241 |
242 | private View.OnLayoutChangeListener mOnLayoutChangeListener =
243 | new AutofitOnLayoutChangeListener();
244 |
245 | private AutofitHelper(TextView view) {
246 | final Context context = view.getContext();
247 | float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
248 |
249 | mTextView = view;
250 | mPaint = new TextPaint();
251 | setRawTextSize(view.getTextSize());
252 |
253 | mMaxLines = getMaxLines(view);
254 | mMinTextSize = scaledDensity * DEFAULT_MIN_TEXT_SIZE;
255 | mMaxTextSize = mTextSize;
256 | mPrecision = DEFAULT_PRECISION;
257 | }
258 |
259 | /**
260 | * Adds an {@link OnTextSizeChangeListener} to the list of those whose methods are called
261 | * whenever the {@link TextView}'s {@code textSize} changes.
262 | */
263 | public AutofitHelper addOnTextSizeChangeListener(OnTextSizeChangeListener listener) {
264 | if (mListeners == null) {
265 | mListeners = new ArrayList();
266 | }
267 | mListeners.add(listener);
268 | return this;
269 | }
270 |
271 | /**
272 | * Removes the specified {@link OnTextSizeChangeListener} from the list of those whose methods
273 | * are called whenever the {@link TextView}'s {@code textSize} changes.
274 | */
275 | public AutofitHelper removeOnTextSizeChangeListener(OnTextSizeChangeListener listener) {
276 | if (mListeners != null) {
277 | mListeners.remove(listener);
278 | }
279 | return this;
280 | }
281 |
282 | /**
283 | * Returns the amount of precision used to calculate the correct text size to fit within its
284 | * bounds.
285 | */
286 | public float getPrecision() {
287 | return mPrecision;
288 | }
289 |
290 | /**
291 | * Set the amount of precision used to calculate the correct text size to fit within its
292 | * bounds. Lower precision is more precise and takes more time.
293 | *
294 | * @param precision The amount of precision.
295 | */
296 | public AutofitHelper setPrecision(float precision) {
297 | if (mPrecision != precision) {
298 | mPrecision = precision;
299 |
300 | autofit();
301 | }
302 | return this;
303 | }
304 |
305 | /**
306 | * Returns the minimum size (in pixels) of the text.
307 | */
308 | public float getMinTextSize() {
309 | return mMinTextSize;
310 | }
311 |
312 | /**
313 | * Set the minimum text size to the given value, interpreted as "scaled pixel" units. This size
314 | * is adjusted based on the current density and user font size preference.
315 | *
316 | * @param size The scaled pixel size.
317 | *
318 | * @attr ref me.grantland.R.styleable#AutofitTextView_minTextSize
319 | */
320 | public AutofitHelper setMinTextSize(float size) {
321 | return setMinTextSize(TypedValue.COMPLEX_UNIT_SP, size);
322 | }
323 |
324 | /**
325 | * Set the minimum text size to a given unit and value. See TypedValue for the possible
326 | * dimension units.
327 | *
328 | * @param unit The desired dimension unit.
329 | * @param size The desired size in the given units.
330 | *
331 | * @attr ref me.grantland.R.styleable#AutofitTextView_minTextSize
332 | */
333 | public AutofitHelper setMinTextSize(int unit, float size) {
334 | Context context = mTextView.getContext();
335 | Resources r = Resources.getSystem();
336 |
337 | if (context != null) {
338 | r = context.getResources();
339 | }
340 |
341 | setRawMinTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()));
342 | return this;
343 | }
344 |
345 | private void setRawMinTextSize(float size) {
346 | if (size != mMinTextSize) {
347 | mMinTextSize = size;
348 |
349 | autofit();
350 | }
351 | }
352 |
353 | /**
354 | * Returns the maximum size (in pixels) of the text.
355 | */
356 | public float getMaxTextSize() {
357 | return mMaxTextSize;
358 | }
359 |
360 | /**
361 | * Set the maximum text size to the given value, interpreted as "scaled pixel" units. This size
362 | * is adjusted based on the current density and user font size preference.
363 | *
364 | * @param size The scaled pixel size.
365 | *
366 | * @attr ref android.R.styleable#TextView_textSize
367 | */
368 | public AutofitHelper setMaxTextSize(float size) {
369 | return setMaxTextSize(TypedValue.COMPLEX_UNIT_SP, size);
370 | }
371 |
372 | /**
373 | * Set the maximum text size to a given unit and value. See TypedValue for the possible
374 | * dimension units.
375 | *
376 | * @param unit The desired dimension unit.
377 | * @param size The desired size in the given units.
378 | *
379 | * @attr ref android.R.styleable#TextView_textSize
380 | */
381 | public AutofitHelper setMaxTextSize(int unit, float size) {
382 | Context context = mTextView.getContext();
383 | Resources r = Resources.getSystem();
384 |
385 | if (context != null) {
386 | r = context.getResources();
387 | }
388 |
389 | setRawMaxTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()));
390 | return this;
391 | }
392 |
393 | private void setRawMaxTextSize(float size) {
394 | if (size != mMaxTextSize) {
395 | mMaxTextSize = size;
396 |
397 | autofit();
398 | }
399 | }
400 |
401 | /**
402 | * @see TextView#getMaxLines()
403 | */
404 | public int getMaxLines() {
405 | return mMaxLines;
406 | }
407 |
408 | /**
409 | * @see TextView#setMaxLines(int)
410 | */
411 | public AutofitHelper setMaxLines(int lines) {
412 | if (mMaxLines != lines) {
413 | mMaxLines = lines;
414 |
415 | autofit();
416 | }
417 | return this;
418 | }
419 |
420 | /**
421 | * Returns whether or not automatically resizing text is enabled.
422 | */
423 | public boolean isEnabled() {
424 | return mEnabled;
425 | }
426 |
427 | /**
428 | * Set the enabled state of automatically resizing text.
429 | */
430 | public AutofitHelper setEnabled(boolean enabled) {
431 | if (mEnabled != enabled) {
432 | mEnabled = enabled;
433 |
434 | if (enabled) {
435 | mTextView.addTextChangedListener(mTextWatcher);
436 | mTextView.addOnLayoutChangeListener(mOnLayoutChangeListener);
437 |
438 | autofit();
439 | } else {
440 | mTextView.removeTextChangedListener(mTextWatcher);
441 | mTextView.removeOnLayoutChangeListener(mOnLayoutChangeListener);
442 |
443 | mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
444 | }
445 | }
446 | return this;
447 | }
448 |
449 | /**
450 | * Returns the original text size of the View.
451 | *
452 | * @see TextView#getTextSize()
453 | */
454 | public float getTextSize() {
455 | return mTextSize;
456 | }
457 |
458 | /**
459 | * Set the original text size of the View.
460 | *
461 | * @see TextView#setTextSize(float)
462 | */
463 | public void setTextSize(float size) {
464 | setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
465 | }
466 |
467 | /**
468 | * Set the original text size of the View.
469 | *
470 | * @see TextView#setTextSize(int, float)
471 | */
472 | public void setTextSize(int unit, float size) {
473 | if (mIsAutofitting) {
474 | // We don't want to update the TextView's actual textSize while we're autofitting
475 | // since it'd get set to the autofitTextSize
476 | return;
477 | }
478 | Context context = mTextView.getContext();
479 | Resources r = Resources.getSystem();
480 |
481 | if (context != null) {
482 | r = context.getResources();
483 | }
484 |
485 | setRawTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()));
486 | }
487 |
488 | private void setRawTextSize(float size) {
489 | if (mTextSize != size) {
490 | mTextSize = size;
491 | }
492 | }
493 |
494 | private void autofit() {
495 | float oldTextSize = mTextView.getTextSize();
496 | float textSize;
497 |
498 | mIsAutofitting = true;
499 | autofit(mTextView, mPaint, mMinTextSize, mMaxTextSize, mMaxLines, mPrecision);
500 | mIsAutofitting = false;
501 |
502 | textSize = mTextView.getTextSize();
503 | if (textSize != oldTextSize) {
504 | sendTextSizeChange(textSize, oldTextSize);
505 | }
506 | }
507 |
508 | private void sendTextSizeChange(float textSize, float oldTextSize) {
509 | if (mListeners == null) {
510 | return;
511 | }
512 |
513 | for (OnTextSizeChangeListener listener : mListeners) {
514 | listener.onTextSizeChange(textSize, oldTextSize);
515 | }
516 | }
517 |
518 | private class AutofitTextWatcher implements TextWatcher {
519 | @Override
520 | public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
521 | // do nothing
522 | }
523 |
524 | @Override
525 | public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
526 | autofit();
527 | }
528 |
529 | @Override
530 | public void afterTextChanged(Editable editable) {
531 | // do nothing
532 | }
533 | }
534 |
535 | private class AutofitOnLayoutChangeListener implements View.OnLayoutChangeListener {
536 | @Override
537 | public void onLayoutChange(View view, int left, int top, int right, int bottom,
538 | int oldLeft, int oldTop, int oldRight, int oldBottom) {
539 | autofit();
540 | }
541 | }
542 |
543 | /**
544 | * When an object of a type is attached to an {@code AutofitHelper}, its methods will be called
545 | * when the {@code textSize} is changed.
546 | */
547 | public interface OnTextSizeChangeListener {
548 | /**
549 | * This method is called to notify you that the size of the text has changed to
550 | * {@code textSize} from {@code oldTextSize}.
551 | */
552 | public void onTextSizeChange(float textSize, float oldTextSize);
553 | }
554 | }
555 |
--------------------------------------------------------------------------------
/autofitcolortextview_library/src/main/java/com/barnettwong/autofitcolortextview_library/AutoFitColorTextView.java:
--------------------------------------------------------------------------------
1 | package com.barnettwong.autofitcolortextview_library;
2 |
3 | import android.content.Context;
4 | import android.support.v7.widget.AppCompatTextView;
5 | import android.text.Spannable;
6 | import android.text.SpannableString;
7 | import android.text.TextPaint;
8 | import android.text.style.AbsoluteSizeSpan;
9 | import android.text.style.ClickableSpan;
10 | import android.text.style.ForegroundColorSpan;
11 | import android.text.style.StyleSpan;
12 | import android.util.AttributeSet;
13 | import android.util.TypedValue;
14 | import android.view.MotionEvent;
15 | import android.view.View;
16 |
17 | /**
18 | * A {@link AppCompatTextView} that re-sizes its text to be no larger than the width of the view.
19 | *
20 | * @attr ref R.styleable.AutofitTextView_sizeToFit
21 | * @attr ref R.styleable.AutofitTextView_minTextSize
22 | * @attr ref R.styleable.AutofitTextView_precision
23 | */
24 | public class AutoFitColorTextView extends AppCompatTextView implements AutofitHelper.OnTextSizeChangeListener {
25 |
26 | private AutofitHelper mHelper;
27 |
28 | public boolean isClickSpan = false;
29 | private String[] colorArr,sizeArr,styleArr;
30 |
31 | public AutoFitColorTextView(Context context) {
32 | super(context);
33 | init(context, null, 0);
34 | }
35 |
36 | public AutoFitColorTextView(Context context, AttributeSet attrs) {
37 | super(context, attrs);
38 | init(context, attrs, 0);
39 | }
40 |
41 | public AutoFitColorTextView(Context context, AttributeSet attrs, int defStyle) {
42 | super(context, attrs, defStyle);
43 | init(context, attrs, defStyle);
44 | }
45 |
46 | private void init(Context context, AttributeSet attrs, int defStyle) {
47 | mHelper = AutofitHelper.create(this, attrs, defStyle)
48 | .addOnTextSizeChangeListener(this);
49 | }
50 |
51 | // Getters and Setters
52 |
53 | /**
54 | * {@inheritDoc}
55 | */
56 | @Override
57 | public void setTextSize(int unit, float size) {
58 | super.setTextSize(unit, size);
59 | if (mHelper != null) {
60 | mHelper.setTextSize(unit, size);
61 | }
62 | }
63 |
64 | /**
65 | * {@inheritDoc}
66 | */
67 | @Override
68 | public void setLines(int lines) {
69 | super.setLines(lines);
70 | if (mHelper != null) {
71 | mHelper.setMaxLines(lines);
72 | }
73 | }
74 |
75 | /**
76 | * {@inheritDoc}
77 | */
78 | @Override
79 | public void setMaxLines(int maxLines) {
80 | super.setMaxLines(maxLines);
81 | if (mHelper != null) {
82 | mHelper.setMaxLines(maxLines);
83 | }
84 | }
85 |
86 | /**
87 | * Returns the {@link AutofitHelper} for this View.
88 | */
89 | public AutofitHelper getAutofitHelper() {
90 | return mHelper;
91 | }
92 |
93 | /**
94 | * Returns whether or not the text will be automatically re-sized to fit its constraints.
95 | */
96 | public boolean isSizeToFit() {
97 | return mHelper.isEnabled();
98 | }
99 |
100 | /**
101 | * Sets the property of this field (sizeToFit), to automatically resize the text to fit its
102 | * constraints.
103 | */
104 | public void setSizeToFit() {
105 | setSizeToFit(true);
106 | }
107 |
108 | /**
109 | * If true, the text will automatically be re-sized to fit its constraints; if false, it will
110 | * act like a normal TextView.
111 | *
112 | * @param sizeToFit
113 | */
114 | public void setSizeToFit(boolean sizeToFit) {
115 | mHelper.setEnabled(sizeToFit);
116 | }
117 |
118 | /**
119 | * Returns the maximum size (in pixels) of the text in this View.
120 | */
121 | public float getMaxTextSize() {
122 | return mHelper.getMaxTextSize();
123 | }
124 |
125 | /**
126 | * Set the maximum text size to the given value, interpreted as "scaled pixel" units. This size
127 | * is adjusted based on the current density and user font size preference.
128 | *
129 | * @param size The scaled pixel size.
130 | *
131 | * @attr ref android.R.styleable#TextView_textSize
132 | */
133 | public void setMaxTextSize(float size) {
134 | mHelper.setMaxTextSize(size);
135 | }
136 |
137 | /**
138 | * Set the maximum text size to a given unit and value. See TypedValue for the possible
139 | * dimension units.
140 | *
141 | * @param unit The desired dimension unit.
142 | * @param size The desired size in the given units.
143 | *
144 | * @attr ref android.R.styleable#TextView_textSize
145 | */
146 | public void setMaxTextSize(int unit, float size) {
147 | mHelper.setMaxTextSize(unit, size);
148 | }
149 |
150 | /**
151 | * Returns the minimum size (in pixels) of the text in this View.
152 | */
153 | public float getMinTextSize() {
154 | return mHelper.getMinTextSize();
155 | }
156 |
157 | /**
158 | * Set the minimum text size to the given value, interpreted as "scaled pixel" units. This size
159 | * is adjusted based on the current density and user font size preference.
160 | *
161 | * @param minSize The scaled pixel size.
162 | *
163 | * @attr ref me.grantland.R.styleable#AutofitTextView_minTextSize
164 | */
165 | public void setMinTextSize(int minSize) {
166 | mHelper.setMinTextSize(TypedValue.COMPLEX_UNIT_SP, minSize);
167 | }
168 |
169 | /**
170 | * Set the minimum text size to a given unit and value. See TypedValue for the possible
171 | * dimension units.
172 | *
173 | * @param unit The desired dimension unit.
174 | * @param minSize The desired size in the given units.
175 | *
176 | * @attr ref me.grantland.R.styleable#AutofitTextView_minTextSize
177 | */
178 | public void setMinTextSize(int unit, float minSize) {
179 | mHelper.setMinTextSize(unit, minSize);
180 | }
181 |
182 | /**
183 | * Returns the amount of precision used to calculate the correct text size to fit within its
184 | * bounds.
185 | */
186 | public float getPrecision() {
187 | return mHelper.getPrecision();
188 | }
189 |
190 | /**
191 | * Set the amount of precision used to calculate the correct text size to fit within its
192 | * bounds. Lower precision is more precise and takes more time.
193 | *
194 | * @param precision The amount of precision.
195 | */
196 | public void setPrecision(float precision) {
197 | mHelper.setPrecision(precision);
198 | }
199 |
200 | @Override
201 | public void onTextSizeChange(float textSize, float oldTextSize) {
202 | // do nothing
203 | }
204 |
205 | @Override
206 | public boolean performClick() {
207 | if (isClickSpan){
208 | return true;
209 | }
210 | return super.performClick();
211 | }
212 |
213 | @Override
214 | public boolean onTouchEvent(MotionEvent event) {
215 | isClickSpan = false;
216 | return super.onTouchEvent(event);
217 | }
218 |
219 | /**
220 | * 设置文本内容其中文字的颜色
221 | * @param text 内容
222 | * @param color 颜色
223 | */
224 | public AutoFitColorTextView setTextArrColor(String text, int color){
225 | if (getText().length()==0){
226 | throw new NullPointerException("Please Set The textView Content!");
227 | }
228 | SpannableString styledText = new SpannableString(getText());
229 | int startIndex = (getText()+"").indexOf(text);
230 | if (startIndex>-1){
231 | styledText.setSpan(new ForegroundColorSpan(color),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
232 | setText(styledText, BufferType.SPANNABLE);
233 | }
234 | return this;
235 | }
236 |
237 | /**
238 | * 设置文本内容其中文字的颜色
239 | * @param text 内容
240 | * @param color 颜色
241 | * @param onClickSpan 点击事件
242 | */
243 | public AutoFitColorTextView setTextArrColor(final String text, final int color, final AutoFitColorTextView.OnClickSpan onClickSpan){
244 | if (getText().length()==0){
245 | throw new NullPointerException("Please Set The textView Content!");
246 | }
247 | SpannableString styledText = new SpannableString(getText());
248 | int startIndex = (getText()+"").indexOf(text);
249 | if (startIndex>-1){
250 | styledText.setSpan(new ClickableSpan() {
251 | @Override
252 | public void onClick(View widget) {
253 | onClickSpan.onClick(text);
254 | }
255 |
256 | @Override
257 | public void updateDrawState(TextPaint ds) {
258 | super.updateDrawState(ds);
259 | //设置文本的颜色
260 | ds.setColor(color);
261 | //超链接形式的下划线,false 表示不
262 | ds.setUnderlineText(false);
263 | }
264 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
265 | setText(styledText, BufferType.SPANNABLE);
266 | setMovementMethod(CustomLinkMovementMethod.getInstance());
267 | }
268 | return this;
269 | }
270 |
271 | /**
272 | * 设置文本内容其中文字的颜色
273 | * @param text 内容
274 | * @param color 颜色
275 | * @param startIndex 开始位置
276 | */
277 | public AutoFitColorTextView setTextArrColor(String text, int color, int startIndex){
278 | if (getText().length()==0){
279 | throw new NullPointerException("Please Set The textView Content!");
280 | }
281 | SpannableString styledText = new SpannableString(getText());
282 | styledText.setSpan(new ForegroundColorSpan(color),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
283 | setText(styledText, BufferType.SPANNABLE);
284 | return this;
285 | }
286 |
287 | /**
288 | * 设置文本内容其中文字的颜色
289 | * @param text 内容
290 | * @param color 颜色
291 | * @param startIndex 开始位置
292 | * @param onClickSpan 点击事件
293 | */
294 | public AutoFitColorTextView setTextArrColor(final String text, final int color, int startIndex, final AutoFitColorTextView.OnClickSpan onClickSpan){
295 | if (getText().length()==0){
296 | throw new NullPointerException("Please Set The textView Content!");
297 | }
298 | SpannableString styledText = new SpannableString(getText());
299 | styledText.setSpan(new ClickableSpan() {
300 | @Override
301 | public void onClick(View widget) {
302 | onClickSpan.onClick(text);
303 | }
304 |
305 | @Override
306 | public void updateDrawState(TextPaint ds) {
307 | super.updateDrawState(ds);
308 | //设置文本的颜色
309 | ds.setColor(color);
310 | //超链接形式的下划线,false 表示不
311 | ds.setUnderlineText(false);
312 | }
313 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
314 | setText(styledText, BufferType.SPANNABLE);
315 | setMovementMethod(CustomLinkMovementMethod.getInstance());
316 |
317 | return this;
318 | }
319 |
320 | /**
321 | * 设置文本内容其中文字的字体大小
322 | * @param text 内容
323 | * @param size 字体大小
324 | */
325 | public AutoFitColorTextView setTextArrSize(String text, int size){
326 | if (getText().length()==0){
327 | throw new NullPointerException("Please Set The textView Content!");
328 | }
329 | SpannableString styledText = new SpannableString(getText());
330 | int startIndex = (getText()+"").indexOf(text);
331 | if (startIndex>-1){
332 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
333 | setText(styledText, BufferType.SPANNABLE);
334 | }
335 | return this;
336 | }
337 |
338 | /**
339 | * 设置文本内容其中文字的字体大小
340 | * @param text 内容
341 | * @param size 字体大小
342 | * @param onClickSpan 点击事件
343 | */
344 | public AutoFitColorTextView setTextArrSize(final String text, int size, final AutoFitColorTextView.OnClickSpan onClickSpan){
345 | if (getText().length()==0){
346 | throw new NullPointerException("Please Set The textView Content!");
347 | }
348 | SpannableString styledText = new SpannableString(getText());
349 | int startIndex = (getText()+"").indexOf(text);
350 | if (startIndex>-1){
351 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
352 | styledText.setSpan(new ClickableSpan() {
353 | @Override
354 | public void onClick(View widget) {
355 | onClickSpan.onClick(text);
356 | }
357 |
358 | @Override
359 | public void updateDrawState(TextPaint ds) {
360 | super.updateDrawState(ds);
361 | //设置文本的颜色
362 | ds.setColor(getTextColors().getDefaultColor());
363 | //超链接形式的下划线,false 表示不
364 | ds.setUnderlineText(false);
365 | }
366 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
367 | setText(styledText, BufferType.SPANNABLE);
368 | setMovementMethod(CustomLinkMovementMethod.getInstance());
369 | }
370 | return this;
371 | }
372 |
373 | /**
374 | * 设置文本内容其中文字的字体大小
375 | * @param text 内容
376 | * @param size 字体大小
377 | * @param startIndex 开始位置
378 | */
379 | public AutoFitColorTextView setTextArrSize(String text, int size, int startIndex){
380 | if (getText().length()==0){
381 | throw new NullPointerException("Please Set The textView Content!");
382 | }
383 | SpannableString styledText = new SpannableString(getText());
384 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
385 | setText(styledText, BufferType.SPANNABLE);
386 | return this;
387 | }
388 |
389 | /**
390 | * 设置文本内容其中文字的字体大小
391 | * @param text 内容
392 | * @param size 字体大小
393 | * @param startIndex 开始位置
394 | * @param onClickSpan 点击事件
395 | */
396 | public AutoFitColorTextView setTextArrSize(final String text, int size, int startIndex, final AutoFitColorTextView.OnClickSpan onClickSpan){
397 | if (getText().length()==0){
398 | throw new NullPointerException("Please Set The textView Content!");
399 | }
400 | SpannableString styledText = new SpannableString(getText());
401 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
402 | styledText.setSpan(new ClickableSpan() {
403 | @Override
404 | public void onClick(View widget) {
405 | onClickSpan.onClick(text);
406 | }
407 |
408 | @Override
409 | public void updateDrawState(TextPaint ds) {
410 | super.updateDrawState(ds);
411 | //设置文本的颜色
412 | ds.setColor(getTextColors().getDefaultColor());
413 | //超链接形式的下划线,false 表示不
414 | ds.setUnderlineText(false);
415 | }
416 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
417 | setText(styledText, BufferType.SPANNABLE);
418 | setMovementMethod(CustomLinkMovementMethod.getInstance());
419 | return this;
420 | }
421 |
422 | /**
423 | * 设置文本内容其中文字的字体样式
424 | * @param text 内容
425 | * @param style 字体样式
426 | */
427 | public AutoFitColorTextView setTextArrStyle(String text, int style){
428 | if (getText().length()==0){
429 | throw new NullPointerException("Please Set The textView Content!");
430 | }
431 | SpannableString styledText = new SpannableString(getText());
432 | int startIndex = (getText()+"").indexOf(text);
433 | if (startIndex>-1){
434 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
435 | setText(styledText, BufferType.SPANNABLE);
436 | }
437 | return this;
438 | }
439 |
440 | /**
441 | * 设置文本内容其中文字的字体样式
442 | * @param text 内容
443 | * @param style 字体样式
444 | * @param onClickSpan 点击事件
445 | */
446 | public AutoFitColorTextView setTextArrStyle(final String text, int style, final AutoFitColorTextView.OnClickSpan onClickSpan){
447 | if (getText().length()==0){
448 | throw new NullPointerException("Please Set The textView Content!");
449 | }
450 | SpannableString styledText = new SpannableString(getText());
451 | int startIndex = (getText()+"").indexOf(text);
452 | if (startIndex>-1){
453 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
454 | styledText.setSpan(new ClickableSpan() {
455 | @Override
456 | public void onClick(View widget) {
457 | onClickSpan.onClick(text);
458 | }
459 |
460 | @Override
461 | public void updateDrawState(TextPaint ds) {
462 | super.updateDrawState(ds);
463 | //设置文本的颜色
464 | ds.setColor(getTextColors().getDefaultColor());
465 | //超链接形式的下划线,false 表示不
466 | ds.setUnderlineText(false);
467 | }
468 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
469 | setText(styledText, BufferType.SPANNABLE);
470 | setMovementMethod(CustomLinkMovementMethod.getInstance());
471 | }
472 | return this;
473 | }
474 |
475 | /**
476 | * 设置文本内容其中文字的字体样式
477 | * @param text 内容
478 | * @param style 字体样式
479 | * @param startIndex 开始位置
480 | */
481 | public AutoFitColorTextView setTextArrStyle(String text, int style, int startIndex){
482 | if (getText().length()==0){
483 | throw new NullPointerException("Please Set The textView Content!");
484 | }
485 | SpannableString styledText = new SpannableString(getText());
486 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
487 | setText(styledText, BufferType.SPANNABLE);
488 | return this;
489 | }
490 |
491 | /**
492 | * 设置文本内容其中文字的字体样式
493 | * @param text 内容
494 | * @param style 字体样式
495 | * @param startIndex 开始位置
496 | * @param onClickSpan 点击事件
497 | */
498 | public AutoFitColorTextView setTextArrStyle(final String text, int style, int startIndex, final AutoFitColorTextView.OnClickSpan onClickSpan){
499 | if (getText().length()==0){
500 | throw new NullPointerException("Please Set The textView Content!");
501 | }
502 | SpannableString styledText = new SpannableString(getText());
503 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
504 | styledText.setSpan(new ClickableSpan() {
505 | @Override
506 | public void onClick(View widget) {
507 | onClickSpan.onClick(text);
508 | }
509 |
510 | @Override
511 | public void updateDrawState(TextPaint ds) {
512 | super.updateDrawState(ds);
513 | //设置文本的颜色
514 | ds.setColor(getTextColors().getDefaultColor());
515 | //超链接形式的下划线,false 表示不
516 | ds.setUnderlineText(false);
517 | }
518 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
519 | setText(styledText, BufferType.SPANNABLE);
520 | setMovementMethod(CustomLinkMovementMethod.getInstance());
521 | return this;
522 | }
523 |
524 | /**
525 | * 设置文本内容其中文字的字体颜色,字体大小
526 | * @param text 内容
527 | * @param color 字体颜色
528 | * @param size 字体大小
529 | */
530 | public AutoFitColorTextView setTextArrColorSize(String text, int color, int size){
531 | if (getText().length()==0){
532 | throw new NullPointerException("Please Set The textView Content!");
533 | }
534 | SpannableString styledText = new SpannableString(getText());
535 | int startIndex = (getText()+"").indexOf(text);
536 | if (startIndex>-1){
537 | styledText.setSpan(new ForegroundColorSpan(color),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
538 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
539 | setText(styledText, BufferType.SPANNABLE);
540 | }
541 | return this;
542 | }
543 |
544 | /**
545 | * 设置文本内容其中文字的字体颜色,字体大小
546 | * @param text 内容
547 | * @param color 字体颜色
548 | * @param size 字体大小
549 | * @param onClickSpan 点击事件
550 | */
551 | public AutoFitColorTextView setTextArrColorSize(final String text, final int color, int size, final AutoFitColorTextView.OnClickSpan onClickSpan){
552 | if (getText().length()==0){
553 | throw new NullPointerException("Please Set The textView Content!");
554 | }
555 | SpannableString styledText = new SpannableString(getText());
556 | int startIndex = (getText()+"").indexOf(text);
557 | if (startIndex>-1){
558 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
559 | styledText.setSpan(new ClickableSpan() {
560 | @Override
561 | public void onClick(View widget) {
562 | onClickSpan.onClick(text);
563 | }
564 |
565 | @Override
566 | public void updateDrawState(TextPaint ds) {
567 | super.updateDrawState(ds);
568 | //设置文本的颜色
569 | ds.setColor(color);
570 | //超链接形式的下划线,false 表示不
571 | ds.setUnderlineText(false);
572 | }
573 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
574 | setText(styledText, BufferType.SPANNABLE);
575 | setMovementMethod(CustomLinkMovementMethod.getInstance());
576 | }
577 | return this;
578 | }
579 |
580 | /**
581 | * 设置文本内容其中文字的字体颜色,字体大小
582 | * @param text 内容
583 | * @param color 字体颜色
584 | * @param size 字体大小
585 | * @param startIndex 开始位置
586 | */
587 | public AutoFitColorTextView setTextArrColorSize(String text, int color, int size, int startIndex){
588 | if (getText().length()==0){
589 | throw new NullPointerException("Please Set The textView Content!");
590 | }
591 | SpannableString styledText = new SpannableString(getText());
592 | styledText.setSpan(new ForegroundColorSpan(color),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
593 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
594 | setText(styledText, BufferType.SPANNABLE);
595 | return this;
596 | }
597 |
598 | /**
599 | * 设置文本内容其中文字的字体颜色,字体大小
600 | * @param text 内容
601 | * @param color 字体颜色
602 | * @param size 字体大小
603 | * @param startIndex 开始位置
604 | * @param onClickSpan 点击事件
605 | */
606 | public AutoFitColorTextView setTextArrColorSize(final String text, final int color, int size, int startIndex, final AutoFitColorTextView.OnClickSpan onClickSpan){
607 | if (getText().length()==0){
608 | throw new NullPointerException("Please Set The textView Content!");
609 | }
610 | SpannableString styledText = new SpannableString(getText());
611 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
612 | styledText.setSpan(new ClickableSpan() {
613 | @Override
614 | public void onClick(View widget) {
615 | onClickSpan.onClick(text);
616 | }
617 |
618 | @Override
619 | public void updateDrawState(TextPaint ds) {
620 | super.updateDrawState(ds);
621 | //设置文本的颜色
622 | ds.setColor(color);
623 | //超链接形式的下划线,false 表示不
624 | ds.setUnderlineText(false);
625 | }
626 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
627 | setText(styledText, BufferType.SPANNABLE);
628 | setMovementMethod(CustomLinkMovementMethod.getInstance());
629 | return this;
630 | }
631 |
632 | /**
633 | * 设置文本内容其中文字的字体颜色,字体样式
634 | * @param text 内容
635 | * @param color 字体颜色
636 | * @param style 字体样式
637 | */
638 | public AutoFitColorTextView setTextArrColorStyle(String text, int color, int style){
639 | if (getText().length()==0){
640 | throw new NullPointerException("Please Set The textView Content!");
641 | }
642 | SpannableString styledText = new SpannableString(getText());
643 | int startIndex = (getText()+"").indexOf(text);
644 | if (startIndex>-1){
645 | styledText.setSpan(new ForegroundColorSpan(color),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
646 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
647 | setText(styledText, BufferType.SPANNABLE);
648 | }
649 | return this;
650 | }
651 |
652 | /**
653 | * 设置文本内容其中文字的字体颜色,字体样式
654 | * @param text 内容
655 | * @param color 字体颜色
656 | * @param style 字体样式
657 | * @param onClickSpan 点击事件
658 | */
659 | public AutoFitColorTextView setTextArrColorStyle(final String text, final int color, int style, final AutoFitColorTextView.OnClickSpan onClickSpan){
660 | if (getText().length()==0){
661 | throw new NullPointerException("Please Set The textView Content!");
662 | }
663 | SpannableString styledText = new SpannableString(getText());
664 | int startIndex = (getText()+"").indexOf(text);
665 | if (startIndex>-1){
666 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
667 | styledText.setSpan(new ClickableSpan() {
668 | @Override
669 | public void onClick(View widget) {
670 | onClickSpan.onClick(text);
671 | }
672 |
673 | @Override
674 | public void updateDrawState(TextPaint ds) {
675 | super.updateDrawState(ds);
676 | //设置文本的颜色
677 | ds.setColor(color);
678 | //超链接形式的下划线,false 表示不
679 | ds.setUnderlineText(false);
680 | }
681 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
682 | setText(styledText, BufferType.SPANNABLE);
683 | setMovementMethod(CustomLinkMovementMethod.getInstance());
684 | }
685 | return this;
686 | }
687 |
688 | /**
689 | * 设置文本内容其中文字的字体颜色,字体样式
690 | * @param text 内容
691 | * @param color 字体颜色
692 | * @param style 字体样式
693 | * @param startIndex 开始位置
694 | */
695 | public AutoFitColorTextView setTextArrColorStyle(String text, int color, int style, int startIndex){
696 | if (getText().length()==0){
697 | throw new NullPointerException("Please Set The textView Content!");
698 | }
699 | SpannableString styledText = new SpannableString(getText());
700 | styledText.setSpan(new ForegroundColorSpan(color),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
701 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
702 | setText(styledText, BufferType.SPANNABLE);
703 | return this;
704 | }
705 |
706 | /**
707 | * 设置文本内容其中文字的字体颜色,字体样式
708 | * @param text 内容
709 | * @param color 字体颜色
710 | * @param style 字体样式
711 | * @param startIndex 开始位置
712 | * @param onClickSpan 点击事件
713 | */
714 | public AutoFitColorTextView setTextArrColorStyle(final String text, final int color, int style, int startIndex, final AutoFitColorTextView.OnClickSpan onClickSpan){
715 | if (getText().length()==0){
716 | throw new NullPointerException("Please Set The textView Content!");
717 | }
718 | SpannableString styledText = new SpannableString(getText());
719 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
720 | styledText.setSpan(new ClickableSpan() {
721 | @Override
722 | public void onClick(View widget) {
723 | onClickSpan.onClick(text);
724 | }
725 |
726 | @Override
727 | public void updateDrawState(TextPaint ds) {
728 | super.updateDrawState(ds);
729 | //设置文本的颜色
730 | ds.setColor(color);
731 | //超链接形式的下划线,false 表示不
732 | ds.setUnderlineText(false);
733 | }
734 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
735 | setText(styledText, BufferType.SPANNABLE);
736 | setMovementMethod(CustomLinkMovementMethod.getInstance());
737 | return this;
738 | }
739 |
740 | /**
741 | * 设置文本内容其中文字的字体大小,字体样式
742 | * @param text 内容
743 | * @param size 字体大小
744 | * @param style 字体样式
745 | */
746 | public AutoFitColorTextView setTextArrSizeStyle(String text, int size, int style){
747 | if (getText().length()==0){
748 | throw new NullPointerException("Please Set The textView Content!");
749 | }
750 | SpannableString styledText = new SpannableString(getText());
751 | int startIndex = (getText()+"").indexOf(text);
752 | if (startIndex>-1){
753 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
754 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
755 | setText(styledText, BufferType.SPANNABLE);
756 | }
757 | return this;
758 | }
759 |
760 | /**
761 | * 设置文本内容其中文字的字体大小,字体样式
762 | * @param text 内容
763 | * @param size 字体大小
764 | * @param style 字体样式
765 | * @param onClickSpan 点击事件
766 | */
767 | public AutoFitColorTextView setTextArrSizeStyle(final String text, int size, int style, final AutoFitColorTextView.OnClickSpan onClickSpan){
768 | if (getText().length()==0){
769 | throw new NullPointerException("Please Set The textView Content!");
770 | }
771 | SpannableString styledText = new SpannableString(getText());
772 | int startIndex = (getText()+"").indexOf(text);
773 | if (startIndex>-1){
774 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
775 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
776 | styledText.setSpan(new ClickableSpan() {
777 | @Override
778 | public void onClick(View widget) {
779 | onClickSpan.onClick(text);
780 | }
781 |
782 | @Override
783 | public void updateDrawState(TextPaint ds) {
784 | super.updateDrawState(ds);
785 | //设置文本的颜色
786 | ds.setColor(getTextColors().getDefaultColor());
787 | //超链接形式的下划线,false 表示不
788 | ds.setUnderlineText(false);
789 | }
790 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
791 | setText(styledText, BufferType.SPANNABLE);
792 | setMovementMethod(CustomLinkMovementMethod.getInstance());
793 | }
794 | return this;
795 | }
796 |
797 | /**
798 | * 设置文本内容其中文字的字体大小,字体样式
799 | * @param text 内容
800 | * @param size 字体大小
801 | * @param style 字体样式
802 | * @param startIndex 开始位置
803 | */
804 | public AutoFitColorTextView setTextArrSizeStyle(String text, int size, int style, int startIndex){
805 | if (getText().length()==0){
806 | throw new NullPointerException("Please Set The textView Content!");
807 | }
808 | SpannableString styledText = new SpannableString(getText());
809 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
810 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
811 | setText(styledText, BufferType.SPANNABLE);
812 | return this;
813 | }
814 |
815 | /**
816 | * 设置文本内容其中文字的字体大小,字体样式
817 | * @param text 内容
818 | * @param size 字体大小
819 | * @param style 字体样式
820 | * @param startIndex 开始位置
821 | * @param onClickSpan 点击事件
822 | */
823 | public AutoFitColorTextView setTextArrSizeStyle(final String text, int size, int style, int startIndex, final AutoFitColorTextView.OnClickSpan onClickSpan){
824 | if (getText().length()==0){
825 | throw new NullPointerException("Please Set The textView Content!");
826 | }
827 | SpannableString styledText = new SpannableString(getText());
828 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
829 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
830 | styledText.setSpan(new ClickableSpan() {
831 | @Override
832 | public void onClick(View widget) {
833 | onClickSpan.onClick(text);
834 | }
835 |
836 | @Override
837 | public void updateDrawState(TextPaint ds) {
838 | super.updateDrawState(ds);
839 | //设置文本的颜色
840 | ds.setColor(getTextColors().getDefaultColor());
841 | //超链接形式的下划线,false 表示不
842 | ds.setUnderlineText(false);
843 | }
844 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
845 | setText(styledText, BufferType.SPANNABLE);
846 | setMovementMethod(CustomLinkMovementMethod.getInstance());
847 | return this;
848 | }
849 |
850 | /**
851 | * 设置文本内容其中文字的字体颜色,字体大小,字体样式
852 | * @param text 内容
853 | * @param color 字体颜色
854 | * @param size 字体大小
855 | * @param style 字体样式
856 | */
857 | public AutoFitColorTextView setTextArrColorSizeStyle(String text, int color, int size, int style){
858 | if (getText().length()==0){
859 | throw new NullPointerException("Please Set The textView Content!");
860 | }
861 | SpannableString styledText = new SpannableString(getText());
862 | int startIndex = (getText()+"").indexOf(text);
863 | if (startIndex>-1){
864 | styledText.setSpan(new ForegroundColorSpan(color),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
865 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
866 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
867 | setText(styledText, BufferType.SPANNABLE);
868 | }
869 | return this;
870 | }
871 |
872 | /**
873 | * 设置文本内容其中文字的字体颜色,字体大小,字体样式
874 | * @param text 内容
875 | * @param color 字体颜色
876 | * @param size 字体大小
877 | * @param style 字体样式
878 | * @param onClickSpan 点击事件
879 | */
880 | public AutoFitColorTextView setTextArrColorSizeStyle(final String text, final int color, int size, int style, final AutoFitColorTextView.OnClickSpan onClickSpan){
881 | if (getText().length()==0){
882 | throw new NullPointerException("Please Set The textView Content!");
883 | }
884 | SpannableString styledText = new SpannableString(getText());
885 | int startIndex = (getText()+"").indexOf(text);
886 | if (startIndex>-1){
887 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
888 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
889 | styledText.setSpan(new ClickableSpan() {
890 | @Override
891 | public void onClick(View widget) {
892 | onClickSpan.onClick(text);
893 | }
894 |
895 | @Override
896 | public void updateDrawState(TextPaint ds) {
897 | super.updateDrawState(ds);
898 | //设置文本的颜色
899 | ds.setColor(color);
900 | //超链接形式的下划线,false 表示不
901 | ds.setUnderlineText(false);
902 | }
903 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
904 | setText(styledText, BufferType.SPANNABLE);
905 | setMovementMethod(CustomLinkMovementMethod.getInstance());
906 | }
907 | return this;
908 | }
909 |
910 | /**
911 | * 设置文本内容其中文字的字体颜色,字体大小,字体样式
912 | * @param text 内容
913 | * @param color 字体颜色
914 | * @param size 字体大小
915 | * @param style 字体样式
916 | * @param startIndex 开始位置
917 | */
918 | public AutoFitColorTextView setTextArrColorSizeStyle(String text, int color, int size, int style, int startIndex){
919 | if (getText().length()==0){
920 | throw new NullPointerException("Please Set The textView Content!");
921 | }
922 | SpannableString styledText = new SpannableString(getText());
923 | styledText.setSpan(new ForegroundColorSpan(color),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
924 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
925 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
926 | setText(styledText, BufferType.SPANNABLE);
927 | return this;
928 | }
929 |
930 | /**
931 | * 设置文本内容其中文字的字体颜色,字体大小,字体样式
932 | * @param text 内容
933 | * @param color 字体颜色
934 | * @param size 字体大小
935 | * @param style 字体样式
936 | * @param startIndex 开始位置
937 | * @param onClickSpan 点击事件
938 | */
939 | public AutoFitColorTextView setTextArrColorSizeStyle(final String text, final int color, int size, int style, int startIndex, final AutoFitColorTextView.OnClickSpan onClickSpan){
940 | if (getText().length()==0){
941 | throw new NullPointerException("Please Set The textView Content!");
942 | }
943 | SpannableString styledText = new SpannableString(getText());
944 | styledText.setSpan(new AbsoluteSizeSpan(DensityUtil.dp2px(getContext(),size)),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
945 | styledText.setSpan(new StyleSpan(style),startIndex,startIndex+text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
946 | styledText.setSpan(new ClickableSpan() {
947 | @Override
948 | public void onClick(View widget) {
949 | onClickSpan.onClick(text);
950 | }
951 |
952 | @Override
953 | public void updateDrawState(TextPaint ds) {
954 | super.updateDrawState(ds);
955 | //设置文本的颜色
956 | ds.setColor(color);
957 | //超链接形式的下划线,false 表示不
958 | ds.setUnderlineText(false);
959 | }
960 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
961 | setText(styledText, BufferType.SPANNABLE);
962 | setMovementMethod(CustomLinkMovementMethod.getInstance());
963 | return this;
964 | }
965 |
966 | /**
967 | * 设置文本内容其中文字的点击事件
968 | * @param text 内容
969 | * @param onClickSpan 点击事件
970 | */
971 | public AutoFitColorTextView setTextClick(final String text, final AutoFitColorTextView.OnClickSpan onClickSpan){
972 | if (getText().length()==0){
973 | throw new NullPointerException("Please Set The textView Content!");
974 | }
975 | SpannableString styledText = new SpannableString(getText());
976 | int startIndex = (getText()+"").indexOf(text);
977 | if (startIndex>-1){
978 | styledText.setSpan(new ClickableSpan() {
979 | @Override
980 | public void onClick(View widget) {
981 | onClickSpan.onClick(text);
982 | }
983 |
984 | @Override
985 | public void updateDrawState(TextPaint ds) {
986 | super.updateDrawState(ds);
987 | //设置文本的颜色
988 | ds.setColor(getTextColors().getDefaultColor());
989 | //超链接形式的下划线,false 表示不
990 | ds.setUnderlineText(false);
991 | }
992 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
993 | setText(styledText, BufferType.SPANNABLE);
994 | setMovementMethod(CustomLinkMovementMethod.getInstance());
995 | }
996 |
997 | return this;
998 | }
999 |
1000 | /**
1001 | * 设置文本内容其中文字的点击事件
1002 | * @param text 内容
1003 | * @param startIndex 开始位置
1004 | * @param onClickSpan 点击事件
1005 | */
1006 | public AutoFitColorTextView setTextClick(final String text, int startIndex, final AutoFitColorTextView.OnClickSpan onClickSpan){
1007 | if (getText().length()==0){
1008 | throw new NullPointerException("Please Set The textView Content!");
1009 | }
1010 | SpannableString styledText = new SpannableString(getText());
1011 | styledText.setSpan(new ClickableSpan() {
1012 | @Override
1013 | public void onClick(View widget) {
1014 | onClickSpan.onClick(text);
1015 | }
1016 |
1017 | @Override
1018 | public void updateDrawState(TextPaint ds) {
1019 | super.updateDrawState(ds);
1020 | //设置文本的颜色
1021 | ds.setColor(getTextColors().getDefaultColor());
1022 | //超链接形式的下划线,false 表示不
1023 | ds.setUnderlineText(false);
1024 | }
1025 | }, startIndex, startIndex + text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
1026 | setText(styledText, BufferType.SPANNABLE);
1027 | setMovementMethod(CustomLinkMovementMethod.getInstance());
1028 |
1029 | return this;
1030 | }
1031 |
1032 | public interface OnClickSpan{
1033 | void onClick(String text);
1034 | }
1035 | }
1036 |
--------------------------------------------------------------------------------