简体   繁体   English

在动态地运行到另一个UILabel之前截断UILabel

[英]Truncate UILabel before it runs into another UILabel dynamically

Basically, I have this situation: 基本上,我有这种情况:

Two UILabels in a UITableViewCell. UITableViewCell中的两个UILabel。 They're both constrained to the top of the cell. 它们都被限制在单元格的顶部。 One is constrained to the left of the cell, and one is constrained to the right of the cell. 一个约束在单元格的左侧,而一个约束在单元格的右侧。

There exists a change that the UILabel on the left can run into the UILabel on the right. 左侧的UILabel可以运行到右侧的UILabel,这是一个变化。 Is there anyway to truncate the text x points before it gets to the UILabel on the right? 无论如何,在到达右侧的UILabel之前,是否要截断文本x点?

Right now, I handle this by giving the UILabel on the left an explicit width that ensures it will truncate before reaching the UILabel, but the explicit width is not dynamic based on screen size. 现在,我通过为左侧的UILabel提供一个显式宽度来确保这一点,以确保在到达UILabel之前将其截断,但是该显式宽度并不是根据屏幕大小动态变化的。 If there is a larger screen size, it might not need to be truncated. 如果屏幕较大,则可能不需要将其截断。 I'm new to iOS development and am not sure how to do this. 我是iOS开发的新手,不确定如何执行此操作。

The best way to achieve this is in auto-layout. 实现此目的的最佳方法是自动布局。 Make sure the label on the right is a fixed size, you can even change it programatically if you need to accommodate to screen size. 确保右边的标签是固定尺寸,如果需要适应屏幕尺寸,甚至可以通过编程方式更改它。 But the thing is this one needs a width constraint. 但问题是这需要宽度限制。 The label on the left does not have a width constraint, instead do a horizontal spacing in front of it to the cell border and from it's trailing end to the start of the label view on the right. 左侧的标签没有宽度限制,而是在其前面到单元格边界以及从其尾端到右侧标签视图的起点之间保持水平间距。 It will widen and shrink to fit the size that's left for it. 它将扩大和缩小以适合剩余的大小。

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

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