Marmelador
Marmelador

Reputation: 1017

Text in table view cell not wrapping to second line

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

Upvotes: 6

Views: 6284

Answers (6)

jafarbtech
jafarbtech

Reputation: 7025

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

Upvotes: 0

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

Upvotes: 8

Develen
Develen

Reputation: 1

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

tableView.rowHeight = UITableViewAutomaticDimension

Upvotes: 0

Vishnu Prasannan
Vishnu Prasannan

Reputation: 57

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.

Upvotes: 3

mag_zbc
mag_zbc

Reputation: 6992

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

Upvotes: 0

Jaykumar Patel
Jaykumar Patel

Reputation: 684

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
 }

Upvotes: 5

Related Questions