123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package com.xkl.cdl.util
-
- import android.text.Html
- import android.text.Spanned
- import android.widget.TextView
- import androidx.annotation.ColorInt
- import com.suliang.common.util.ColorUtil
- import com.suliang.common.util.os.ScreenUtil
- import java.util.regex.Pattern
-
- /**
- * author suliang
- * create 2022/4/27 14:49
- * Describe: 进行一些特殊设置
- */
- class ViewUtil {
- companion object {
- /**
- * 生成显示用的音标
- * @param view TextView 用于显示的 textView
- * @param phone_uk String? 英式音标
- * @param phone_us String? 美式音标
- * @return String?
- */
- fun generatePhonetic(view:TextView , phone_uk : String?, phone_us : String?) : String? {
- val maxWidth = ScreenUtil.getScreenWidth() - ScreenUtil.dp2px(48f)
- return when {
- //相等
- phone_uk == phone_us -> phone_uk
- //都不为空
- !phone_uk.isNullOrEmpty() && !phone_us.isNullOrEmpty() -> {
- "英 $phone_uk 美 $phone_us".let {
- if (view.paint.measureText(it) > maxWidth)
- "英 $phone_uk \n 美 $phone_us"
- else it
- }
- }
- !phone_uk.isNullOrEmpty() -> phone_uk
- !phone_us.isNullOrEmpty() -> phone_us
- else -> ""
- }
- }
-
- /**
- * 识字课程认读显示转换: (你)好,括号中的字变大,并设置颜色
- * @param word 处理的文字
- * @param colorId 处理的颜色id color.xml
- * @return Spanned 你好 你为大写
- */
- fun literacyToHtmlWord(word : String, @ColorInt colorId : Int) : Spanned {
- val m = Pattern.compile("\\([\u4e00-\u9fa5]\\)").matcher(word)
- val builder = StringBuffer()
- val color : String = ColorUtil.getHexWebString(colorId)
- var isFind = false
- var previousEndPosition = 0 //下次循环开始的位置
- while (m.find()) {
- isFind = true
- val startPosition = m.start() //左括号的下一位置
- val endPosition = m.end() //右括号位置
- if (startPosition - previousEndPosition > 0) {
- builder.append("<font color=\"#FF323233\">").append(word.substring(previousEndPosition, startPosition))
- .append("</font>")
- }
- //直接匹配添加
- builder.append("<font color= \"").append(color).append("\"><big><big>")
- .append(word.substring(startPosition + 1, endPosition - 1)).append("</big></big></font>")
- previousEndPosition = endPosition
- }
- if (isFind) {
- if (previousEndPosition != word.length) {
- builder.append("<font color=\"#FF323233\">").append(word.substring(previousEndPosition)).append("</font>")
- }
- } else {
- builder.append("<font color=\"").append(color).append("\">").append(word).append("</font>")
- }
- println(builder.toString())
- return Html.fromHtml(builder.toString())
- }
-
- /**
- * 识字课程,获取括号包裹的中文内容 (你)好 --> 你
- * @param word 识字的词语
- * @return 英文括号中的内容
- */
- fun literacyGetWord(word : String) : String {
- val m = Pattern.compile("\\([\u4e00-\u9fa5]\\)").matcher(word)
- return if (m.find()) {
- word.substring(m.start() + 1, m.end() - 1)
- } else word
- }
-
- /** 识字课程,备忘本使用,去掉括号包裹内容的中文括号 */
- fun literacyGetMemoWord(word : String) : String {
- val m = Pattern.compile("\\([\u4e00-\u9fa5]\\)").matcher(word)
- val buffer = StringBuffer()
- while (m.find()) {
- val startPosition = m.start() //左括号的下一位置
- val endPosition = m.end() //右括号位置
- m.appendReplacement(buffer, word.substring(startPosition + 1, endPosition - 1))
- }
- m.appendTail(buffer)
- return buffer.toString()
- }
-
- }
- }
|