Skip to content

Commit

Permalink
add vertical padding span
Browse files Browse the repository at this point in the history
  • Loading branch information
2dxgujun committed Jan 18, 2018
1 parent 55b8dcc commit cceb3ab
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class SampleActivity : AppCompatActivity() {
}
+"\n"
span("Align normal") {
textAlign = "normal"
alignment = "normal"
}
+"\n"
span("Align center") {
textAlign = "center"
alignment = "center"
}
+"\n"
span("Align opposite") {
textAlign = "opposite"
alignment = "opposite"
}
+"\n"
span("Underline") {
Expand Down Expand Up @@ -85,23 +85,29 @@ class SampleActivity : AppCompatActivity() {
+"\n"
span {
lineSpacing = dp(8)
+"LineSpacingSpan"
backgroundColor = Color.LTGRAY
verticalPadding = dp(5)
+"LineSpacingWithVerticalPadding"
+"\n"
+"LineSpacingSpan"
+"LineSpacingWithVerticalPadding"
+"\n"
+"LineSpacingSpan"
+"LineSpacingWithVerticalPadding"
}
+"\n"
quote(Color.RED, "QuoteSpan")
+"\n"
span {
+"Plain text"
subscript("SubscriptSpan")
superscript("SuperscriptSpan")
+"Plain"
subscript("Subscript") {
textSize = dp(8)
}
superscript("Superscript") {
textSize = dp(8)
}
}
+"\n"
span("BackgroundSpan") {
backgroundColor = Color.LTGRAY
backgroundColor = Color.CYAN
}
+"\n"
span("ForegroundSpan") {
Expand All @@ -116,7 +122,7 @@ class SampleActivity : AppCompatActivity() {
span {
textColor = Color.BLUE
textSize = dp(20)
+"Simple text"
+"Origin text"
+"\n"
span("Override foreground") {
textColor = Color.RED
Expand Down
16 changes: 14 additions & 2 deletions span/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ install {
pom.project {
name ARTIFACTID
description 'Styling text with fun'
url 'https://github.com/2dxgujun/span'
url 'https://github.com/2dxgujun/Spannable-In-Kotlin'

packaging 'aar'
groupId GROUP
Expand Down Expand Up @@ -69,7 +69,7 @@ try {
repo = 'maven'
name = ARTIFACTID
licenses = ['Apache-2.0']
vcsUrl = 'https://github.com/2dxgujun/span.git'
vcsUrl = 'https://github.com/2dxgujun/Spannable-In-Kotlin.git'
}
}

Expand All @@ -78,7 +78,19 @@ try {
classifier = 'sources'
}

task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath = files(project.android.getBootClasspath())
excludes = ['**/*.kt']
}

task javadocJar(type: Jar, dependsOn: javadoc) {
from javadoc.destinationDir
classifier = 'javadoc'
}

artifacts {
archives javadocJar
archives sourcesJar
}
} catch (Exception e) {
Expand Down
27 changes: 24 additions & 3 deletions span/src/main/kotlin/me/gujun/android/span/span.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.view.View
import me.gujun.android.span.style.CustomTypefaceSpan
import me.gujun.android.span.style.LineSpacingSpan
import me.gujun.android.span.style.TextDecorationLineSpan
import me.gujun.android.span.style.VerticalPaddingSpan

class Span(val parent: Span? = null) : SpannableStringBuilder() {

Expand All @@ -47,12 +48,18 @@ class Span(val parent: Span? = null) : SpannableStringBuilder() {

var textStyle: String = parent?.textStyle ?: ""

var textAlign: String = parent?.textAlign ?: ""
var alignment: String = parent?.alignment ?: ""

var textDecorationLine: String = parent?.textDecorationLine ?: ""

@Dimension(unit = Dimension.PX) var lineSpacing: Int = UNSPECIFIED

@Dimension(unit = Dimension.PX) var paddingTop: Int = UNSPECIFIED

@Dimension(unit = Dimension.PX) var paddingBottom: Int = UNSPECIFIED

@Dimension(unit = Dimension.PX) var verticalPadding: Int = UNSPECIFIED

var onClick: (() -> Unit)? = null

var styles: ArrayList<Any> = arrayListOf()
Expand Down Expand Up @@ -102,8 +109,8 @@ class Span(val parent: Span? = null) : SpannableStringBuilder() {
}

private fun buildParagraphStyle(builder: ArrayList<Any>) {
if (!TextUtils.isEmpty(textAlign)) {
builder.add(AlignmentSpan.Standard(when (textAlign) {
if (!TextUtils.isEmpty(alignment)) {
builder.add(AlignmentSpan.Standard(when (alignment) {
"normal" -> Layout.Alignment.ALIGN_NORMAL
"opposite" -> Layout.Alignment.ALIGN_OPPOSITE
"center" -> Layout.Alignment.ALIGN_CENTER
Expand All @@ -114,6 +121,20 @@ class Span(val parent: Span? = null) : SpannableStringBuilder() {
if (lineSpacing != UNSPECIFIED) {
builder.add(LineSpacingSpan(lineSpacing))
}

paddingTop = when {
paddingTop != UNSPECIFIED -> paddingTop
verticalPadding != UNSPECIFIED -> verticalPadding
else -> 0
}
paddingBottom = when {
paddingBottom != UNSPECIFIED -> paddingBottom
verticalPadding != UNSPECIFIED -> verticalPadding
else -> 0
}
if (paddingTop != 0 || paddingBottom != 0) {
builder.add(VerticalPaddingSpan(paddingTop, paddingBottom))
}
}

fun build(): Span {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.gujun.android.span.style

import android.graphics.Paint.FontMetricsInt
import android.text.Spanned
import android.text.style.LineHeightSpan

class VerticalPaddingSpan(private val paddingTop: Int,
private val paddingBottom: Int) : LineHeightSpan {

override fun chooseHeight(text: CharSequence, start: Int, end: Int, spanstartv: Int, v: Int,
fm: FontMetricsInt) {
text as Spanned
val spanStart = text.getSpanStart(this)
val spanEnd = text.getSpanEnd(this)

// Log.d("DEBUG", "text: $text")
// Log.d("DEBUG", "spanStart: $spanStart")
// Log.d("DEBUG", "spanEnd: $spanEnd")
// Log.d("DEBUG", "start: $start")
// Log.d("DEBUG", "end: $end")
// Log.d("DEBUG", "spanstartv: $spanstartv")
// Log.d("DEBUG", "v: $v")

if (spanStart == start) {
fm.ascent -= paddingTop
}
if (spanEnd == end - 1) {
fm.descent += paddingBottom
}
}
}

0 comments on commit cceb3ab

Please sign in to comment.