Skip to content

Commit f6df900

Browse files
authored
Merge pull request #752 from skydoves/feature/auto-size
Support auto-sized text
2 parents dfbfe21 + 634eb6a commit f6df900

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,26 @@ balloon.setPaddingTop(12) // sets 12dp padding to content's top.
259259
### Margin
260260
If the location of the balloon according to the anchor would be located at the boundaries on the screen,<br>
261261
the balloon will be stick to the end of the screen. In this case, we can give horizontal margins to the balloon.
262+
262263
```kotlin
263264
.setMargin(12) // sets the margin on the balloon all directions.
264265
.setMarginLeft(14) // sets the left margin on the balloon.
265266
.setMarginRight(14) // sets the right margin on the balloon.
266267
```
267268

269+
### Auto-sized text
270+
271+
You can set auto-sized text based on the balloon's window size, specifying minimum and maximum text sizes, as shown in the example below:
272+
273+
```kotlin
274+
.setTextSize(15f)
275+
.setMinAutoSizeTextSize(14f)
276+
.setMaxAutoSizeTextSize(18f)
277+
.setEnableAutoSized(true)
278+
```
279+
280+
> Note: Ensure that the maximum auto text size is set to a value higher than the minimum auto text size.
281+
268282
### Arrow Composition
269283
We can customize the arrow on the Balloon with various methods. For more details, check out the [Balloon.Builder](https://skydoves.github.io/libraries/balloon/html/balloon/com.skydoves.balloon/-balloon/-builder/index.html).
270284

@@ -322,9 +336,11 @@ We can customize the text on the Balloon.
322336
```
323337

324338
If your text includes HTML tags, you can render the text by enabling HTML option with `setTextIsHtml` method.
339+
325340
```java
326341
.setTextIsHtml(true)
327342
```
343+
328344
This method will parse the text with the `Html.fromHtml(text)` internally.
329345

330346
### TextForm

balloon/api/balloon.api

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ public final class com/skydoves/balloon/Balloon$Builder {
262262
public final fun getDismissWhenTouchMargin ()Z
263263
public final fun getDismissWhenTouchOutside ()Z
264264
public final fun getElevation ()F
265+
public final fun getEnableAutoSized ()Z
265266
public final fun getHeight ()I
266267
public final fun getIconColor ()I
267268
public final fun getIconContentDescription ()Ljava/lang/CharSequence;
@@ -280,9 +281,11 @@ public final class com/skydoves/balloon/Balloon$Builder {
280281
public final fun getMarginLeft ()I
281282
public final fun getMarginRight ()I
282283
public final fun getMarginTop ()I
284+
public final fun getMaxAutoSizeTextSize ()F
283285
public final fun getMaxWidth ()I
284286
public final fun getMaxWidthRatio ()F
285287
public final fun getMeasuredWidth ()I
288+
public final fun getMinAutoSizeTextSize ()F
286289
public final fun getMinWidth ()I
287290
public final fun getMinWidthRatio ()F
288291
public final fun getMovementMethod ()Landroid/text/method/MovementMethod;
@@ -420,6 +423,8 @@ public final class com/skydoves/balloon/Balloon$Builder {
420423
public final synthetic fun setElevation (F)V
421424
public final fun setElevation (I)Lcom/skydoves/balloon/Balloon$Builder;
422425
public final fun setElevationResource (I)Lcom/skydoves/balloon/Balloon$Builder;
426+
public final fun setEnableAutoSized (Z)Lcom/skydoves/balloon/Balloon$Builder;
427+
public final synthetic fun setEnableAutoSized (Z)V
423428
public final fun setFocusable (Z)Lcom/skydoves/balloon/Balloon$Builder;
424429
public final synthetic fun setFocusable (Z)V
425430
public final fun setHeight (I)Lcom/skydoves/balloon/Balloon$Builder;
@@ -483,13 +488,17 @@ public final class com/skydoves/balloon/Balloon$Builder {
483488
public final fun setMarginTopResource (I)Lcom/skydoves/balloon/Balloon$Builder;
484489
public final fun setMarginVertical (I)Lcom/skydoves/balloon/Balloon$Builder;
485490
public final fun setMarginVerticalResource (I)Lcom/skydoves/balloon/Balloon$Builder;
491+
public final fun setMaxAutoSizeTextSize (F)Lcom/skydoves/balloon/Balloon$Builder;
492+
public final synthetic fun setMaxAutoSizeTextSize (F)V
486493
public final fun setMaxWidth (I)Lcom/skydoves/balloon/Balloon$Builder;
487494
public final synthetic fun setMaxWidth (I)V
488495
public final fun setMaxWidthRatio (F)Lcom/skydoves/balloon/Balloon$Builder;
489496
public final synthetic fun setMaxWidthRatio (F)V
490497
public final fun setMaxWidthResource (I)Lcom/skydoves/balloon/Balloon$Builder;
491498
public final fun setMeasuredWidth (I)Lcom/skydoves/balloon/Balloon$Builder;
492499
public final synthetic fun setMeasuredWidth (I)V
500+
public final fun setMinAutoSizeTextSize (F)Lcom/skydoves/balloon/Balloon$Builder;
501+
public final synthetic fun setMinAutoSizeTextSize (F)V
493502
public final fun setMinWidth (I)Lcom/skydoves/balloon/Balloon$Builder;
494503
public final synthetic fun setMinWidth (I)V
495504
public final fun setMinWidthRatio (F)Lcom/skydoves/balloon/Balloon$Builder;
@@ -865,7 +874,10 @@ public final class com/skydoves/balloon/PlacementType : java/lang/Enum {
865874

866875
public final class com/skydoves/balloon/TextForm {
867876
public synthetic fun <init> (Lcom/skydoves/balloon/TextForm$Builder;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
877+
public final fun getEnableAutoSized ()Z
868878
public final fun getIncludeFontPadding ()Z
879+
public final fun getMaxAutoSizeTextSize ()F
880+
public final fun getMinAutoSizeTextSize ()F
869881
public final fun getMovementMethod ()Landroid/text/method/MovementMethod;
870882
public final fun getText ()Ljava/lang/CharSequence;
871883
public final fun getTextColor ()I
@@ -882,7 +894,10 @@ public final class com/skydoves/balloon/TextForm$Builder {
882894
public fun <init> (Landroid/content/Context;)V
883895
public final fun build ()Lcom/skydoves/balloon/TextForm;
884896
public final fun getContext ()Landroid/content/Context;
897+
public final fun getEnableAutoSized ()Z
885898
public final fun getIncludeFontPadding ()Z
899+
public final fun getMaxAutoSizeTextSize ()F
900+
public final fun getMinAutoSizeTextSize ()F
886901
public final fun getMovementMethod ()Landroid/text/method/MovementMethod;
887902
public final fun getText ()Ljava/lang/CharSequence;
888903
public final fun getTextColor ()I
@@ -893,8 +908,14 @@ public final class com/skydoves/balloon/TextForm$Builder {
893908
public final fun getTextSize ()F
894909
public final fun getTextTypeface ()I
895910
public final fun getTextTypefaceObject ()Landroid/graphics/Typeface;
911+
public final fun setEnableAutoSized (Z)Lcom/skydoves/balloon/TextForm$Builder;
912+
public final synthetic fun setEnableAutoSized (Z)V
896913
public final fun setIncludeFontPadding (Z)Lcom/skydoves/balloon/TextForm$Builder;
897914
public final synthetic fun setIncludeFontPadding (Z)V
915+
public final fun setMaxAutoSizeTextSize (F)Lcom/skydoves/balloon/TextForm$Builder;
916+
public final synthetic fun setMaxAutoSizeTextSize (F)V
917+
public final fun setMinAutoSizeTextSize (F)Lcom/skydoves/balloon/TextForm$Builder;
918+
public final synthetic fun setMinAutoSizeTextSize (F)V
898919
public final fun setMovementMethod (Landroid/text/method/MovementMethod;)Lcom/skydoves/balloon/TextForm$Builder;
899920
public final synthetic fun setMovementMethod (Landroid/text/method/MovementMethod;)V
900921
public final fun setText (Ljava/lang/CharSequence;)Lcom/skydoves/balloon/TextForm$Builder;

balloon/src/main/kotlin/com/skydoves/balloon/Balloon.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,9 @@ public class Balloon private constructor(
639639
setTextLineSpacing(builder.textLineSpacing)
640640
setTextLetterSpacing(builder.textLetterSpacing)
641641
setMovementMethod(builder.movementMethod)
642+
setEnableAutoSized(builder.enableAutoSized)
643+
setMinAutoSizeTextSize(builder.minAutoSizeTextSize)
644+
setMaxAutoSizeTextSize(builder.maxAutoSizeTextSize)
642645
},
643646
)
644647
measureTextWidth(this, binding.balloonCard)
@@ -2163,6 +2166,15 @@ public class Balloon private constructor(
21632166
@set:JvmSynthetic
21642167
public var textSize: Float = 12f
21652168

2169+
@set:JvmSynthetic
2170+
public var enableAutoSized: Boolean = false
2171+
2172+
@set:JvmSynthetic
2173+
public var minAutoSizeTextSize: Float = textSize
2174+
2175+
@set:JvmSynthetic
2176+
public var maxAutoSizeTextSize: Float = textSize + 1
2177+
21662178
@set:JvmSynthetic
21672179
public var textTypeface: Int = Typeface.NORMAL
21682180

@@ -2814,6 +2826,21 @@ public class Balloon private constructor(
28142826
this.textSize = context.px2Sp(context.dimen(value))
28152827
}
28162828

2829+
/** Enables or disables the auto-sized text. */
2830+
public fun setEnableAutoSized(value: Boolean): Builder = apply {
2831+
this.enableAutoSized = value
2832+
}
2833+
2834+
/** Sets the minimum auto-sized text size. */
2835+
public fun setMinAutoSizeTextSize(@Sp value: Float): Builder = apply {
2836+
this.minAutoSizeTextSize = value
2837+
}
2838+
2839+
/** Sets the maximum auto-sized text size. */
2840+
public fun setMaxAutoSizeTextSize(@Sp value: Float): Builder = apply {
2841+
this.maxAutoSizeTextSize = value
2842+
}
2843+
28172844
/** sets the typeface of the main text content. */
28182845
public fun setTextTypeface(value: Int): Builder = apply { this.textTypeface = value }
28192846

balloon/src/main/kotlin/com/skydoves/balloon/TextForm.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public class TextForm private constructor(
6161
@Sp
6262
public val textSize: Float = builder.textSize
6363

64+
public val enableAutoSized: Boolean = builder.enableAutoSized
65+
66+
public val minAutoSizeTextSize: Float = builder.minAutoSizeTextSize
67+
68+
public val maxAutoSizeTextSize: Float = builder.maxAutoSizeTextSize
69+
6470
@ColorInt
6571
public val textColor: Int = builder.textColor
6672

@@ -90,6 +96,15 @@ public class TextForm private constructor(
9096
@set:JvmSynthetic
9197
public var textSize: Float = 12f
9298

99+
@set:JvmSynthetic
100+
public var enableAutoSized: Boolean = true
101+
102+
@set:JvmSynthetic
103+
public var minAutoSizeTextSize: Float = textSize
104+
105+
@set:JvmSynthetic
106+
public var maxAutoSizeTextSize: Float = textSize + 1
107+
93108
@ColorInt
94109
@set:JvmSynthetic
95110
public var textColor: Int = Color.WHITE
@@ -121,6 +136,21 @@ public class TextForm private constructor(
121136
/** sets the content text of the form. */
122137
public fun setText(value: CharSequence): Builder = apply { this.text = value }
123138

139+
/** Enables or disables the auto-sized text. */
140+
public fun setEnableAutoSized(value: Boolean): Builder = apply {
141+
this.enableAutoSized = value
142+
}
143+
144+
/** Sets the minimum auto-sized text size. */
145+
public fun setMinAutoSizeTextSize(@Sp value: Float): Builder = apply {
146+
this.minAutoSizeTextSize = value
147+
}
148+
149+
/** Sets the maximum auto-sized text size. */
150+
public fun setMaxAutoSizeTextSize(@Sp value: Float): Builder = apply {
151+
this.maxAutoSizeTextSize = value
152+
}
153+
124154
/** sets the content text of the form using string resource. */
125155
public fun setTextResource(@StringRes value: Int): Builder = apply {
126156
this.text = context.getString(value)

balloon/src/main/kotlin/com/skydoves/balloon/extensions/TextViewExtension.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import android.text.Spanned
2323
import android.widget.TextView
2424
import androidx.appcompat.content.res.AppCompatResources
2525
import androidx.core.text.HtmlCompat
26+
import androidx.core.widget.TextViewCompat
2627
import com.skydoves.balloon.IconForm
2728
import com.skydoves.balloon.IconGravity
2829
import com.skydoves.balloon.TextForm
@@ -46,6 +47,15 @@ internal fun TextView.applyTextForm(textForm: TextForm) {
4647
textForm.textLetterSpacing?.let { letterSpacing = it }
4748
textForm.textTypeface?.let { typeface = it } ?: setTypeface(typeface, textForm.textStyle)
4849
textForm.movementMethod?.let { movementMethod = it }
50+
if (textForm.enableAutoSized) {
51+
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
52+
this,
53+
textForm.minAutoSizeTextSize.toInt(),
54+
textForm.maxAutoSizeTextSize.toInt(),
55+
1,
56+
TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM,
57+
)
58+
}
4959
}
5060

5161
private fun fromHtml(text: String): Spanned? {
@@ -72,14 +82,17 @@ internal fun VectorTextView.applyIconForm(iconForm: IconForm) {
7282
drawableStart = iconForm.drawable
7383
drawableStartRes = iconForm.drawableRes
7484
}
85+
7586
IconGravity.TOP -> {
7687
drawableTop = iconForm.drawable
7788
drawableTopRes = iconForm.drawableRes
7889
}
90+
7991
IconGravity.BOTTOM -> {
8092
drawableBottom = iconForm.drawable
8193
drawableBottomRes = iconForm.drawableRes
8294
}
95+
8396
IconGravity.END -> {
8497
drawableEnd = iconForm.drawable
8598
drawableEndRes = iconForm.drawableRes

0 commit comments

Comments
 (0)