0

I have a custom cell for a tableView in which I have added a few labels. for this custom cell I also created a Swift file for it.

Say, I have the following labels:

LabelShop
LabelAddress
LabelZipcode

Problem: How to set up San Francisco Font for these Lables which inside the custom cell? Set up inside the ViewController?

Example:

LabelShop.font = UIFont.systemFont(ofSize: 21.0 , weight: UIFontWeightSemibold)

Please kindly extend your assistance.

Thanks

MilkBottle
  • 4,242
  • 13
  • 64
  • 146
  • Please check https://stackoverflow.com/questions/46396260/in-swift-how-can-i-change-an-attribute-of-part-of-a-string/46398951#46398951 – Vini App Oct 05 '17 at 06:00

1 Answers1

1

You can setup this font from tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) function in view controller or directly from cell itself in awakeFromNib.

This code should be sufficient: label.font = UIFont.systemFont(ofSize: 21.0 , weight: UIFontWeightSemibold).

First approach:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: YOURCELLIDENTIFIER, for: indexPath)

   if let castedCell = cell as? YourCellClass {
       castedCell.label.font = UIFont.systemFont(ofSize: 21.0, weight: UIFontWeightSemibold)
   }

   return cell
}

Second:

override func awakeFromNib() {
   super.awakeFromNib()
   label.font = UIFont.systemFont(ofSize: 21.0 , weight: UIFontWeightSemibold)
}
K. Michał
  • 1,033
  • 6
  • 17