Skip to content

Commit

Permalink
FastTextView库新增fastTextView_textBoldFontWeight配置,支持设置粗体大小,优先级高于fastTe…
Browse files Browse the repository at this point in the history
…xtView_textMediumBold
  • Loading branch information
liuzhentao committed Nov 14, 2023
1 parent 9b5f6ed commit b12a06e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ allprojects {
-FastTextView不仅支持支持四个方向drawable的不同Padding和宽高,也支持设置中粗和圆角边框,所有支持的参数如下:
| 参数 | 说明 | 类型 | 默认值 |
| ------ | ------ | ------ | ------ |
| fastTextView_textBoldFontWeight | 设置粗体大小,优先级高于fastTextView_textMediumBold | Float,例如1 | 0 |
| fastTextView_textMediumBold | 设置中粗 | boolean,例如true | false |
| fastTextView_leftImageHeight | 左边方向drawable高度 | dimension,例如10dp | 0 |
| fastTextView_leftImageWidth | 左边方向drawable宽度 | dimension,例如10dp | 0 |
Expand Down
31 changes: 25 additions & 6 deletions text-view/src/main/java/com/arc/fast/view/FastTextView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ open class FastTextView @JvmOverloads constructor(
field = value
if (value) setTextMediumBold() else disableTextMediumBold()
}
var textBoldFontWeight: Float = 0f
set(value) {
field = value
setTextBoldFontWeight(value)
}

/**
* 初始化读取参数
Expand Down Expand Up @@ -112,8 +117,18 @@ open class FastTextView @JvmOverloads constructor(
R.styleable.FastTextView_fastTextView_bottomImagePadding,
0
)
isTextMediumBold =
typedArray.getBoolean(R.styleable.FastTextView_fastTextView_textMediumBold, false)
val fontWeight =
typedArray.getFloat(R.styleable.FastTextView_fastTextView_textBoldFontWeight, 0f)
if (fontWeight > 0f) {
textBoldFontWeight = fontWeight
} else {
isTextMediumBold =
typedArray.getBoolean(
R.styleable.FastTextView_fastTextView_textMediumBold,
false
)
}

} finally {
typedArray.recycle()
}
Expand Down Expand Up @@ -309,14 +324,18 @@ open class FastTextView @JvmOverloads constructor(
/**
* 设置文本字体为中粗
*/
fun TextView.setTextMediumBold() = this.setTextMediumBold(1f)
fun TextView.setTextMediumBold(mediumWeight: Float) {
fun TextView.setTextMediumBold() = this.setTextBoldFontWeight(1f)

/**
* 设置文字粗体
*/
fun TextView.setTextBoldFontWeight(fontWeight: Float) {
paint.style = Paint.Style.FILL_AND_STROKE
paint.strokeWidth = mediumWeight
paint.strokeWidth = fontWeight
invalidate()
}

/**
* 禁用文本字体中粗
*/
fun TextView.disableTextMediumBold() = setTextMediumBold(0f)
fun TextView.disableTextMediumBold() = setTextBoldFontWeight(0f)
1 change: 1 addition & 0 deletions text-view/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<attr name="fastTextView_bottomImageWidth" format="dimension" />
<attr name="fastTextView_bottomImagePadding" format="dimension" />
<attr name="fastTextView_textMediumBold" format="boolean" />
<attr name="fastTextView_textBoldFontWeight" format="float" />
</declare-styleable>
</resources>

0 comments on commit b12a06e

Please sign in to comment.