|
| 1 | +# FormatTextViewLib |
| 2 | +[](https://jitpack.io/#FlyJingFish/FormatTextViewLib) |
| 3 | + |
| 4 | +## 多数app登陆首页都需要显示用户协议和隐私政策并且需要能够点击,遇到需要翻译多个国家语言的,多个TextView拼接会导致语序不对,而且换行也是个问题 |
| 5 | + |
| 6 | +# 特色功能 |
| 7 | + |
| 8 | +## FormatTextView 功能介绍 |
| 9 | + |
| 10 | +**1、支持字体设置字体颜色,加粗,斜体,下划线,删除线,字体大小** |
| 11 | + |
| 12 | +**2、支持下划线支持设置线宽,距离文字距离,下划线颜色** |
| 13 | + |
| 14 | +**3、支持删除线支持设置线宽,删除线颜色** |
| 15 | + |
| 16 | +**4、支持设置图片,大小,左右距离,加载本地、网络图片** |
| 17 | + |
| 18 | +**5、支持给每个位置的富文本添加点击事件** |
| 19 | + |
| 20 | +**6、支持给每个位置的富文本设置背景色** |
| 21 | + |
| 22 | +## HtmlTextView 功能介绍 |
| 23 | + |
| 24 | +**1、支持加载网络图片** |
| 25 | + |
| 26 | +**2、支持为存在链接的标签添加点击事件** |
| 27 | + |
| 28 | +<img src="https://github.com/FlyJingFish/FormatTextViewLib/blob/master/screenshot/Screenshot_20220908_184829.jpg" width="405px" height="842px" alt="show" /> |
| 29 | + |
| 30 | + |
| 31 | +## 第一步,根目录build.gradle |
| 32 | + |
| 33 | +```gradle |
| 34 | + allprojects { |
| 35 | + repositories { |
| 36 | + ... |
| 37 | + maven { url 'https://jitpack.io' } |
| 38 | + } |
| 39 | + } |
| 40 | +``` |
| 41 | +## 第二步,需要引用的build.gradle (最新版本[](https://jitpack.io/#FlyJingFish/FormatTextViewLib)) |
| 42 | + |
| 43 | +```gradle |
| 44 | + dependencies { |
| 45 | + implementation 'com.github.FlyJingFish:FormatTextViewLib:2.1.7' |
| 46 | + } |
| 47 | +``` |
| 48 | +## 第三步,使用说明 |
| 49 | + |
| 50 | +## 一、FormatTextView 使用说明 |
| 51 | + |
| 52 | +### Kotlin调用示例 |
| 53 | + |
| 54 | +```kotlin |
| 55 | + //如果包含网络图片必须先设置以下方法 |
| 56 | + textView.setOnInflateImageListener(object : FormatTextView.OnInflateImageListener { |
| 57 | + override fun onInflate( |
| 58 | + formatImage: FormatImage?, |
| 59 | + drawableListener: FormatTextView.OnReturnDrawableListener? |
| 60 | + ) { |
| 61 | + val requestBuilder: RequestBuilder<Drawable> = |
| 62 | + Glide.with(this@MainActivity).asDrawable().load( |
| 63 | + formatImage!!.imageUrlValue |
| 64 | + ) |
| 65 | + if (formatImage.width > 0 && formatImage.height > 0) { |
| 66 | + requestBuilder.apply( |
| 67 | + RequestOptions().override( |
| 68 | + formatImage.width.toInt(), |
| 69 | + formatImage.height.toInt() |
| 70 | + ).centerCrop() |
| 71 | + ) |
| 72 | + } |
| 73 | + requestBuilder.into(object : CustomTarget<Drawable?>() { |
| 74 | + override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable?>?) { |
| 75 | + drawableListener?.onReturnDrawable(resource) |
| 76 | + } |
| 77 | + |
| 78 | + override fun onLoadCleared(placeholder: Drawable?) {} |
| 79 | + }) |
| 80 | + } |
| 81 | +}) |
| 82 | + //设置数据 |
| 83 | + textView.setFormatTextBean("%1\$s欢迎欢迎欢迎欢迎欢迎欢迎%3\$s欢迎欢迎欢迎%2\$s", |
| 84 | + FormatText().apply { |
| 85 | + textSize = 30f |
| 86 | + textColor = R.color.colorAccent |
| 87 | + bold = false |
| 88 | + italic = true |
| 89 | + underline = true |
| 90 | + underlineColor = R.color.color_red |
| 91 | + underlineMarginTop = 10f |
| 92 | + underlineWidth = 2f |
| 93 | + resValue = R.string.we |
| 94 | + }, |
| 95 | + FormatImage().apply { |
| 96 | + imagePlaceHolder = R.mipmap.ic_launcher_round |
| 97 | + imageUrlValue = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp0.itc.cn%2Fq_70%2Fimages03%2F20210227%2F6687c969b58d486fa2f23d8488b96ae4.jpeg&refer=http%3A%2F%2Fp0.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661701773&t=19043990158a1d11c2a334146020e2ce" |
| 98 | + verticalAlignment = FormatImage.ALIGN_CENTER |
| 99 | + width = 25f |
| 100 | + height = 25f |
| 101 | + marginStart = 10f |
| 102 | + marginEnd = 10f |
| 103 | + }, |
| 104 | + FormatImage().apply { |
| 105 | + imagePlaceHolder = R.mipmap.ic_launcher_round |
| 106 | + imageUrlValue = "https://pics4.baidu.com/feed/50da81cb39dbb6fd95aa0c599b8d0d1e962b3708.jpeg?token=bf17224f51a6f4bb389e787f9c487940" |
| 107 | + verticalAlignment = FormatImage.ALIGN_CENTER |
| 108 | + width = 25f |
| 109 | + height = 25f |
| 110 | + marginStart = 10f |
| 111 | + marginEnd = 10f |
| 112 | + }) |
| 113 | +// 设置点击的监听 |
| 114 | + textView.setOnFormatClickListener(object : OnFormatClickListener{ |
| 115 | + override fun onLabelClick(position: Int) {//position就是设置数据的顺序 |
| 116 | + Toast.makeText(this@MainActivity,"onItemClick-item"+position,Toast.LENGTH_SHORT).show() |
| 117 | + } |
| 118 | + }) |
| 119 | +``` |
| 120 | + |
| 121 | +### Java调用示例 |
| 122 | + |
| 123 | +```java |
| 124 | +textView.setOnInflateImageListener(new FormatTextView.OnInflateImageListener() { |
| 125 | + @Override |
| 126 | + public void onInflate(FormatImage formatImage, final FormatTextView.OnReturnDrawableListener drawableListener) { |
| 127 | + RequestBuilder<Drawable> requestBuilder = Glide.with(SecondActivity.this).asDrawable().load(formatImage.imageUrlValue); |
| 128 | + if (formatImage.width > 0 && formatImage.height > 0) { |
| 129 | + requestBuilder.apply(new RequestOptions().override(((int) formatImage.width), ((int) formatImage.height)).centerCrop()); |
| 130 | + } |
| 131 | + requestBuilder.into(new CustomTarget<Drawable>() { |
| 132 | + @Override |
| 133 | + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) { |
| 134 | + drawableListener.onReturnDrawable(resource); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void onLoadCleared(@Nullable Drawable placeholder) { |
| 139 | + |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | +}); |
| 144 | +textView.setFormatTextBean("%1$s欢迎欢迎欢迎欢迎欢迎欢迎欢迎%3$s欢迎欢迎欢迎%2$s", |
| 145 | + new FormatText().setTextColor(R.color.colorAccent).setBold(false) |
| 146 | + .setUnderlineColor(R.color.color_red).setUnderlineMarginTop(10f).setUnderlineWidth(2f) |
| 147 | + .setUnderline(true).setItalic(true).setResValue(R.string.we), |
| 148 | + new FormatImage().setImagePlaceHolder(R.mipmap.ic_launcher_round) |
| 149 | + .setImageUrlValue("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp0.itc.cn%2Fq_70%2Fimages03%2F20210227%2F6687c969b58d486fa2f23d8488b96ae4.jpeg&refer=http%3A%2F%2Fp0.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661701773&t=19043990158a1d11c2a334146020e2ce") |
| 150 | + .setVerticalAlignment(FormatImage.ALIGN_CENTER) |
| 151 | + .setWidth(30f) |
| 152 | + .setHeight(40f) |
| 153 | + .setMarginStart(20f) |
| 154 | + .setMarginEnd(20f), |
| 155 | + new FormatImage().setImagePlaceHolder(R.mipmap.ic_launcher_round) |
| 156 | + .setImageUrlValue("https://pics4.baidu.com/feed/50da81cb39dbb6fd95aa0c599b8d0d1e962b3708.jpeg?token=bf17224f51a6f4bb389e787f9c487940") |
| 157 | + .setVerticalAlignment(FormatImage.ALIGN_CENTER) |
| 158 | + .setWidth(30f) |
| 159 | + .setHeight(40f) |
| 160 | + .setMarginStart(20f) |
| 161 | + .setMarginEnd(20f)); |
| 162 | +``` |
| 163 | + |
| 164 | +### 下划线常见问题 |
| 165 | +如果您设置了下划线以下样式: |
| 166 | + |
| 167 | +**underlineColor、underlineMarginTop、underlineWidth** |
| 168 | + |
| 169 | +那么下划线将采用绘制下划线方案,**underlineMarginTop**不设置(即为0)绘制在默认的删除线位置,如果设置数值过大并且文本长到换行,则需要设置行间距,否则会出现下滑线压在下一行的问题,你可通过设置lineSpacingExtra或lineSpacingMultiplier来解决问题 |
| 170 | + |
| 171 | +### verticalAlignment 常见问题 |
| 172 | + |
| 173 | +ALIGN_CENTER 为当前库新增对齐方式旨解决在小图标和文本中心对齐问题,在图片设置超过行高时将会出现裁剪问题,如果您图片很大还是建议使用ALIGN_BASELINE |
| 174 | + |
| 175 | +## FormatText 参数一览 |
| 176 | +|属性|参数类型|描述| |
| 177 | +|---|:---:|:---:| |
| 178 | +|textColor|@ColorRes int|文字资源颜色Id| |
| 179 | +|bold|boolean|文字是否加粗| |
| 180 | +|italic|boolean|文字是否斜体| |
| 181 | +|strValue|String|文字String类型值| |
| 182 | +|resValue|@StringRes int|文字文本资源Id| |
| 183 | +|textSize|float|文字字体大小(单位:SP)| |
| 184 | +|underline|boolean|文字是否下划线| |
| 185 | +|underlineColor|@ColorRes int|文字下划线颜色| |
| 186 | +|underlineWidth|float|文字下划线线宽| |
| 187 | +|underlineMarginTop|float|文字下划线向下偏移的距离| |
| 188 | +|deleteLine|boolean|文字是否删除线| |
| 189 | +|deleteLineColor|@ColorRes int|文字删除线颜色| |
| 190 | +|deleteLineWidth|float|文字删除线线宽| |
| 191 | +|backgroundColor|@ColorRes int|文字区域背景色| |
| 192 | + |
| 193 | +## FormatImage 参数一览 |
| 194 | +|属性|参数类型|描述| |
| 195 | +|---|:---:|:---:| |
| 196 | +|imageUrlValue|String|网络图片Url| |
| 197 | +|imageResValue|@DrawableRes int|本地图片资源Id| |
| 198 | +|imagePlaceHolder|@DrawableRes int|网络图片加载时图片资源Id| |
| 199 | +|width|float|图片宽度(单位:DP)| |
| 200 | +|height|float|图片高度(单位:DP)| |
| 201 | +|verticalAlignment|int|图片对齐方式(ALIGN_BASELINE/ALIGN_CENTER/ALIGN_BOTTOM)| |
| 202 | +|marginLeft|float|图片距离左侧距离(单位:DP)| |
| 203 | +|marginRight|float|图片距离右侧距离(单位:DP)| |
| 204 | +|marginStart|float|图片距离左侧(Rtl:右侧)距离(单位:DP)| |
| 205 | +|marginEnd|float|图片距离右侧(Rtl:左侧)距离(单位:DP)| |
| 206 | +|backgroundColor|@ColorRes int|图片区域背景色| |
| 207 | + |
| 208 | +## 二、HtmlTextView 使用说明 |
| 209 | + |
| 210 | +### Kotlin调用示例 |
| 211 | + |
| 212 | +```kotlin |
| 213 | + //如果包含网络图片必须先设置以下方法 |
| 214 | + text7.setOnInflateImageListener(object : HtmlTextView.OnInflateImageListener{ |
| 215 | + override fun onInflate( |
| 216 | + source: String?, |
| 217 | + drawableListener: HtmlTextView.OnReturnDrawableListener? |
| 218 | + ) { |
| 219 | + val requestBuilder: RequestBuilder<Drawable> = |
| 220 | + Glide.with(this@MainActivity).asDrawable().load( |
| 221 | + source |
| 222 | + ) |
| 223 | + requestBuilder.into(object : CustomTarget<Drawable?>() { |
| 224 | + override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable?>?) { |
| 225 | + drawableListener?.onReturnDrawable(resource) |
| 226 | + } |
| 227 | + |
| 228 | + override fun onLoadCleared(placeholder: Drawable?) {} |
| 229 | + }) |
| 230 | + } |
| 231 | +}) |
| 232 | +text7.setHtmlText("哈哈哈<a>lala</a>啦啦<a href=\"haha\">haha</a>哈哈哈<img src=\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp0.itc.cn%2Fq_70%2Fimages03%2F20210227%2F6687c969b58d486fa2f23d8488b96ae4.jpeg&refer=http%3A%2F%2Fp0.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661701773&t=19043990158a1d11c2a334146020e2ce\"></img>", |
| 233 | + HtmlImage().apply |
| 234 | + { |
| 235 | + maxWidth = 160f |
| 236 | + maxHeight = 160f |
| 237 | + verticalAlignment = ImageSpan.ALIGN_BASELINE |
| 238 | + imagePlaceHolder = R.mipmap.ic_launcher |
| 239 | + } |
| 240 | +) |
| 241 | +``` |
| 242 | + |
| 243 | +### Java调用示例 |
| 244 | + |
| 245 | +```java |
| 246 | +text7.setOnInflateImageListener(new HtmlTextView.OnInflateImageListener() { |
| 247 | + @Override |
| 248 | + public void onInflate(@Nullable String source, @Nullable final HtmlTextView.OnReturnDrawableListener drawableListener) { |
| 249 | + RequestBuilder<Drawable> requestBuilder = Glide.with(SecondActivity.this).asDrawable().load(source); |
| 250 | + requestBuilder.into(new CustomTarget<Drawable>() { |
| 251 | + @Override |
| 252 | + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) { |
| 253 | + drawableListener.onReturnDrawable(resource); |
| 254 | + } |
| 255 | + |
| 256 | + @Override |
| 257 | + public void onLoadCleared(@Nullable Drawable placeholder) { |
| 258 | + |
| 259 | + } |
| 260 | + }); |
| 261 | + } |
| 262 | +}); |
| 263 | + |
| 264 | +text7.setHtmlText("哈哈哈<a>lala</a>啦啦<a href=\"haha\">haha</a>哈哈哈<img src=\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp0.itc.cn%2Fq_70%2Fimages03%2F20210227%2F6687c969b58d486fa2f23d8488b96ae4.jpeg&refer=http%3A%2F%2Fp0.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661701773&t=19043990158a1d11c2a334146020e2ce\"></img>", |
| 265 | + new HtmlImage().setMaxHeight(100).setMaxWidth(100)); |
| 266 | +``` |
| 267 | + |
| 268 | +## HtmlImage 参数一览 |
| 269 | +|属性|参数类型|描述| |
| 270 | +|---|:---:|:---:| |
| 271 | +|imagePlaceHolder|@DrawableRes int|网络图片加载时图片资源Id| |
| 272 | +|maxWidth|float|图片宽度(单位:DP)| |
| 273 | +|maxHeight|float|图片高度(单位:DP)| |
| 274 | +|verticalAlignment|int|图片对齐方式(ALIGN_BASELINE/ALIGN_CENTER/ALIGN_BOTTOM)| |
| 275 | + |
| 276 | +# 我的更多开源库推荐 |
| 277 | + |
| 278 | +支持不操作Bitmap的圆图或圆角图,可绘制圆环背景边框或圆角框背景边框,除ImageView自带属性外新增4种显示模式;另外更有可绘制任意图形的图片只有你想不到,没有它做不到 |
| 279 | + |
| 280 | +- [ShapeImageView](https://github.com/FlyJingFish/ShapeImageView) |
| 281 | + |
| 282 | +支持点击小图查看大图的动画打开效果的图片查看器 |
| 283 | + |
| 284 | +- [OpenImage](https://github.com/FlyJingFish/OpenImage)(已内置ShapeImageView) |
| 285 | + |
0 commit comments