Skip to content

Commit

Permalink
v1.6 release
Browse files Browse the repository at this point in the history
  • Loading branch information
2dxgujun committed Jan 25, 2018
1 parent 77c70b0 commit 6e6eaed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ val text = span {
You can grab it via Gradle:

```
implementation 'me.gujun.android:span:1.5'
implementation 'me.gujun.android:span:1.6'
```

### Usage
Expand Down Expand Up @@ -136,7 +136,7 @@ val text = span {

#### Reusable style

You can create reusable styles and reuse them in multiple spans.
You can create reusable styles and use them in multiple spans.

```kotlin
val headerStyle = style {
Expand All @@ -146,18 +146,36 @@ val headerStyle = style {
verticalPadding = dp(3)
}
val content = span {
span("Header 1") {
span("Header1") {
style = headerStyle
}
span {
text = "\n...\n"
}
span("Header 2") {
span("Header2") {
style = headerStyle
}
}
```

or you can declare an extension function and apply styles as you wish:

```kotlin
fun Span.header(text: CharSequence): Span = span(text) {
textColor = Color.BLACK
textStyle = "bold"
textSize = dp(20)
verticalPadding = dp(3)
}

val content = span {
header("Header1\n")
span {
text = "..."
}
}
```

#### Global style

You can set global style by create a *style* and pass it to the companion object `Span.globalStyle`.
Expand Down
2 changes: 1 addition & 1 deletion span/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {

def GROUP = 'me.gujun.android'
def ARTIFACTID = 'span'
def VERSION = '1.5'
def VERSION = '1.6'

group = GROUP
version = VERSION
Expand Down
23 changes: 8 additions & 15 deletions span/src/main/kotlin/me/gujun/android/span/span.kt
Original file line number Diff line number Diff line change
Expand Up @@ -243,67 +243,61 @@ fun style(init: Span.() -> Unit): Span = Span().apply {
init()
}

fun Span.span(init: Span.() -> Unit = {}): Span = run {
fun Span.span(init: Span.() -> Unit = {}): Span = apply {
append(Span(parent = this).apply {
init()
build()
})
this
}

fun Span.span(text: CharSequence, init: Span.() -> Unit = {}): Span = run {
fun Span.span(text: CharSequence, init: Span.() -> Unit = {}): Span = apply {
append(Span(parent = this).apply {
this.text = text
init()
build()
})
this
}

fun Span.link(url: String, text: CharSequence = "",
init: Span.() -> Unit = {}): Span = run {
init: Span.() -> Unit = {}): Span = apply {
append(Span(parent = this).apply {
this.text = text
this.spans.add(URLSpan(url))
init()
build()
})
this
}

fun Span.quote(@ColorInt color: Int, text: CharSequence = "",
init: Span.() -> Unit = {}): Span = run {
init: Span.() -> Unit = {}): Span = apply {
append(Span(parent = this).apply {
this.text = text
this.spans.add(QuoteSpan(color))
init()
build()
})
this
}

fun Span.superscript(text: CharSequence = "", init: Span.() -> Unit = {}): Span = run {
fun Span.superscript(text: CharSequence = "", init: Span.() -> Unit = {}): Span = apply {
append(Span(parent = this).apply {
this.text = text
this.spans.add(SuperscriptSpan())
init()
build()
})
this
}

fun Span.subscript(text: CharSequence = "", init: Span.() -> Unit = {}): Span = run {
fun Span.subscript(text: CharSequence = "", init: Span.() -> Unit = {}): Span = apply {
append(Span(parent = this).apply {
this.text = text
this.spans.add(SubscriptSpan())
init()
build()
})
this
}

fun Span.image(drawable: Drawable, alignment: String = "bottom",
init: Span.() -> Unit = {}): Span = run {
init: Span.() -> Unit = {}): Span = apply {
drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
append(Span(parent = this).apply {
this.text = " "
Expand All @@ -315,9 +309,8 @@ fun Span.image(drawable: Drawable, alignment: String = "bottom",
init()
build()
})
this
}

fun Span.addSpan(what: Any) = run {
fun Span.addSpan(what: Any) = apply {
this.spans.add(what)
}

0 comments on commit 6e6eaed

Please sign in to comment.