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

feat: letter spacing #128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ fun MarkdownText(

with(style) {
applyTextAlign(textAlign)
applyLetterSpacing(letterSpacing)
fontStyle?.let { applyFontStyle(it) }
fontWeight?.let { applyFontWeight(it) }
fontFamily?.let { applyFontFamily(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.ui.text.font.createFontFamilyResolver
import androidx.compose.ui.text.font.resolveAsTypeface
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.TextUnit
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.doOnNextLayout
import androidx.core.widget.TextViewCompat
Expand Down Expand Up @@ -94,6 +95,16 @@ fun TextView.applyTextAlign(align: TextAlign) {
}
}

fun TextView.applyLetterSpacing(letterSpacing: TextUnit) {
if (letterSpacing != TextUnit.Unspecified) {
if (letterSpacing.isSp) {
setLetterSpacing((letterSpacing.value * 0.0624).toFloat())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what 0.0624 means? where is it coming from?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I was using a value used by others here https://chrisdavies.github.io/sp-to-em/ which seemed to be fairly consistent across screen sizes.

I've ended up using another custom solution over this project as a result of letter spacing not being consistent with existing behaviour (as a result of letter spacing only supporting EM for AndroidViews out of the box)

} else {
setLetterSpacing(letterSpacing.value)
}
}
}

fun TextView.enableTextOverflow() {
doOnNextLayout {
if (maxLines != -1 && lineCount > maxLines) {
Expand Down
Loading