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

Vertical and Horizontal space around the text are used additional values #1

Open
XFRAY opened this issue May 21, 2022 · 4 comments
Open

Comments

@XFRAY
Copy link

XFRAY commented May 21, 2022

As I see you use some additional values to measure vertical and horizontal padding

val verticalSpaceAroundText = (textHeight * .12f + 6 + paddingVertical).toInt()

What is 6 and .12 here ?

Same for
val horizontalSpaceAroundText = ((textHeight * .24f) + 8 + paddingHorizontal).toInt()

What is 8 and .24 here ?

@XFRAY XFRAY changed the title Vertical and Horizontal space around the text additional values Vertical and Horizontal space around the text are used additional values May 21, 2022
@SmartToolFactory
Copy link
Owner

SmartToolFactory commented May 21, 2022

Those are the base values to use when fontSize, or text height, is too small. 24% or 12% of small text height values doesn't look good. You can test it out by removing static values and use 12.sp or less. If you can come up with better formula for paddings for any size of text or paddings, feel free to open a PR.

@XFRAY
Copy link
Author

XFRAY commented May 21, 2022

6 and 8 are used also by that reason ?

@SmartToolFactory
Copy link
Owner

SmartToolFactory commented May 21, 2022

Yes, but i think it would be better to changed them with Float.coerceAtLeast as ((textHeight * .12f).coerceAtLeast(Some number) + paddingVertical).toInt() would be better

@XFRAY
Copy link
Author

XFRAY commented Jun 2, 2022

I looked into this and your "magic" values would work only with default Android fonts.
During testing it I figured out that would be better to use
val textHeight = (paint.fontFetrics.bottom - paint.fontFetrics.top)

Then val verticalSpaceAroundText = textHight - textSize
Then we need to calculate horizontalSpace to make background a circle if text.lenght == 1 otherwise rect

val paddedTextWidth = (textWidth + spaceAroundText * 2 + paddingStart + paddingEnd).toInt()
val desiredHeight = (textHeight + spaceAroundText + paddingTop + paddingBottom).toInt()
val desiredWidth = paddedTextWidth.coerceAtLeast(desiredHeight)

I used all space as a left and right padding to expand a rect
otherwise it will be smaller then circle shape when there is text.length == 2

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