Created
September 19, 2018 14:23
-
-
Save alorma/24c61a5ef9be3d5b615456fdb20bf54a to your computer and use it in GitHub Desktop.
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
import android.graphics.Typeface.BOLD | |
import android.graphics.Typeface.ITALIC | |
import android.support.annotation.ColorInt | |
import android.text.Spannable.SPAN_INCLUSIVE_EXCLUSIVE | |
import android.text.SpannableStringBuilder | |
import android.text.SpannedString | |
import android.text.style.* | |
fun spannable(block: SpannableStringBuilder.() -> Unit): SpannableStringBuilder = SpannableStringBuilder().apply(block) | |
fun SpannableStringBuilder.appendSpace(): SpannableStringBuilder = append(" ") | |
fun SpannableStringBuilder.appendWithSpace(charSequence: CharSequence): SpannableStringBuilder { | |
append(charSequence) | |
appendSpace() | |
return this | |
} | |
/** | |
* Builds new string by populating a newly created [SpannableStringBuilder] using the provided | |
* [builderAction] and then converting it to [SpannedString]. | |
*/ | |
inline fun buildSpannedString(builderAction: SpannableStringBuilder.() -> Unit): SpannedString { | |
val builder = SpannableStringBuilder() | |
builder.builderAction() | |
return SpannedString(builder) | |
} | |
/** | |
* Wrap appended text in [builderAction] in [spans]. | |
* | |
* Note: the spans will only have the correct position if the [builderAction] only appends or | |
* replaces text. Inserting, deleting, or clearing the text will cause the span to be placed at | |
* an incorrect position. | |
*/ | |
inline fun SpannableStringBuilder.inSpans( | |
vararg spans: Any, | |
builderAction: SpannableStringBuilder.() -> Unit | |
): SpannableStringBuilder { | |
val start = length | |
builderAction() | |
for (span in spans) setSpan(span, start, length, SPAN_INCLUSIVE_EXCLUSIVE) | |
return this | |
} | |
/** | |
* Wrap appended text in [builderAction] in [span]. | |
* | |
* Note: the span will only have the correct position if the `builderAction` only appends or | |
* replaces text. Inserting, deleting, or clearing the text will cause the span to be placed at | |
* an incorrect position. | |
*/ | |
inline fun SpannableStringBuilder.inSpans( | |
span: Any, | |
builderAction: SpannableStringBuilder.() -> Unit | |
): SpannableStringBuilder { | |
val start = length | |
builderAction() | |
setSpan(span, start, length, SPAN_INCLUSIVE_EXCLUSIVE) | |
return this | |
} | |
/** | |
* Wrap appended text in [builderAction] in a @Style [TextAppearanceSpan]. | |
* | |
* @see SpannableStringBuilder.inSpans | |
*/ | |
inline fun SpannableStringBuilder.style( | |
styleSpan: Any, | |
builderAction: SpannableStringBuilder.() -> Unit) = | |
inSpans(styleSpan, builderAction = builderAction) | |
/** | |
* Wrap appended text in [builderAction] in a bold [StyleSpan]. | |
* | |
* @see SpannableStringBuilder.inSpans | |
*/ | |
inline fun SpannableStringBuilder.bold(builderAction: SpannableStringBuilder.() -> Unit) = | |
inSpans(StyleSpan(BOLD), builderAction = builderAction) | |
/** | |
* Wrap appended text in [builderAction] in an italic [StyleSpan]. | |
* | |
* @see SpannableStringBuilder.inSpans | |
*/ | |
inline fun SpannableStringBuilder.italic(builderAction: SpannableStringBuilder.() -> Unit) = | |
inSpans(StyleSpan(ITALIC), builderAction = builderAction) | |
/** | |
* Wrap appended text in [builderAction] in an [UnderlineSpan]. | |
* | |
* @see SpannableStringBuilder.inSpans | |
*/ | |
inline fun SpannableStringBuilder.underline(builderAction: SpannableStringBuilder.() -> Unit) = | |
inSpans(UnderlineSpan(), builderAction = builderAction) | |
/** | |
* Wrap appended text in [builderAction] in a [ForegroundColorSpan]. | |
* | |
* @see SpannableStringBuilder.inSpans | |
*/ | |
inline fun SpannableStringBuilder.color( | |
@ColorInt color: Int, | |
builderAction: SpannableStringBuilder.() -> Unit | |
) = inSpans(ForegroundColorSpan(color), builderAction = builderAction) | |
/** | |
* Wrap appended text in [builderAction] in a [BackgroundColorSpan]. | |
* | |
* @see SpannableStringBuilder.inSpans | |
*/ | |
inline fun SpannableStringBuilder.backgroundColor( | |
@ColorInt color: Int, | |
builderAction: SpannableStringBuilder.() -> Unit | |
) = inSpans(BackgroundColorSpan(color), builderAction = builderAction) | |
/** | |
* Wrap appended text in [builderAction] in a [StrikethroughSpan]. | |
* | |
* @see SpannableStringBuilder.inSpans | |
*/ | |
inline fun SpannableStringBuilder.strikeThrough(builderAction: SpannableStringBuilder.() -> Unit) = | |
inSpans(StrikethroughSpan(), builderAction = builderAction) | |
/** | |
* Wrap appended text in [builderAction] in a [RelativeSizeSpan]. | |
* | |
* @see SpannableStringBuilder.inSpans | |
*/ | |
inline fun SpannableStringBuilder.scale( | |
proportion: Float, | |
builderAction: SpannableStringBuilder.() -> Unit | |
) = inSpans(RelativeSizeSpan(proportion), builderAction = builderAction) | |
/** | |
* Wrap appended text in [builderAction] in a [SuperscriptSpan]. | |
* | |
* @see SpannableStringBuilder.inSpans | |
*/ | |
inline fun SpannableStringBuilder.superscript(builderAction: SpannableStringBuilder.() -> Unit) = | |
inSpans(SuperscriptSpan(), builderAction = builderAction) | |
/** | |
* Wrap appended text in [builderAction] in a [SubscriptSpan]. | |
* | |
* @see SpannableStringBuilder.inSpans | |
*/ | |
inline fun SpannableStringBuilder.subscript(builderAction: SpannableStringBuilder.() -> Unit) = | |
inSpans(SubscriptSpan(), builderAction = builderAction) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment