我创建了 UILabel
类别,它会在字体属性更改时做额外的工作。
我选择了类别而不是分类,所以我不必更改所有 XIB 文件中所有标签的类别。我只是将这个类别声明添加到前缀标题中,类别在整个项目范围内都是可见的。
执行文件:
//
// UILabel+XIBCustomFonts.m
#import "UILabel+XIBCustomFonts.h"
@implementation UILabel (XIBCustomFonts)
BOOL comes_from_nib = NO;
-(id)initWithCoder:(NSCoder *)aDecoder_{
self = [super initWithCoder:aDecoder_];
if (self) {
comes_from_nib = YES;
}
return self;
}
-(void)setFont:(UIFont *)font_{
[super setFont:font_];
NSLog(@"Setting from for label from XIB for name:%@ size: %.1f - do font theme replacement if needed", self.font.fontName, self.font.pointSize);
}
@end
令人惊讶的是以下日志的崩溃:
-[UIButtonLabel setFont:]: unrecognized selector sent to instance 0x9ad1b70
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButtonLabel setFont:]: unrecognized selector sent to instance 0x9ad1b70'
UIButtonLabel 是从哪里来的?
即使我在 setFont:
setter:
if ([self class] != [UILabel class] || !comes_from_nib) {
[super setFont:font_];
return;
}
有没有什么方法可以在不进行子类化的情况下覆盖 UILabel 中的 setFont: setter?
最佳答案
您不能在类别中调用 super 方法!绝不! 当您使用类别来扩充类的功能时,这是不可能的,您不是在扩展类,您实际上是在完全覆盖现有方法,您完全失去了原始方法。这就是出现错误的原因。
关于iphone - UILabel 类别和 setFont : crash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12474868/