2
votes

I am building an iOS app with Swift. On each of the app pages, we have text labels for different purposes. For example, text labels for titles, text labels for descriptions. And we need to unify the look of texts for each category of purpose.

I can unify their look in interface builder, say for all text titles, set font family to "Helvetica Nueue", and font size to "15". However, when our designer changed font family and sizes, we have to manually modify each of this label settings, which is very inefficient.

A direct approach to solve this would be, define the font family and size as constants in a utility class, then in the code behind, set all the text style after an app page is loaded. In this case, when there is any change, we'd only need to update the constants in the utility class. However, this requires we adding references for every text label in our code behind from interface builder as well, which also requires lots of work, and it is not efficient enough.

Any better ideas to do it smartly and efficiently? Appreciate your suggestions!

1
Very good question!meaning-matters

1 Answers

1
votes

Here's an idea:

  • Define a set of UIView tag values for each different label/textfield/textview/... style.
  • Then in Interface Builder set the UI labels/textfields/textviews/... to an approximate style so that you get a good feel of how things will look and size. But also set the tag value matching the appropriate style as defined.
  • Sit down with the designer to define a future proof granularity. I mean, two labels could have the same style now, but are (or might be in foreseeable future) actually perceived as differently by the designers; so the same style used could just be a 'coincidence'.
  • Subsequently create a module that defines these styles.
  • In that module create a method that traverses the view hierarchy and sets the appropriate style according to the tag value you've assigned.
  • If you want to style different UI element types (e.g. UILabel, UITextField and UIButton) it's most probably to simply use different tag ranges for each type because chances are high they will be perceived as different at some point by designers.
  • In your code, all you have to do is call this above method for each top-level view you have in for example a viewWillAppear, ... depending on if you're dealing with a view controller, table view cell, custom view, ...

I hope this helps, or that it gives you some direction that leads to a solution that fits your needs and preferences.

Other idea:

  • Make a subclass for each foreseeable label style in which you set all properties you wish.
  • In Interdace Builder change the type of the UI element to that of its appropriate subclass.

Or, a combination:

  • Make one subclass for each UI element type you need to style.
  • In IB change the type as above.
  • In IB set the tag to choose a particular style.
  • This saves you from having 'many' subclasses that are just there to hold & set one style.
  • This would keep all the styles (per UI element type) together in one file, which is nice for comparison and updating.