简体   繁体   中英

How to apply the same configuration to some UILabel in iOS?

I am tired of setting property values to each UILabel in the IB. Is extending the UILabel the only possible way? or there exists some other way to do it?

  • Assign all the properties to one UILabel
  • Copy paste the label as many times you want in storyboard or xib
  • New labels will also get the same properties, you just require to arrange their positions
  • Then you can go ahead with IBOutlets and manipulate with those

This applies to all the widgets in storyboard/xib.

Also as @dehlen metioned,

If you are using codebase for UI then, Custom Button Class

  • Inherit UIButton and create a subclass CustomButton
  • In its init method, assign all the properties which you require, like font, color etc
  • And assign frame value at use-time, through initWithFrame method. Or
  • Add button in storyboard/xib, but in its class property change class name to CustomButton

Thanks @dehlen, You are right, I forgot to mention code side implementation.

You could create an IBOuletCollection for the labels and set the properties programmatically (in viewDidLoad for example) using:

[self.outletCollection setValue:whatever forKey:something];

You can also call makeObjectsPerformSelector on the collection in order to send messages to each label.

After setting the property of a UILabel make copy of it by

Press and hold alt and drag and drop UILabel to another place it will create new copy of that UILabel with same properties.

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