简体   繁体   English

Swift:UINavigationItem显示截断的文本

[英]Swift: UINavigationItem displaying truncated text

Is there an option that gets the UINavigationItem text to truncate dynamically based on the view type? 是否有一个选项可以根据视图类型动态截断UINavigationItem文本?

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): 我设法为的UILabel做到这一点通过设置断线之后 自动收缩属性(但无法找到在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..). 编辑 :我在Swift中为String写了一个truncate扩展,但它似乎不是一个优雅的解决方案,因为要正常工作,它需要知道上下文(例如显示大小,屏幕旋转等)。 This context is handled automatically in the UILabel class, but not in the ** UINavigationItem** one (unless there is some trick that I can use). 这个上下文在UILabel类中自动处理,但不在** UINavigationItem **中处理(除非我有一些技巧可以使用)。

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 您可能正在寻找可变宽度表示字符串,如下所述: 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. 为了举例说明这个问题,想象一下iOS应用程序意味着可以在不同的屏幕尺寸和/或不同的方向上运行。 Now, say there was some text, “Welcome to the Apple Store.” At times, that text may fit perfectly well in the available space. 现在,请说有一些文字,“欢迎来到Apple Store。”有时,该文本可能非常适合可用空间。 For others, it may be too long and it'd be desirable to shorten it to something like “Welcome." 对于其他人来说,它可能太长了,并且希望将其缩短为“欢迎”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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