Created
August 4, 2022 06:29
-
-
Save shibbirweb/d0925bf63ff8084d4898e9557ce812fa to your computer and use it in GitHub Desktop.
Android: Dynamically set font for ASCII and UNICODE text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @BindingAdapter(value = ["android:fontFamilyByText"]) | |
| fun TextView.customFontFamilyByText(text: String?) { | |
| val isAscii = Charsets.US_ASCII.newEncoder().canEncode(text) | |
| val fontFamily = if (isAscii) { | |
| ResourcesCompat.getFont(context, R.font.inter_variable_font_slnt_wght); | |
| } else { | |
| ResourcesCompat.getFont(context, R.font.noto_sans_bengali_variable_font_wdth_wght); | |
| } | |
| setTypeface(fontFamily, typeface.style) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <TextView | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="Any content" | |
| android:textSize="60sp" | |
| android:fontFamilyByText="@{`Same content as text for checking text is unicode or ascii`}" | |
| app:layout_constraintBottom_toBottomOf="parent" | |
| app:layout_constraintEnd_toEndOf="parent" | |
| app:layout_constraintStart_toStartOf="parent" | |
| app:layout_constraintTop_toTopOf="parent" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment