79483

Custom Labels Implementation and Use for iOS Apps

Question:

I use Storyboards heavily for my iPhone development. To keep the Labels look the same I create custom UILabels and set the font and size in the custom classes. Then in the Storyboard I assign those classes to the labels displayed in the view.

This works fine but I have 4-5 different kind of labels which only differ by size or weight. How do I deal with this situation? Currently I have the following:

PrimaryLabel PrimaryLabelBold DescriptionLabel DescriptionLabelSmall DescriptionLabelBold

I think this is too much work and these have to be a better way!!

Answer1:

No need to subclass UILabel so many times. Just create one subclass, like so:

<strong>MyLabel.h</strong>

typedef NS_ENUM(NSUInteger, MyLabelStyle) { MyLabelStyleSmall, MyLabelStyleMedium, MyLabelStyleBig, }; @interface MyLabel : UILabel @property (nonatomic) MyLabelStyle style; @end

<strong>MyLabel.m</strong>

#import "UILabel+Styles.h" @implementation UILabel (Styles) - (void)setStyle:(MyLabelStyle)style { switch (style) { case MyLabelStyleSmall: self.font = [UIFont systemFontOfSize:12.0]; break; case MyLabelStyleMedium: self.font = [UIFont systemFontOfSize:17.0]; break; case MyLabelStyleBig: self.font = [UIFont systemFontOfSize:22.0]; break; default: self.font = [UIFont systemFontOfSize:17.0]; break; } } @end

In your storyboard, set the style of a particular label using User Defined Runtime Attributes:

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/Y8Vfn.jpg" data-original="https://i.stack.imgur.com/Y8Vfn.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

2 corresponds to MyLabelStyleBig. Use strings instead of an enum if you want.

Answer2:

You can subclass the UILabel class and than create the constructor method in which you can define the type of label you want ie

PrimaryLabel PrimaryLabelBold DescriptionLabel DescriptionLabelSmall DescriptionLabelBold

For this you can create an enumeration depecting the types of Label's you want. Within the constructor method you can set this enum value.

Happy coding :)

Recommend

  • TableViewCell to auto-resize its height according to content in xcode?
  • How can I calculate font size for my UILabel?
  • Creating a Countdown Watch with a slider in Xcode
  • UILabel in UITableViewCell to make a customised cell without making separate class
  • UILabel in UITableViewCell to make a customised cell without making separate class
  • Adding UITextField to UITableViewCell
  • How do I access text in UITextField from another ViewController (Swift)?
  • Detox: iOS Simulator how to confirm alert message
  • Is there anyway to create a UITableView with one universal footer?
  • UITableView and UILabel repeating
  • Cannot set uilabel font in ios 7( not using custom font)
  • How to use a detail disclosure button in UICollectionViewCell?
  • Pylint warnings on inherited nested class members
  • UITextField — observe changes to selectedTextRange?
  • Grid-lines on a GridView
  • Swift Progress View with NSTimer
  • How can I convert this tuple of tuples into a count of its elements?
  • How to Update UILabel from Another ViewController
  • Is it OK to write code after [super dealloc]?
  • Printing input from TextField to a Label in Xcode with Swift
  • Get UILabel out of UIButton
  • Using same constraints in multiple classes
  • LiveData is abstract android
  • XSLT foreach repeating nodes to flat
  • what makes a request a new request in asp.net C#
  • Set focus to first invalid form element in AngularJS
  • Position: fixed nav does not stay fixed
  • Caching attributes in superclass
  • Atlas images wrong size on iPad iOS 9
  • R - Combining Columns to String Based on Logical Match
  • Get one-time binding to work for ng-if
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Running a C# exe file
  • ActionScript 2 vs ActionScript 3 performance
  • Do create extension work in single-user mode in postgres?
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?