Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I increase the font size via HTML for compose Text? #3

Open
jayesh83 opened this issue Nov 10, 2023 · 2 comments
Open

How do I increase the font size via HTML for compose Text? #3

jayesh83 opened this issue Nov 10, 2023 · 2 comments

Comments

@jayesh83
Copy link

No description provided.

@Aghajari
Copy link
Owner

Hello @jayesh83

Android's Html class does not offer direct support for setting an exact font size. Nevertheless, there are several alternative approaches to achieve the desired result:

  1. Relative Font Sizes:
    You can use HTML tags like <big> and <small> for relative font sizes.
    Additionally, heading tags such as <h1>, <h2>, etc., offer varying font sizes.
  2. Custom Tag Handler:
    For more precise control over font size, consider creating a custom tag handler.
    Here's an example:
class CustomTagHandler : Html.TagHandler {

    private var start = Stack<Int>()

    override fun handleTag(
        opening: Boolean,
        tag: String,
        output: Editable,
        xmlReader: XMLReader
    ) {
        if (tag.equals("test", ignoreCase = true)) {
            if (opening) {
                start.push(output.length)
            } else if (start.isNotEmpty()) {
                output.setSpan(
                    AbsoluteSizeSpan(30, true),
                    start.pop(),
                    output.length,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                )
            }
        }
    }
}

Usage:

@Preview
@Composable
fun TestPreview() {
    AnnotatedText(
        text = "Hello <test>World!</test>".fromHtml(
            tagHandler = CustomTagHandler()
        )
    )
}

@jayesh83
Copy link
Author

@Aghajari thanks for writing back.

one question, how can I make it configurable? Like for my use case I'd like to have say below custom tag

<fontSize value="18sp">12/20</fontSize> Orders Completed

can you help with like how do I also parse the value out of my custom handler?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants