diff --git a/sample/src/main/kotlin/me/gujun/android/span/sample/SampleActivity.kt b/sample/src/main/kotlin/me/gujun/android/span/sample/SampleActivity.kt index 7434c75..3f9e22e 100644 --- a/sample/src/main/kotlin/me/gujun/android/span/sample/SampleActivity.kt +++ b/sample/src/main/kotlin/me/gujun/android/span/sample/SampleActivity.kt @@ -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") { @@ -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") { @@ -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 diff --git a/span/build.gradle b/span/build.gradle index 7115fb7..69cf5fd 100644 --- a/span/build.gradle +++ b/span/build.gradle @@ -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 @@ -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' } } @@ -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) { diff --git a/span/src/main/kotlin/me/gujun/android/span/span.kt b/span/src/main/kotlin/me/gujun/android/span/span.kt index 5bd6009..853f6f3 100644 --- a/span/src/main/kotlin/me/gujun/android/span/span.kt +++ b/span/src/main/kotlin/me/gujun/android/span/span.kt @@ -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() { @@ -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 = arrayListOf() @@ -102,8 +109,8 @@ class Span(val parent: Span? = null) : SpannableStringBuilder() { } private fun buildParagraphStyle(builder: ArrayList) { - 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 @@ -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 { diff --git a/span/src/main/kotlin/me/gujun/android/span/style/VerticalPaddingSpan.kt b/span/src/main/kotlin/me/gujun/android/span/style/VerticalPaddingSpan.kt new file mode 100644 index 0000000..e23e142 --- /dev/null +++ b/span/src/main/kotlin/me/gujun/android/span/style/VerticalPaddingSpan.kt @@ -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 + } + } +} \ No newline at end of file