3

Here's an interesting problem for you:

We are in the process of re-skinning our entire app, which consists of over 100,000 lines of code and almost 100 XIB files. The new design requires (almost) every label in the app to use a new custom font, whereas the old app uses the default font of Helvetica Neue.

What is the most efficient way of changing as many of the UILabels as possible? We don't want to have to manually change thousands of labels both in code and in XIBs (which is especially difficult because we are using a non-system font).

We've discussed swizzling UILabel's creation methods to default to using the new custom font, which would still allow for the few labels that would remain in Helvetica Neue to be customized after creation.

Thoughts?

Senior
  • 2,259
  • 1
  • 20
  • 31

3 Answers3

3

Take a look at NUI https://github.com/tombenner/nui. You can customize a lot controls with it using a CSS-like stylesheet and implement it via subclasses or categories.

artey
  • 1,243
  • 7
  • 10
  • this covers the majority of our cases, but I'm using Martol1ni's answer for the rest. thanks guys! – Senior Jan 03 '13 at 18:02
  • I like this idea but you really have to use NUI from the very start. If you set the font in your nibs as well as your code and then you try to use NUI to white-label your app you can't just blanket change labels because NUI will ignore all the specific fonts you set in code. You'd have to go back and add NUI CSS classes to all your labels where you set the font in code which would be a significant amount of work. – Reid Main Jan 03 '13 at 20:00
2

You should probably subclass UILabel and override either an initmethod of your choice, or awakeFromNib, and set the custom font there. Then you can go through the xib, either manually, or open it up in a text-editor and add a line like

<string key="X.CustomClassName">YourCustomLabelClass</string>

To set the specified label to your custom label class.

Martol1ni
  • 4,684
  • 2
  • 29
  • 39
0

Swizzling is a good option . If you want to do it other way and have problems with custom fonts, I usually make a trivial subclass of UILabel that changes it's font in awakeFromNib for each custom font so I can lay them out in the interface builder.

But... This comes to my mind: [[UILabel appearance] setFont:xxx];

I am not sure how you would deal with keeping the size, though. Perhaps swizzle is the best choice

Fernando Mazzon
  • 3,562
  • 1
  • 19
  • 21
  • I was under the impression that UILabel doesn't support UIAppearance (http://stackoverflow.com/questions/11839044/how-do-i-apply-uiappearance-proxy-properties-to-uilabel) but it does seem to work sporadically. – Senior Jan 02 '13 at 20:26
  • damn, was worth a try. It does seem like the type of problem UIAppearance was conceived to resolve. – Fernando Mazzon Jan 02 '13 at 20:31
  • Definitely; what's strange is that it actually does seem to work for some of the labels, despite UILabel's font property not being marked with UI_APPEARANCE_SELECTOR! – Senior Jan 02 '13 at 20:33