-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpannableStringBuilderExtensions.kt
95 lines (76 loc) · 3.34 KB
/
SpannableStringBuilderExtensions.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.tazkiyatech.utils.display
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.BulletSpan
import androidx.annotation.ColorInt
/**
* Appends each [String] in [paragraphs] to this [SpannableStringBuilder]
* and styles each [String] as a bullet point.
*
* @param paragraphs the [Array] of [String]s to append to this [SpannableStringBuilder].
* @param putVerticalSpaceBeforeFirstParagraph whether to place some vertical space before the first paragraph. The amount of space placed is determined by the [verticalSpaceToPutBetweenParagraphs] parameter.
* @param verticalSpaceToPutBetweenParagraphs the vertical space, in pixels, to place between each paragraph. Pass in 0 for no extra space.
* @param horizontalSpaceToPutBetweenBulletPointAndParagraph the horizontal space, in pixels, to place between the bullet point and the paragraph.
* @param bulletPointColor the bullet point color, as a color integer.
* @return this [SpannableStringBuilder].
*/
fun SpannableStringBuilder.appendBulletSpans(
paragraphs: Array<String>,
putVerticalSpaceBeforeFirstParagraph: Boolean,
verticalSpaceToPutBetweenParagraphs: Int,
horizontalSpaceToPutBetweenBulletPointAndParagraph: Int,
@ColorInt bulletPointColor: Int
): SpannableStringBuilder {
var isFirstItem = true
for (paragraph in paragraphs) {
val spaceToPutBeforeBulletSpan =
if (isFirstItem && !putVerticalSpaceBeforeFirstParagraph) 0 else verticalSpaceToPutBetweenParagraphs
appendBulletSpan(
paragraph,
spaceToPutBeforeBulletSpan,
horizontalSpaceToPutBetweenBulletPointAndParagraph,
bulletPointColor
)
isFirstItem = false
}
return this
}
/**
* Appends [paragraph] to this [SpannableStringBuilder] and styles it as a bullet point.
*
* @param paragraph the [String] to append to this [SpannableStringBuilder].
* @param verticalSpaceToPutBeforeParagraph the vertical space, in pixels, to place before the paragraph. Pass in 0 for no extra space.
* @param horizontalSpaceToPutBetweenBulletPointAndParagraph the horizontal space, in pixels, to place between the bullet point and the paragraph.
* @param bulletPointColor the bullet point color, as a color integer.
* @return this [SpannableStringBuilder].
*/
fun SpannableStringBuilder.appendBulletSpan(
paragraph: String,
verticalSpaceToPutBeforeParagraph: Int,
horizontalSpaceToPutBetweenBulletPointAndParagraph: Int,
@ColorInt bulletPointColor: Int
): SpannableStringBuilder {
val absoluteSizeSpan = AbsoluteSizeSpan(verticalSpaceToPutBeforeParagraph)
val lengthBeforeAddingAbsoluteSizeSpan = length
append("\n\n")
val lengthAddingAddingAbsoluteSizeSpan = length
setSpan(
absoluteSizeSpan,
lengthBeforeAddingAbsoluteSizeSpan,
lengthAddingAddingAbsoluteSizeSpan,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
val bulletSpan =
BulletSpan(horizontalSpaceToPutBetweenBulletPointAndParagraph, bulletPointColor)
val lengthBeforeAddingBulletSpan = length
append(paragraph)
val lengthAfterAddingBulletSpan = length
setSpan(
bulletSpan,
lengthBeforeAddingBulletSpan,
lengthAfterAddingBulletSpan,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
return this
}