简体   繁体   中英

How can I truncate a UILabel next to a UIImage using autolayout/storyboard?

I have a UILabel next to a UIImage where I want the UILabel to be show truncated "..." if it starts to overlap with the neighboring UIImage.

Text is behind the UIImage when i want it to just truncate:

文字在图像后面

My UILabel properties: ui标签属性

My image properties: 图像属性

I thought the issue is the content compression but I set the UILabel's content compression resistant priority horizontal to 250 and the UIImage to 1000. This only hides the UILabel behind the UIImage.

*This is inside a table cell. I'm not using the default table cell view because i intend to customize it further.

从UILable到UIImage的尾随添加常量值5,并且关系小于和等于(<=)。

Three things can be done.

  1. Provide a trailing space for the label to the image view. This will make sure that the label always gets truncated if text grows up.
  2. Provide a trailing space for the label to the cell's content view with the constraint constant set to the image view plus margins, if any.
  3. Keep the current configuration but take an IBOutlet and set its preferred width(found in 2nd last tab of the navigator.) to the screen width minus the image width. This will make sure that your label grows with the text but never exceeds the image.

Add constraint between:

1) UIImage and the table cell (the container view)

2) UILabel and the UIImage

3) and finally one between tableCell and UILabel

This way you will have your UILabel stretching from the left side of the cell all the way to the image view, and truncate the text that doesnt fit into the label.

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