Sometimes it’s necessary to change the font on an app and you don’t want to go into Interface Builder and manually change them all. Ug.
So you can recursively call a method to change the font on all subviews that respond to the setFont: method.
+(void)changeFontOnSubviews:(UIView*)view font:(UIFont*)font color:(UIColor*)color; { // 1. Set font on this control if ([view respondsToSelector:@selector(setFont:)]) [view performSelector:@selector(setFont:) withObject:font]; // 2. Set the text color (if color is passed in) if (color && [view respondsToSelector:@selector(setTextColor:)]) [view performSelector:@selector(setTextColor:) withObject:color]; // 3. Variation: UIButton title color if (color && [view isKindOfClass:[UIButton class]]) [(UIButton*)view setTitleColor:color forState:UIControlStateNormal]; // 4. Other vaiations? // 5. Do the same for subviews for (UIView *v in view.subviews) [MBConstants changeFontOnSubviews:v font:font color:color]; }
- Check that it responds to the selector and call it. Use ‘performSelector:’ b/c UIView doesn’t respond to setFont: so this avoids warnings.
- Set the color (if one is passed in).
- UIButtons ‘font’ color method is based on title
- There may be other controls you may have to handle differently.
- Do the same on all subviews to cover them all.