简体   繁体   中英

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]!

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)
}

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)

property observer.

@IBOutlet weak var collectionNameLabels: [UILabel]! {

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM