学考乐离线App
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ViewUtil.kt 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.xkl.cdl.util
  2. import android.text.Html
  3. import android.text.Spanned
  4. import android.widget.TextView
  5. import androidx.annotation.ColorInt
  6. import com.suliang.common.util.ColorUtil
  7. import com.suliang.common.util.os.ScreenUtil
  8. import java.util.regex.Pattern
  9. /**
  10. * author suliang
  11. * create 2022/4/27 14:49
  12. * Describe: 进行一些特殊设置
  13. */
  14. class ViewUtil {
  15. companion object {
  16. /**
  17. * 生成显示用的音标
  18. * @param view TextView 用于显示的 textView
  19. * @param phone_uk String? 英式音标
  20. * @param phone_us String? 美式音标
  21. * @return String?
  22. */
  23. fun generatePhonetic(view:TextView , phone_uk : String?, phone_us : String?) : String? {
  24. val maxWidth = ScreenUtil.getScreenWidth() - ScreenUtil.dp2px(48f)
  25. return when {
  26. //相等
  27. phone_uk == phone_us -> phone_uk
  28. //都不为空
  29. !phone_uk.isNullOrEmpty() && !phone_us.isNullOrEmpty() -> {
  30. "英 $phone_uk 美 $phone_us".let {
  31. if (view.paint.measureText(it) > maxWidth)
  32. "英 $phone_uk \n 美 $phone_us"
  33. else it
  34. }
  35. }
  36. !phone_uk.isNullOrEmpty() -> phone_uk
  37. !phone_us.isNullOrEmpty() -> phone_us
  38. else -> ""
  39. }
  40. }
  41. /**
  42. * 识字课程认读显示转换: (你)好,括号中的字变大,并设置颜色
  43. * @param word 处理的文字
  44. * @param colorId 处理的颜色id color.xml
  45. * @return Spanned 你好 你为大写
  46. */
  47. fun literacyToHtmlWord(word : String, @ColorInt colorId : Int) : Spanned {
  48. val m = Pattern.compile("\\([\u4e00-\u9fa5]\\)").matcher(word)
  49. val builder = StringBuffer()
  50. val color : String = ColorUtil.getHexWebString(colorId)
  51. var isFind = false
  52. var previousEndPosition = 0 //下次循环开始的位置
  53. while (m.find()) {
  54. isFind = true
  55. val startPosition = m.start() //左括号的下一位置
  56. val endPosition = m.end() //右括号位置
  57. if (startPosition - previousEndPosition > 0) {
  58. builder.append("<font color=\"#FF323233\">").append(word.substring(previousEndPosition, startPosition))
  59. .append("</font>")
  60. }
  61. //直接匹配添加
  62. builder.append("<font color= \"").append(color).append("\"><big><big>")
  63. .append(word.substring(startPosition + 1, endPosition - 1)).append("</big></big></font>")
  64. previousEndPosition = endPosition
  65. }
  66. if (isFind) {
  67. if (previousEndPosition != word.length) {
  68. builder.append("<font color=\"#FF323233\">").append(word.substring(previousEndPosition)).append("</font>")
  69. }
  70. } else {
  71. builder.append("<font color=\"").append(color).append("\">").append(word).append("</font>")
  72. }
  73. println(builder.toString())
  74. return Html.fromHtml(builder.toString())
  75. }
  76. /**
  77. * 识字课程,获取括号包裹的中文内容 (你)好 --> 你
  78. * @param word 识字的词语
  79. * @return 英文括号中的内容
  80. */
  81. fun literacyGetWord(word : String) : String {
  82. val m = Pattern.compile("\\([\u4e00-\u9fa5]\\)").matcher(word)
  83. return if (m.find()) {
  84. word.substring(m.start() + 1, m.end() - 1)
  85. } else word
  86. }
  87. /** 识字课程,备忘本使用,去掉括号包裹内容的中文括号 */
  88. fun literacyGetMemoWord(word : String) : String {
  89. val m = Pattern.compile("\\([\u4e00-\u9fa5]\\)").matcher(word)
  90. val buffer = StringBuffer()
  91. while (m.find()) {
  92. val startPosition = m.start() //左括号的下一位置
  93. val endPosition = m.end() //右括号位置
  94. m.appendReplacement(buffer, word.substring(startPosition + 1, endPosition - 1))
  95. }
  96. m.appendTail(buffer)
  97. return buffer.toString()
  98. }
  99. }
  100. }