3

I have a UITableviewCell with multiple lines of text. I am trying to figure out how to make each line a different font size.

Here is the code I am using to populate the table cell:

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("eventCell")!
        cell.textLabel!.textAlignment = NSTextAlignment.Center
        cell.textLabel!.numberOfLines = 0
        cell.textLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping

        cell.textLabel!.text = self.eventsToday[indexPath.row].eventTitle + "\n" + self.eventsToday[indexPath.row].eventPlaceName

        return cell
    }

Is there a way to reduce the font size after the "\n" line break so that the string labeled "eventPlaceName" will show up as smaller than the one labeled "eventTitle?"

kamisama42
  • 595
  • 4
  • 18
  • Check this answer - http://stackoverflow.com/questions/1417346/iphone-uilabel-containing-text-with-multiple-fonts-at-the-same-time – RJR Dec 24 '15 at 06:45
  • you can use attributed string for attributed string example you can see at below [enter link description here](https://stackoverflow.com/questions/24666515/how-do-i-make-an-attributed-string-using-swift) – Jaydeep Patel Dec 24 '15 at 06:46

2 Answers2

1

Use attributedText property of textLabel object.

Using attributedText you can format your text.

For more, see NSAttributedString

san
  • 3,350
  • 1
  • 28
  • 40
1

Is there a way to reduce the font size after the "\n" line break so that the string labeled "eventPlaceName" will show up as smaller than the one labeled "eventTitle?"

This sounds exactly like your table view cell needs TWO SEPARATE labels:

  1. One for the "Event title"
  2. One for the "Event place"

I mean, you could use an attributed string, but clearly these are two separate pieces of information and you have a repeating pattern here. Two labels sounds like the best practice / less headaches down the road, in my opinion.


How to add an extra label:

  1. Subclass UITableViewCell (Let's call the custom subclass CustomTableCell in the code below).
  2. Set the class of your prototype cells to your custom subclass, in the storyboard. Also set the reuse identifier (here I used "YourCellIdentifier").
  3. Forget the original label (it doesn't show in the storyboard and therefore you couldn't position your second label relative to it); instead add TWO new labels to the cell, and setup outlets with the custom cell subclass. Name the properties (e.g.) eventTitleLabel and eventPlaceLabel. Setup any layout constraints you may need, and configure the font sizes, colors, etc.

  4. At runtime, set the labels' text like so:

    let cell = tableView.dequeueReusableCellWithIdentifier("YourCellIdentifier", forIndexPath: indexPath) as! CustomTableCell
    // (forced cast shouldn't fail f identifier is registered properly)
    
    cell.eventTitleLabel.text = "Event Title XXXX"
    cell.eventPlaceLabel.text = "Event Place YYYY"
    
    return cell
    
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189
  • This looks like it may be the most convenient solution. One question though - how to I create a new label in the cell? Should I make it in the main storyboard and then connect it to the View Controller or is there a line of code that will work for this? – kamisama42 Dec 24 '15 at 06:53
  • See my edited answer for details on how to achieve that. – Nicolas Miari Dec 24 '15 at 07:08