6

I have a cell which may have to display a long text. In this case it should wrap to a second line.

I have Lines set to 2 and Line Break set to Word Wrap in the attributes Inspector. The Interface Builder preview confirm that and behaves as expected/desired.

enter image description here

When building and running however the text is limited to one line and truncuates at the tail: enter image description here

Other changes in the attribute inspector seem to have no effect aswell (text alignment for example)!

Marmelador
  • 947
  • 7
  • 27

6 Answers6

8

If anyone is still having the problem.. Here's the solution for swift 4 Xcode 9 iOS 11

inside the cellForRowAt tableview function you just have to add a single line of code

cell?.textLabel?.numberOfLines = 0
6

For getting the lines without the dots, Do the following -

  1. Take the UILabel inside the cell, go to the attribute inspector.
  2. In the attribute inspector find lines and change it to 0.
  3. Then, change the line break to WordWrap

I think this will work.

Vishnu Prasannan
  • 231
  • 1
  • 11
5

Swift 3

Add Two Methods in tableview.

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
                return UITableViewAutomaticDimension
 }

 func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
                return UITableViewAutomaticDimension
 }
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
Jay
  • 686
  • 1
  • 4
  • 16
  • But why is it neccessary to change the heigth of the row, when I only want to change the line breaking. Also, other changes in the attribute Inspector to the cell don't seem to have any effect either. – Marmelador Jul 11 '17 at 10:55
  • You are use autolayout or size class? – Jay Jul 11 '17 at 11:00
0

You can implement automatic cell sizing depending on the label's text size

  1. In your cell, add constraints for Top and Bottom of UILabel, but not for cell height or label height
  2. In your table view set
    tableView.rowHeight = UITableViewAutomaticDimension

This will make the cells resize according to label's contents

mag_zbc
  • 6,801
  • 14
  • 40
  • 62
0

If you want to have multiple lines in label so in attributes inspector set lines to 0 and implement

tableView.rowHeight = UITableViewAutomaticDimension
Cœur
  • 37,241
  • 25
  • 195
  • 267
Develen
  • 1
  • 1
0

The constraints might not have set properly for the UILabel. So, the UILabel will keep on expanding as the text increases.

  • Set the UILabel constraints with autolayout constraints or give a static width (or left and right properties).
  • Now in label attributes inspector set lines to 0
jafarbtech
  • 6,842
  • 1
  • 36
  • 55