0

The image attached says it all. Is there any way to prevent the title from overlapping such as automatically moving to a second line whenever it reaches its limit? programatically/through interface builder.

P.S: im retrieving the names from firebase database. this is a voting page and title are the booth names

TableView Controller

import UIKit
import FirebaseDatabase

var pitchData = [String]()
var myIndex = 0
class PitchTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.setNavigationBarHidden(false, animated: true)

        self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont(name: "Gotham Rounded", size: 19)!]
        ref = Database.database().reference() //set the firebase reference
        // Retrieve the post and listen for changes
        databaseHandle = ref?.child("Afternoon13").queryOrdered(byChild: "BoothPosition").observe(.value, with: { (snapshot) in
            pitchData.removeAll()

            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let key = snap.key


                pitchData.append(key)

            }
            self.tableView.reloadData()
        })
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return pitchData.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.font = UIFont(name: "Gotham Rounded", size: 20)
        cell.detailTextLabel?.font = UIFont(name: "Gotham Rounded", size: 20)
        cell.textLabel?.text = pitchData[indexPath.row]

        switch (pitchData[indexPath.row]) {
        case "ARC - attract, retain and cultivate":
            let returnValue: Int = UserDefaults.standard.integer(forKey: "voting")
            if (returnValue == 1)
            {
                cell.detailTextLabel?.text = "Completed ✓"
                cell.detailTextLabel?.textColor = UIColor.green
                cell.isUserInteractionEnabled = false
            }
            else {
                cell.detailTextLabel?.text = "Vote ᐳ"
                cell.detailTextLabel?.textColor = UIColor.blue
            }
        case "Anytime RCs":
            let returnValue: Int = UserDefaults.standard.integer(forKey: "voting1")
            if (returnValue == 1)
            {
                cell.detailTextLabel?.text = "Completed ✓"
                cell.detailTextLabel?.textColor = UIColor.green
                cell.isUserInteractionEnabled = false
            }
            else {
                cell.detailTextLabel?.text = "Vote ᐳ"
                cell.detailTextLabel?.textColor = UIColor.blue
            }

enter image description here

Kinja
  • 449
  • 5
  • 22

5 Answers5

0

You need to set lines to 0:

enter image description here

You can set it directly from StoryBoard:

or you can set Programmatically:

cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0

Hope this Help.

Edit : Demo of the TableView Constraints

I have taken two labels in the cell next to each other:

This is my Output of TableView:

enter image description here

This is constraint of label First:

enter image description here

This is the Constraints of label 2:

enter image description here

And yes it worked for me, The text is multi line with no text cutting, please double Check your constraints with this.

Edit To see the Constraints

Select the label and then Select the Show the Size Inspector:

Scroll Down and you can see the given constraints.

enter image description here

Edit 2

As per you description you have Auto resizing:

So you can use the following :

For label One

enter image description here

For label second:

enter image description here

Abhirajsinh Thakore
  • 1,806
  • 2
  • 13
  • 23
0

Since you are using default UITableViewCell's textLabel, this should fix everything :

cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = .byWordWrapping;  
Nitish
  • 13,845
  • 28
  • 135
  • 263
0

You set your UILabel property numberOfLines = 0

enter image description here

  1. set UILabel Top , bottom , leading , trailing Constraints.

enter image description here

  1. add UITableViewDelegate method.

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    
  2. also set your vote Constraints like this.

enter image description here

  1. select your vote UIButton set Constraints

    1. step set Top , bottom , leading , trailing and fix width.

enter image description here

  1. step set vertically center Constraints

enter image description here

Dixit Akabari
  • 2,419
  • 13
  • 26
0

Make sure three things:-

  • Title label has bottom, top and leading constraint with cell content view as well as horizontal spacing with Vote >, and Vote > button has all constraint like top bottom leading and horizontal space with title.
  • Title label has set numberOfLines = 0
  • Set dynamic height of cell using UITableViewAutomaticDimension in heightForRowAt

    func tableView(_ tableView: UITableView, heightForRowAt 
    indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    } 
    
Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19
0

Let try with code

// in view did load:
self.tableView.rowHeight = UITableViewAutomaticDimension

// cell row at indexpath
let reuseIdentifier = "cell"
        var cell:UITableViewCell? =
            tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
        if (cell == nil)
        {
            cell = UITableViewCell(style: .value1,
                                   reuseIdentifier: reuseIdentifier)
        }

        // your customize font, color... here

        cell!.textLabel?.numberOfLines = 0;
        cell!.detailTextLabel?.numberOfLines = 0;

        return cell!
Scott.N
  • 172
  • 2
  • 17