Skip to content

Calculating truncation index not taking into account default React Native word break mode? #27

@roycedot

Description

@roycedot

Am I correct that the calculation for truncationIndex doesn't properly account for the default word break mode for React Native's Text? namely, a word will not be cut partially for line wrap - the entire word will be moved to the next line (at least for iOS - I know you can control this behavior with Android).

This severely undercounts the width (really space/pixels) needed to render some given text:
const { width: totalTextWidth } = await reactNativeTextSize.measure({ text, fontSize: scaledFontSize, fontFamily, fontWeight, });

as a line will be filled with empty space if the last word needs to be moved to the next line.

Example of incorrect calculation(s), current v1.3.0 code and behavior I saw using v1.1.0 for a year+:

bad truncation

Expected (and using my fixes):

Screen Shot 2021-07-02 at 1 16 08 PM

I have modified SeeMoreUtils.js to instead calculate how each line would be rendered, thus taking into account word break where the word is not broken partially. I can post it here, but

  1. please let me know if I'm misunderstanding the current behavior and/or am missing some prop/config that will account for this behavior
  2. or if I should just fork, I can do that as well

Just want to help others, if possible

Basically, my code breaks up the text into lines, accounting for word break moving the last word of a line to the next line if it can't fit, so it better mimics the actual rendering when it does the truncationIndex calculation. Then, I re-assemble the text based on numberOfLines and truncate the last line appropriately to fit the seeMoreText

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions