This lib provides simple and fluent API for creating Android Spannable. Features:
- Simple and fluent API
- Helpers to create spans
- Small method footprint (<200 methods, 29kb AAR package)
This library focuses on building spannable. If you prefer put full text first and then apply spans, take a look at another awesome library
Spannable spannable = new Spanner()
.append("Original text\n\n")
.append("Big and blurry\n", Spans.sizePX(100))
.span("blurry", blur(5.0f, BlurMaskFilter.Blur.SOLID))
.append("big in DP\n", sizeDP(30))
.append("50% of original size\n", scaleSize(0.5f))
.append("bold\n", bold())
.append("italic\n", italic())
.append("bold and italic\n", boldItalic())
.append("custom typeface\n", font("sans-serif-black"))
.append("strike through\n", strikeThrough())
.append("underline\n", underline())
.append("background\n", background(Color.YELLOW))
.append("foreground\n", foreground(Color.RED))
.append("subscript\n", subscript())
.append("superscript\n", superscript())
.append(image(context, R.drawable.ic_android_16dp)).append("\n")
.append("quite\n", quote())
.append("The quick brown fox jumps over the lazy dog\n", bold(), foreground(0xFF904f1c), Spans.quote())
.append("Custom\n", custom(new CustomSpan()))
.append("Click here\n", click(onClickListener))
.append("http://www.android.com\n", url("http://www.android.com"))
;
It looks like:
You can manipulate text in many more ways:
Spannable spannable = new Spanner("The quick brown fox jumps over the lazy dog")
.span("fox", foreground(Color.RED)) // search and span by given text
.replace("dog", "cat", strikeThrough()) // any number of spans
.insert(5, "foo", bold(), italic()) // any number of spans
.append("bar", underline()); // any number of spans
If you need custom span, you need span builder:
//java 7
Spannable spannable = new Spanner("The quick brown fox jumps over the lazy dog")
.span("fox", custom(new SpanBuilder() {
@Override
public Object build() {
return new StyleSpan(Typeface.ITALIC);
}
});
//java 8
Spannable spannable = new Spanner("The quick brown fox jumps over the lazy dog")
.span("fox", custom(() -> new StyleSpan(Typeface.ITALIC));
//kotlin
val spannable = Spanner("The quick brown fox jumps over the lazy dog")
.span("fox", custom { StyleSpan(Typeface.ITALIC) })
Spans:
sizePX(int) | background(int color) | center() |
sizeDP(int) | foreground(int color) | alignmentOpposite() |
sizeSP(int) | subscript() | alignmentNormal() |
scaleSize(float) | superscript() | bullet(...) |
bold() | image(...) | imageMargin(...) |
italic() | click(listener) | leadingMargin(...) |
boldItalic() | url(url) | edit(...) |
font(String) | custom(spanBuilder) | emboss(...) |
strikeThrough() | quote() | blur(...) |
underline() | appearance(...) | locale(...) |
tabStop(where) | suggestion(...) | lineBackground(color) |
lineHeight(height) |
Text manipulation:
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 |
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'lt.neworld:spanner:1.1.0'
}
Copyright 2017 Andrius Semionovas
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.