简体   繁体   中英

Swift: UINavigationItem displaying truncated text

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:

在此输入图像描述


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):

在此输入图像描述


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 (eg 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).

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."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM