Skip to content
This repository has been archived by the owner on Nov 14, 2018. It is now read-only.

SpannableStringBuilder extensions #436

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,11 @@ package androidx.text {
method public static android.text.SpannableStringBuilder italic(android.text.SpannableStringBuilder, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder scale(android.text.SpannableStringBuilder, float proportion, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder strikeThrough(android.text.SpannableStringBuilder, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder subscript(android.text.SpannableStringBuilder, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder superscript(android.text.SpannableStringBuilder, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder typeface(android.text.SpannableStringBuilder, String family, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder underline(android.text.SpannableStringBuilder, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder url(android.text.SpannableStringBuilder, String url, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
}

public final class SpannableStringKt {
Expand Down
72 changes: 72 additions & 0 deletions src/androidTest/java/androidx/text/SpannableStringBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.text.style.SubscriptSpan
import android.text.style.SuperscriptSpan
import android.text.style.TypefaceSpan
import android.text.style.URLSpan
import android.text.style.UnderlineSpan
import org.junit.Assert.assertEquals
import org.junit.Assert.assertSame
Expand Down Expand Up @@ -186,6 +188,76 @@ class SpannableStringBuilderTest {
assertEquals(12, result.getSpanEnd(scale))
}

@Test fun builderUp() {
val result: SpannedString = buildSpannedString {
append("Hello, ")
superscript {
append("World")
}
}
assertEquals("Hello, World", result.toString())

val spans = result.getSpans<Any>()
assertEquals(1, spans.size)

val up = spans.filterIsInstance<SuperscriptSpan>().single()
assertEquals(7, result.getSpanStart(up))
assertEquals(12, result.getSpanEnd(up))
}

@Test fun builderDown() {
val result: SpannedString = buildSpannedString {
append("Hello, ")
subscript {
append("World")
}
}
assertEquals("Hello, World", result.toString())

val spans = result.getSpans<Any>()
assertEquals(1, spans.size)

val down = spans.filterIsInstance<SubscriptSpan>().single()
assertEquals(7, result.getSpanStart(down))
assertEquals(12, result.getSpanEnd(down))
}

@Test fun builderFont() {
val result: SpannedString = buildSpannedString {
append("Hello, ")
typeface("serif") {
append("World")
}
}
assertEquals("Hello, World", result.toString())

val spans = result.getSpans<Any>()
assertEquals(1, spans.size)

val font = spans.filterIsInstance<TypefaceSpan>().single()
assertEquals("serif", font.family)
assertEquals(7, result.getSpanStart(font))
assertEquals(12, result.getSpanEnd(font))
}

@Test fun builderUrl() {
val result: SpannedString = buildSpannedString {
append("Hello, ")
url("http://www.google.com") {
append("World")
}
}
assertEquals("Hello, World", result.toString())

val spans = result.getSpans<Any>()
assertEquals(1, spans.size)

val url = spans.filterIsInstance<URLSpan>().single()
assertEquals("http://www.google.com", url.url)
assertEquals(7, result.getSpanStart(url))
assertEquals(12, result.getSpanEnd(url))
}

@Test fun nested() {
val result: SpannedString = buildSpannedString {
color(RED) {
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/androidx/text/SpannableStringBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import android.text.style.ForegroundColorSpan
import android.text.style.StrikethroughSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.text.style.SubscriptSpan
import android.text.style.SuperscriptSpan
import android.text.style.TypefaceSpan
import android.text.style.URLSpan
import android.text.style.UnderlineSpan

/**
Expand Down Expand Up @@ -134,3 +138,39 @@ 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)

/**
* Wrap appended text in [builderAction] in a [TypefaceSpan].
*
* @see SpannableStringBuilder.inSpans
*/
inline fun SpannableStringBuilder.typeface(
family: String,
builderAction: SpannableStringBuilder.() -> Unit
) = inSpans(TypefaceSpan(family), builderAction = builderAction)

/**
* Wrap appended text in [builderAction] in a [URLSpan].
*
* @see SpannableStringBuilder.inSpans
*/
inline fun SpannableStringBuilder.url(
url: String,
builderAction: SpannableStringBuilder.() -> Unit
) = inSpans(URLSpan(url), builderAction = builderAction)