1

Is there an option that gets the UINavigationItem text to truncate dynamically based on the view type?

Those are the only properties I can edit:

enter image description here


I managed to do so for the UILabel by setting the Line Break and Autoshrink property (but can't find an equivalent one in the UINavigationBar):

enter image description here


EDIT: I wrote a truncate extension for String in Swift, but it doesn't seem an elegant solution as, to work properly, it would need to know the context (e.g. display size, screen rotation etc..). This context is handled automatically in the UILabel class, but not in the ** UINavigationItem** one (unless there is some trick that I can use).

mm24
  • 9,280
  • 12
  • 75
  • 170
  • `UINavigationBar` uses `UINavigationItem` not `UILabel` for title – NSDmitry Jan 16 '17 at 10:48
  • Sorry, I wrote it incorrectly. Edited now. The question should still be valid as there is no such property in UINavigationItem. Any idea on how to achieve this behaviour? (I am currently using a String extension but would be great to have a way to determine the maximum String length that I can display within a UINavigationItem - this needs to be dynamic, e.g. based on current orientation and string font / size) – mm24 Jan 16 '17 at 10:57
  • you can add custom view in navigation item **titleView** may help see this for ref http://stackoverflow.com/questions/12502101/button-in-the-middle-of-navigationbar – Prashant Tukadiya Jan 16 '17 at 11:07

1 Answers1

0

You may be looking for Variable Width Presentation Strings, as explained here: https://developer.apple.com/library/content/releasenotes/Foundation/RN-Foundation/index.html#10_11VariableWidthString

The idea is that you are able to adjust your string in real time by way of a table of widths and alternative strings. The document posits a use case that sounds very similar to yours:

To exemplify the problem, imagine an iOS app meant to run on different screen sizes and/or different orientations. Now, say there was some text, “Welcome to the Apple Store.” At times, that text may fit perfectly well in the available space. For others, it may be too long and it’d be desirable to shorten it to something like “Welcome."

matt
  • 515,959
  • 87
  • 875
  • 1,141