Skip to content

Commit b831a25

Browse files
authored
Merge pull request #2 from pfieffer/master
Add bold label text feature to the library
2 parents 1bde473 + bae6699 commit b831a25

File tree

3 files changed

+68
-47
lines changed

3 files changed

+68
-47
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ShowMoreLess.Builder(this)
4949
.showMoreLabelColor(R.color.colorPrimaryDark)
5050
.showLessLabelColor(R.color.colorPrimaryDark)
5151
.labelUnderLine(labelUnderLine = false)
52+
.labelBold(labelBold = false)
5253
.expandAnimation(expandAnimation = true)
5354
.textClickable(
5455
textClickableInExpand = true,
@@ -57,7 +58,7 @@ ShowMoreLess.Builder(this)
5758
.build().apply {
5859
addShowMoreLess(textView = tv_first, text = tv_first.text, isContentExpanded = true)
5960
setListener(object : ShowMoreLess.OnShowMoreLessClickedListener {
60-
61+
6162
override fun onShowMoreClicked() {
6263
//We can handle or save show more state
6364
}
@@ -67,10 +68,10 @@ ShowMoreLess.Builder(this)
6768
}
6869
}
6970
)
70-
}
71+
}
7172
```
7273

73-
# Customization
74+
# Customization
7475
## You can customize things like bellow
7576

7677
##### 1. Can change *textLengthAndLengthType* as
@@ -107,13 +108,19 @@ ShowMoreLess.Builder(this)
107108
.labelUnderLine(labelUnderLine = false)//true for underline and false for not underline
108109
```
109110

110-
##### 5. Can easily handle expand and collapse *animation* by
111+
##### 5. Can enable *labelBold* easily as
112+
113+
```Kotlin
114+
.labelBold(labelBold = false)//true for bold and false for normal
115+
```
116+
117+
##### 6. Can easily handle expand and collapse *animation* by
111118

112119
```Kotlin
113120
.expandAnimation(expandAnimation = true)//expandAnimation will affect both expand and collapse logic
114121
```
115122

116-
##### 6. Can handle *textClickable* except clicking more or less txt for expand and collapse
123+
##### 7. Can handle *textClickable* except clicking more or less txt for expand and collapse
117124

118125
```Kotlin
119126
.textClickable(
@@ -122,7 +129,7 @@ ShowMoreLess.Builder(this)
122129
)
123130
```
124131

125-
##### 7. *addShowMoreLess()* function will handle text expand or collapse state
132+
##### 8. *addShowMoreLess()* function will handle text expand or collapse state
126133

127134
```Kotlin
128135
addShowMoreLess(
@@ -132,7 +139,7 @@ addShowMoreLess(
132139
)
133140
```
134141

135-
##### 8. At last *setListener()* function for callbacks
142+
##### 9. At last *setListener()* function for callbacks
136143

137144
```Kotlin
138145
setListener(object : ShowMoreLess.OnShowMoreLessClickedListener {

app/src/main/java/com/noowenz/showmoreless/MainActivity.kt

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,36 @@ class MainActivity : AppCompatActivity() {
1616

1717
private fun initViews() {
1818
ShowMoreLess.Builder(this)
19-
/*.textLengthAndLengthType(
20-
length = 100,
21-
textLengthType = ShowMoreLess.TYPE_CHARACTER
22-
)*/
23-
.textLengthAndLengthType(
24-
length = 5,
25-
textLengthType = ShowMoreLess.TYPE_LINE
26-
)
27-
.showMoreLabel("show more")
28-
.showLessLabel("show less")
29-
.showMoreLabelColor(Color.parseColor("#FF0000"))
30-
.showLessLabelColor(Color.parseColor("#FF0000"))
31-
.labelUnderLine(labelUnderLine = true)
32-
.expandAnimation(expandAnimation = true)
33-
.textClickable(
34-
textClickableInExpand = true,
35-
textClickableInCollapse = true
36-
)
37-
.build().apply {
38-
addShowMoreLess(textView = tv_first, text = tv_first.text, isContentExpanded = false)
39-
setListener(object : ShowMoreLess.OnShowMoreLessClickedListener {
40-
override fun onShowMoreClicked() {
41-
//We can handle or save show more state
42-
}
19+
/*.textLengthAndLengthType(
20+
length = 100,
21+
textLengthType = ShowMoreLess.TYPE_CHARACTER
22+
)*/
23+
.textLengthAndLengthType(
24+
length = 5,
25+
textLengthType = ShowMoreLess.TYPE_LINE
26+
)
27+
.showMoreLabel("show more")
28+
.showLessLabel("show less")
29+
.showMoreLabelColor(Color.parseColor("#FF0000"))
30+
.showLessLabelColor(Color.parseColor("#FF0000"))
31+
.labelUnderLine(labelUnderLine = true)
32+
.labelBold(labelBold = true)
33+
.expandAnimation(expandAnimation = true)
34+
.textClickable(
35+
textClickableInExpand = true,
36+
textClickableInCollapse = true
37+
)
38+
.build().apply {
39+
addShowMoreLess(textView = tv_first, text = tv_first.text, isContentExpanded = false)
40+
setListener(object : ShowMoreLess.OnShowMoreLessClickedListener {
41+
override fun onShowMoreClicked() {
42+
//We can handle or save show more state
43+
}
4344

44-
override fun onShowLessClicked() {
45-
//We can handle or save show less state
46-
}
47-
})
48-
}
45+
override fun onShowLessClicked() {
46+
//We can handle or save show less state
47+
}
48+
})
49+
}
4950
}
5051
}

showmoreless/src/main/java/com/noowenz/showmoreless/ShowMoreLess.kt

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class ShowMoreLess private constructor(builder: Builder) {
4141
private val moreLabelColor: Int
4242
private val lessLabelColor: Int
4343
private val labelUnderLine: Boolean
44+
private val labelBold: Boolean
4445
private val expandAnimation: Boolean
4546
private val textClickableInExpand: Boolean
4647
private val textClickableInCollapse: Boolean
@@ -54,15 +55,16 @@ class ShowMoreLess private constructor(builder: Builder) {
5455
this.moreLabelColor = builder.moreLabelColor
5556
this.lessLabelColor = builder.lessLabelColor
5657
this.labelUnderLine = builder.labelUnderLine
58+
this.labelBold = builder.labelBold
5759
this.expandAnimation = builder.expandAnimation
5860
this.textClickableInExpand = builder.textClickableInExpand
5961
this.textClickableInCollapse = builder.textClickableInCollapse
6062
}
6163

6264
fun addShowMoreLess(
63-
textView: TextView,
64-
text: CharSequence,
65-
isContentExpanded: Boolean
65+
textView: TextView,
66+
text: CharSequence,
67+
isContentExpanded: Boolean
6668
) {
6769
if (textLengthType == TYPE_CHARACTER) {
6870
if (text.length <= textLength) {
@@ -114,16 +116,16 @@ class ShowMoreLess private constructor(builder: Builder) {
114116
}
115117

116118
private fun addShowMore(
117-
textView: TextView,
118-
trimText: CharSequence
119+
textView: TextView,
120+
trimText: CharSequence
119121
) {
120122
try {
121123
val newSubString: CharSequence
122124
if (textLengthType == TYPE_LINE) {
123125
val lp = textView.layoutParams as ViewGroup.MarginLayoutParams
124126
val subString = trimText.substring(
125-
startIndex = textView.layout.getLineStart(0),
126-
endIndex = textView.layout.getLineEnd(textLength - 1)
127+
startIndex = textView.layout.getLineStart(0),
128+
endIndex = textView.layout.getLineEnd(textLength - 1)
127129
)
128130
newSubString = if (!subString.endsWith("\n", false)) {
129131
val startRange = subString.length - (moreLabel.length + 4 + lp.rightMargin / 6)
@@ -140,7 +142,7 @@ class ShowMoreLess private constructor(builder: Builder) {
140142
newSubString = trimText.subSequence(0, textLength)
141143
}
142144
val spannableStringBuilder = SpannableStringBuilder(
143-
newSubString
145+
newSubString
144146
).apply {
145147
this.append("...")
146148
this.append(moreLabel)
@@ -156,6 +158,7 @@ class ShowMoreLess private constructor(builder: Builder) {
156158
override fun updateDrawState(ds: TextPaint) {
157159
super.updateDrawState(ds)
158160
ds.isUnderlineText = labelUnderLine
161+
ds.isFakeBoldText = labelBold
159162
ds.color = moreLabelColor
160163
}
161164
}
@@ -205,8 +208,8 @@ class ShowMoreLess private constructor(builder: Builder) {
205208
}
206209

207210
private fun addShowLess(
208-
textView: TextView,
209-
trimText: CharSequence
211+
textView: TextView,
212+
trimText: CharSequence
210213
) {
211214
try {
212215
textView.maxLines = Integer.MAX_VALUE
@@ -229,6 +232,7 @@ class ShowMoreLess private constructor(builder: Builder) {
229232
override fun updateDrawState(ds: TextPaint) {
230233
super.updateDrawState(ds)
231234
ds.isUnderlineText = labelUnderLine
235+
ds.isFakeBoldText = labelBold
232236
ds.color = lessLabelColor
233237
}
234238
}
@@ -313,7 +317,7 @@ class ShowMoreLess private constructor(builder: Builder) {
313317
}
314318

315319
class Builder(// required
316-
val context: Context) {
320+
val context: Context) {
317321
// optional
318322
var textLength = 100
319323
var textLengthType = TYPE_CHARACTER
@@ -322,6 +326,7 @@ class ShowMoreLess private constructor(builder: Builder) {
322326
var moreLabelColor = Color.parseColor("#ffffff")
323327
var lessLabelColor = Color.parseColor("#ffffff")
324328
var labelUnderLine = false
329+
var labelBold = false
325330
var expandAnimation = false
326331
var textClickableInExpand = false
327332
var textClickableInCollapse = false
@@ -380,6 +385,14 @@ class ShowMoreLess private constructor(builder: Builder) {
380385
return this
381386
}
382387

388+
/**
389+
* @param labelBold is boolean to enable or disable bold label text
390+
*/
391+
fun labelBold(labelBold: Boolean): Builder {
392+
this.labelBold = labelBold
393+
return this
394+
}
395+
383396
/**
384397
* @param textClickableInCollapse for text collapse condition
385398
* @param textClickableInExpand for text expand condition

0 commit comments

Comments
 (0)