diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ea5aa7..509259f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 1.2.0 + +- Add `ignoreCase` flag support to `span` function (#18) + ### 1.1.0 - Add `lineBackground` and `lineHeight` for Android Q diff --git a/README.md b/README.md index 5e82da2..37c4b9e 100644 --- a/README.md +++ b/README.md @@ -94,13 +94,13 @@ Spans: Text manipulation: -| Methods | Description | -| --------------- | --------------------------- | -| append(text, spans...) | appends text | -| append(image(...)) | appends image | -| replace(search, replace, spans...)| search and replace text | -| span(search, spans...) | search text and apply spans | -| insert(pos, text, spans...) | insert given text in given position | +| Methods | Description | +| --------------- | --------------------------- | +| append(text, spans...) | appends text | +| append(image(...)) | appends image | +| replace(search, replace, spans...) | search and replace text | +| span(search, ignoreCase = false, spans...)| search text and apply spans | +| insert(pos, text, spans...) | insert given text in given position | #### How to use ``` diff --git a/lib/src/main/java/lt/neworld/spanner/Spanner.kt b/lib/src/main/java/lt/neworld/spanner/Spanner.kt index 6447c27..63bce94 100644 --- a/lib/src/main/java/lt/neworld/spanner/Spanner.kt +++ b/lib/src/main/java/lt/neworld/spanner/Spanner.kt @@ -15,7 +15,7 @@ class Spanner(text: CharSequence?) : SpannableStringBuilder(text) { return this } - override fun append(text: CharSequence, start: Int, end: Int): Spanner { + override fun append(text: CharSequence?, start: Int, end: Int): Spanner { super.append(text, start, end) return this @@ -131,19 +131,41 @@ class Spanner(text: CharSequence?) : SpannableStringBuilder(text) { return this } + fun span(search: CharSequence, ignoreCase: Boolean = false, vararg spans: Span): Spanner { + span(0, search, ignoreCase, *spans) + + return this + } + fun span(startIndex: Int, search: CharSequence, vararg spans: Span): Spanner { + return span(startIndex, search, false, *spans) + } + + fun span(startIndex: Int, search: CharSequence, ignoreCase: Boolean = false, vararg spans: Span): Spanner { if (TextUtils.isEmpty(search)) { setSpans(0, length, *spans) return this } + val actualSearch = if (ignoreCase) { + (search.toString() as java.lang.String).toLowerCase() + } else { + search + } + + val actualThis = if (ignoreCase) { + (toString() as java.lang.String).toLowerCase() + } else { + this + } + var lastPos: Int = startIndex - 1 while (true) { - lastPos = TextUtils.indexOf(this, search, lastPos + 1) + lastPos = TextUtils.indexOf(actualThis, actualSearch, lastPos + 1) if (lastPos == -1) break - setSpans(lastPos, lastPos + search.length, *spans) + setSpans(lastPos, lastPos + actualSearch.length, *spans) } return this diff --git a/lib/src/test/java/lt/neworld/spanner/SpannerTest.kt b/lib/src/test/java/lt/neworld/spanner/SpannerTest.kt index e8221d9..8a4df3b 100644 --- a/lib/src/test/java/lt/neworld/spanner/SpannerTest.kt +++ b/lib/src/test/java/lt/neworld/spanner/SpannerTest.kt @@ -6,7 +6,6 @@ import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner -import java.lang.StringBuilder /** * @author Andrius Semionovas @@ -110,8 +109,21 @@ class SpannerTest { assertSpans("foo bar", Spanner().append("foo bar").span("bar", bold())) assertSpans("foo", Spanner().append("foo").span("bar", bold())) assertSpans( - "bar foo bar", - Spanner().append("bar foo bar").span("bar", bold()) + expected = "bar foo bar", + actual = Spanner().append("bar foo bar").span("bar", bold()) + ) + } + + @Test + fun span_text_ignoreCase() { + assertSpans( + expected = "foo BAR", + actual = Spanner("foo BAR").span("bar", ignoreCase = true, spans = bold()) + ) + assertSpans("foo", Spanner().append("foo").span("bar", bold())) + assertSpans( + expected = "bAR foo Bar", + actual = Spanner().append("bAR foo Bar").span("bar", ignoreCase = true, spans = bold()) ) }