Coder221
Coder221

Reputation: 1433

Setting font for collection of labels

I have a nameLabel and I can set the font as

 @IBOutlet weak var nameLabel: UILabel!
 nameLabel.font = UIFont(name: "Georgia-bold", size: 18)

Is there a way, I can do same for collection of labels at once,

@IBOutlet weak var collectionNameLabels: [UILabel]!

Upvotes: 0

Views: 255

Answers (3)

Yongjoon
Yongjoon

Reputation: 104

property observer.

@IBOutlet weak var collectionNameLabels: [UILabel]! {

didSet{
  for nameLabel  in collectionNameLabels{
    nameLabel.font = UIFont(name: "Georgia-bold", size: 18)
  }
}

}

Upvotes: 0

Luzo
Luzo

Reputation: 1374

If you want to change it more than once, it might be useful to create property for that.

var labelFont: UIFont! { didSet { labels.forEach { $0.font = labelFont } } }

and in viewDidLoad or after set the font:

labelFont = UIFont(name: "Georgia-bold", size: 18)

Upvotes: 2

trungduc
trungduc

Reputation: 12154

If you want to change font of all labels which is created from xib or storyboard in your app, you can use

UILabel.appearance().font = UIFont(name: "Georgia-bold", size: 18)

But if you only want to change font of collectionNameLabels, as @Sivajee Battina said, i think you have to use a loop to change font for each label.

for label in self.collectionNameLabels {
    label.font = UIFont(name: "Georgia-bold", size: 18)
}

Upvotes: 3

Related Questions