2

I have a number of UILabel elements in my app and I'm looking for an easy way to style them using the UIAppearance Proxy object. My current approach is to create different subclasses of UILabel equipped with an UI_APPEARANCE_SELECTOR decorator in order to be accessible though [UILabelSubclass appearance] calls. You can find the sources I'm using as reference here and here. The problem is that I don't want to set the font size but only the font family relying on the size defined in the Storyboard.

How can I set this in the subclass?

Community
  • 1
  • 1
Claus
  • 5,662
  • 10
  • 77
  • 118

3 Answers3

7

Here's a simple category that works with UIAppearance proxies:

@interface UILabel (FontName)

- (void)setFontName:(NSString *)name UI_APPEARANCE_SELECTOR;

@end


@implementation UILabel (FontName)

- (void)setFontName:(NSString *)name {
    self.font = [UIFont fontWithName:name size:self.font.pointSize];
}

@end
Robert Wijas
  • 693
  • 7
  • 14
  • this is a great solution too. I flagged the other one just because was a bit closer to what I was implementing. – Claus Jul 03 '13 at 09:43
1

I think you can try in your subclass something like:

UIFont *newFont = [UIFont fontWithName:@"YourFontName" size:self.font.pointSize];
self.font = newFont
danypata
  • 9,895
  • 1
  • 31
  • 44
1

Thanks to this post, and also a blog post from Peter Steinberger (http://petersteinberger.com/) I was able to get something together that works for me:

@interface UILabel (FontAppearance)
@property (nonatomic, copy) UIFont *appearanceFont UI_APPEARANCE_SELECTOR;
@end


@implementation UILabel (FontAppearance)
- (void)setAppearanceFont:(UIFont*)font
{
    [self setFont:font];
}

-(UIFont*)appearanceFont
{
    return [self font];
}
@end
Michael
  • 1,213
  • 6
  • 9