-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from POLZZAK/feat/easy-spannable
[FEAT] SpannableBuilder 클래스 구현
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
app/src/main/java/com/polzzak_android/presentation/common/util/SpannableBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.polzzak_android.presentation.common.util | ||
|
||
import android.content.Context | ||
import android.text.Spannable | ||
import android.text.SpannableStringBuilder | ||
import android.text.style.BackgroundColorSpan | ||
import android.text.style.ForegroundColorSpan | ||
import android.text.style.TextAppearanceSpan | ||
import android.text.style.UnderlineSpan | ||
import androidx.annotation.ColorRes | ||
import androidx.annotation.StyleRes | ||
import androidx.core.text.set | ||
import androidx.core.text.toSpannable | ||
|
||
/** | ||
* [Spannable]을 쉽게 만들 수 있게 해주는 유틸 클래스. | ||
* | ||
* 사용 예: | ||
* ``` | ||
* val spannable = SpannableBuilder.build(context) { | ||
* span(text = "안녕", textColor = R.color.Red, style = R.style.title) | ||
* span(text = "하세요", textColor = R.color.Bule, underline = true) | ||
* } | ||
* ``` | ||
*/ | ||
class SpannableBuilder private constructor( | ||
private val context: Context, | ||
private val spannableBuilder: SpannableStringBuilder = SpannableStringBuilder() | ||
) { | ||
fun span( | ||
text: String, | ||
@StyleRes style: Int = -1, | ||
@ColorRes textColor: Int = -1, | ||
@ColorRes backgroundColor: Int = -1, | ||
underline: Boolean = false | ||
) { | ||
val startIndex = spannableBuilder.length | ||
val endIndex = startIndex + text.length | ||
|
||
spannableBuilder.append(text) | ||
|
||
if (style != -1) { | ||
spannableBuilder[startIndex..endIndex] = TextAppearanceSpan(context, style) | ||
} | ||
if (textColor != -1) { | ||
spannableBuilder[startIndex..endIndex] = ForegroundColorSpan(context.getColor(textColor)) | ||
} | ||
if (backgroundColor != -1) { | ||
spannableBuilder[startIndex..endIndex] = BackgroundColorSpan(context.getColor(backgroundColor)) | ||
} | ||
if (underline) { | ||
spannableBuilder[startIndex..endIndex] = UnderlineSpan() | ||
} | ||
} | ||
|
||
companion object { | ||
fun build(context: Context, block: SpannableBuilder.() -> Unit): Spannable { | ||
return SpannableBuilder(context) | ||
.apply { block() } | ||
.spannableBuilder.toSpannable() | ||
} | ||
} | ||
} |