I want define a macro for unifying all fonts in my app:
#define EXO_REGULAR_FONT(size) [UIFont fontWithName:@"Exo-Regular" size:size]
and than using this macro like this:
myLabel.font = EXO_REGULAR_FONT(15);
But the compiler give me this error:
(there is an arrow under 15).
Any ideas to resolve ? Thanks
Change the name of your parameter:
#define EXO_REGULAR_FONT(theFontSize) [UIFont fontWithName:@"Exo-Regular" size:theFontSize]
From GCC manual:
When the macro is expanded, each use of a parameter in its body is replaced by the tokens of the corresponding argument
So when your macro is expanded, it inserts this in your code, hence the compilation error:
[UIFont fontWithName:@"Exo-Regular" 15:15]
#define EXO_REGULAR_FONT(size) [UIFont fontWithName:@"Exo-Regular" size:size]
gets expanded, when you call EXO_REGULAR_FONT(15)
, to
[UIFont fontWithName:@"Exo-Regular" 15:15]
So you need to define it like this for example:
#define EXO_REGULAR_FONT(_size_) [UIFont fontWithName:@"Exo-Regular" size:_size_]
Don't use a macro; they are evil (you did just experience one of the many reasons).
Use a function instead:
static inline UIFont *ExoRegularFont(CGFloat size) {
return [UIFont fontWithName:@"Exo-Regular" size:size];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With