Multinerd Multinerd - 5 months ago 25
iOS Question

__NSPlaceholderDictionary crash when overriding UIFonts

i am overriding all fonts in my app using the following code. it took me hours to figure this out but with the fonts overridden, normal functionality would break (ex: long press gesture on a

UITableViewCell
), and all instances of
UIBarButtonItem
truncated showing only
...
in the nav bar.

the crash always resulted in


* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'


//.h
@interface UIFont (SystemFontOverride)
@end

//.m
@implementation UIFont (SystemFontOverride)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:@"HelveticaNeue-Regular" size:fontSize];
}

+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:@"HelveticaNeue-Thin" size:fontSize];
}

+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:@"HelveticaNeue-ThinItalic" size:fontSize];
}
#pragma clang diagnostic pop

@end


am i doing something wrong? is there a better way to override fonts without manually setting each label, segment control, etc... this app wont be on the app store so im open to ANY suggestions.

bonus question,
#pragma clang diagnostic pop
and
#pragma clang diagnostic push
. i understand its saving the state of the compiler, silence a warning, and then revert the state. can i get away with just
#pragma clang diagnostic ignored
?

Answer

for the sake of marking this as answered, im following rmaddy's comment

Instead of trying to replace the standard UIFont methods in a category, add your own methods to the category. Example - instead of overriding boldSystemFontOfSize:, name it myBoldSystemFontOfSize:. Or use UI_APPEARANCE or change a label's font size using label.font = [label.font fontWithSize:newSize];

i went the UI_APPEARANCE route

[[UILabel appearance] setFont:[UIFont fontWithName:fontName size:fontSize]];

if your labels require different font size, this might be problematic, fortunately, the labels i need to change were all the same fontsize