如何创建一个宏[UIFont fontWithName:size:]?

时间:2013-07-29 06:39:20

标签: objective-c macros uifont

我使用这个宏:

#define fontMacro(name, size) ((UIFont *)[UIFont fontWithName:(NSString *)(name) size:(CGFloat)(size)])

UIFont *font = fontMacro(@"Akrol", 15); // Error: Expected ']'
UIFont *font2 = ((UIFont *)[UIFont fontWithName:(NSString *)(@"Akrol") size:(CGFloat)(15)]); // work fine

那么,我该如何使用fontMacro

1 个答案:

答案 0 :(得分:2)

问题是,宏的size参数在右侧的所有出现的“大小”处展开,以便fontMacro(@"Akrol", 15)扩展为

((UIFont *)[UIFont fontWithName:(NSString *)(@"Akrol") 15:(CGFloat)(15)])
                            unwanted expansion here ---^

将宏参数重命名为_size_或类似名称 应该解决问题:

#define fontMacro(_name_, _size_) ((UIFont *)[UIFont fontWithName:(NSString *)(_name_) size:(CGFloat)(_size_)])