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 1 commit
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 @@ -386,12 +386,16 @@ package androidx.text {
method public static android.text.SpannableStringBuilder bold(android.text.SpannableStringBuilder, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannedString buildSpannedString(kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder color(android.text.SpannableStringBuilder, @ColorInt int color, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder down(android.text.SpannableStringBuilder, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder font(android.text.SpannableStringBuilder, String family, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder inSpans(android.text.SpannableStringBuilder, Object[] spans, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder inSpans(android.text.SpannableStringBuilder, Object span, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
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 underline(android.text.SpannableStringBuilder, kotlin.jvm.functions.Function1<? super android.text.SpannableStringBuilder,kotlin.Unit> builderAction);
method public static android.text.SpannableStringBuilder up(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, ")
up {
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, ")
down {
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, ")
font("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.up(builderAction: SpannableStringBuilder.() -> Unit) =
inSpans(SuperscriptSpan(), builderAction = builderAction)

/**
* Wrap appended text in [builderAction] in a [SubscriptSpan].
*
* @see SpannableStringBuilder.inSpans
*/
inline fun SpannableStringBuilder.down(builderAction: SpannableStringBuilder.() -> Unit) =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think "up" and "down" correctly convey the behavior. "superscript" and "subscript" seem fine.

Also are these common enough that they warrant inclusion? I can't remember using them once.

inSpans(SubscriptSpan(), builderAction = builderAction)

/**
* Wrap appended text in [builderAction] in a [TypefaceSpan].
*
* @see SpannableStringBuilder.inSpans
*/
inline fun SpannableStringBuilder.font(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm torn between calling this "font" or "typeface" to match the span name. I suspect matching the span name will improve discoverability.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching name seems like better, I renamed the methods - "up", "down", "font" to "superscript", "subscript" and "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)