question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Text in a custom TitleBar component does not truncate on iOS

See original GitHub issue

Hi there!

We are using the TitleBar component to build a custom title bar in our app. The main part of this title is a Text component, similar to what is in the zoom example.

However, for our use case, this text is dynamic/user-generated and can get quite long. When that happens, we cannot truncate the text properly on iOS. From the zoom example:

<TitleBar style={styles.titleBar}>
  <Text style={styles.titleBarText} numberOfLines={1}>
    Erat curabitur elit cursus est in feugiat gravida, habitant hac
    morbi urna eget per sapien, enim dictum maximus fringilla fames
    suscipit.
  </Text>
  <View style={{backgroundColor: color, width: 28, height: 28}} />
</TitleBar>

My expectation is that by setting numberOfLines={1} we should get truncated text, as per the docs: https://reactnative.dev/docs/text#numberoflines. This behaviour is present outside of the TitleBar.


My hunch is that this has something to do about the TitleBar not properly reporting its layout to RN. When I set a breakpoint in the NVTitleBarView I can see that the frame starts quite large and then shrinks to fit the available space. Maybe RN is not receiving the newly shrunken frame?

For instance, if I add a background to just the TitleBar, it doesn’t appear to run off the page either:

and the Layout Inspector seems to confirm that:

Screenshot 2021-04-26 at 18 34 45

As a way of hacking around it, I attempted to add an onLayout prop to the TitleBar. However, it only receives one event - and it’s the original big frame, not the “shrunken frame”.


I will dig into this some more, but please do let me know if you have any ideas!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
grahammendickcommented, Apr 29, 2021

Thanks for testing the changes and adding them to the PR

0reactions
salockhartcommented, Apr 29, 2021

Oh well done, that’s a great solution!

I’ve tested it on my end, portrait/landscape with both using the TitleBar as well as just the title prop - looks to be working great. I’ll add these changes to my PR

Read more comments on GitHub >

github_iconTop Results From Across the Web

UINavigationBar title gets truncated when using custom font
Not an answer, but an observation from my own experience: I've tried using a custom font in a NavigationBar too and it appears...
Read more >
Text fields - Selection and input - Human Interface Guidelines
A text field is a rectangular area in which people enter or edit small, ... this result automatically, so you won't need to...
Read more >
Cards - Lightning Design System
The card body accommodates any layout or design, as long as its a grouping of related information. Styling Note. By default, the body...
Read more >
App bars: top - Material Design
Truncated “Vacation photos” text in title. Don'tDon't truncate text, hiding the full title may cause misunderstanding. Top app bar with shrunk text size...
Read more >
Explore text properties - Figma Help Center
Adjust the letter case. Choose from uppercase, lowercase, capitalize, and small caps. If available, you can also toggle case-sensitive forms and capital spacing ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found