0

I have a UITableViewCell that have a Label contained within the cell. The problem is that i wish to change the font to "Century Gothic" however when I set the label to "Century Gothic" using the storyboard this does not work. The font seems to be stuck on the default "Avenir" font. I set label to Attributed in the label settings in order to select the font"Century Gothic" from the drop down menu.

I have tried to set it in code however this does not seem to work either. when I set the font to "Avenir" in code it will allow me to adjust the size of the font. However when I set the font to "Century Gothic" in code it will not produce the "Century Gothic" or let me adjust the size of the font.

I have tried both adjusting the font using storyboard settings and code.

//setting up tableview rows
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return foodTitleArray.count
}


//what cell is & how it looks
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = Tableview.dequeueReusableCell(withIdentifier: "cell123") as! UITableViewCell1

    //How text looks in tableview sections
    cell.foodTitle.font = UIFont(name: "Century Gothic", size:58)
    cell.foodTitle.textColor = UIColor.white
    cell.backgroundColor = UIColor(red: 211/255, green: 211/255, blue: 211/255, alpha: 1)


    return (cell)
}

enter image description here

enter image description here

kitchen800
  • 197
  • 1
  • 12
  • 36
  • have you added your font into "font provided by application" in the info.plist? – Alastar Oct 30 '19 at 13:38
  • You're dequeueing a UITableViewCell, but trying to set the `foodTitle` so it seems like you're trying to use a custom cell. Maybe try setting the cell class in storyboard and set the correct type when you dequeue it in code? – Rachel Harvey Oct 30 '19 at 13:46
  • Potential issue: `UIFont(name: "Century Gothic", size:58)`, `Century Gothic` isn't the postscript name of the font. Potential issue: you are mixing `.text` and `.attributedText`. What is `foodTitle` in a `UITableViewCell`. In a custom one maybe? – Larme Oct 30 '19 at 15:06

2 Answers2

0

You are casting the cell as a UITableView cell when you dequeue it, so it doesn't have a foodTitle label.

You need to cast it as your custom cell type, then you will be able to access your fields.

Better still register is as the correct cell type in viewDidLoad...

tableView. register(MyCustomCellClass.self, forCellReuseIdentifier: "MyCellID")

and then you can use the newer API for dequeueing it

let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellID", for: indexPath)

and there is no need to cast it at all.

If you want to be really smart, add a static property to the customer cell and use that for the cell ID:

class MyCustomCell: UITableViewCell {
   static let cellID = "cellIDString" //can be anything as it's never seen
   // all the existing code...
}

and then use it as

MyCustomCell.cellID

whenever the cell identifier is needed. This gets rid of the string literals, helps readability, and removed typo errors.

flanker
  • 3,840
  • 1
  • 12
  • 20
  • Ah, you've added the info re storyboards. In that case the registering the cell is not needed, but the rest still applies. – flanker Oct 30 '19 at 13:50
0

https://gist.github.com/tadija/cb4ec0cbf0a89886d488d1d8b595d0e9 may help you. It has all fonts' names that can be used in UIFont(name: ,size: )