Add Label text alignment property

This commit is contained in:
Shadowfacts 2019-08-09 17:06:15 -04:00
parent 5c66e961d2
commit 37835b578d
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 12 additions and 2 deletions

View File

@ -24,7 +24,8 @@ class Label(
text: String, text: String,
val shadow: Boolean = true, val shadow: Boolean = true,
val maxLines: Int = 0, val maxLines: Int = 0,
val wrappingMode: WrappingMode = WrappingMode.WRAP val wrappingMode: WrappingMode = WrappingMode.WRAP,
val textAlignment: TextAlignment = TextAlignment.LEFT
): View() { ): View() {
companion object { companion object {
@ -36,6 +37,10 @@ class Label(
WRAP, NO_WRAP WRAP, NO_WRAP
} }
enum class TextAlignment {
LEFT, CENTER, RIGHT
}
/** /**
* The text of this label. Mutating this field will update the intrinsic content size and trigger a layout. * The text of this label. Mutating this field will update the intrinsic content size and trigger a layout.
*/ */
@ -79,8 +84,13 @@ class Label(
} }
for (i in 0 until lines.size) { for (i in 0 until lines.size) {
val x = when (textAlignment) {
TextAlignment.LEFT -> 0.0
TextAlignment.CENTER -> (bounds.width + textRenderer.getStringWidth(lines[i])) / 2
TextAlignment.RIGHT -> bounds.width - textRenderer.getStringWidth(lines[i])
}
val y = i * textRenderer.fontHeight val y = i * textRenderer.fontHeight
drawFunc(lines[i], 0f, y.toFloat(), textColorARGB) drawFunc(lines[i], x.toFloat(), y.toFloat(), textColorARGB)
} }
} }