2 o0olike o0olike 于 2016.01.19 15:28 提问

ios 用Method Swizzling修改字体出现问题了,求大神帮忙看一下,谢谢了,在线等

buttonWithType:创建的按钮的字体怎么都改不了,myButtonWithType:这个方法就进不去。
我是新手,求大神帮帮忙看一下

``@implementation UIButton (myFontBtn)
+(void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
// When swizzling a class method, use the following:
// Class class = object_getClass((id)self);
SEL originalSelector = @selector(init);
SEL originalSelector2 = @selector(initWithFrame:);
SEL originalSelector3 = @selector(awakeFromNib);
SEL originalSelector4 = @selector(buttonWithType:);
SEL swizzledSelector = @selector(YHBaseInit);
SEL swizzledSelector2 = @selector(YHBaseInitWithFrame:);
SEL swizzledSelector3 = @selector(YHBaseAwakeFromNib);
SEL swizzledSelector4 = @selector(myButtonWithType:);

    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method originalMethod2 = class_getInstanceMethod(class, originalSelector2);
    Method originalMethod3 = class_getInstanceMethod(class, originalSelector3);
    Method originalMethod4 = class_getInstanceMethod(class, originalSelector4);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2);
    Method swizzledMethod3 = class_getInstanceMethod(class, swizzledSelector3);
    Method swizzledMethod4 = class_getInstanceMethod(class, swizzledSelector4);
    BOOL didAddMethod =
    class_addMethod(class,
                    originalSelector,
                    method_getImplementation(swizzledMethod),
                    method_getTypeEncoding(swizzledMethod));
    BOOL didAddMethod2 =
    class_addMethod(class,
                    originalSelector2,
                    method_getImplementation(swizzledMethod2),
                    method_getTypeEncoding(swizzledMethod2));
    BOOL didAddMethod3 =
    class_addMethod(class,
                    originalSelector3,
                    method_getImplementation(swizzledMethod3),
                    method_getTypeEncoding(swizzledMethod3));
    BOOL didAddMethod4 =
    class_addMethod(class,
                    originalSelector4,
                    method_getImplementation(swizzledMethod4),
                    method_getTypeEncoding(swizzledMethod4));

    if (didAddMethod) {
        class_replaceMethod(class,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));

    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
    if (didAddMethod2) {
        class_replaceMethod(class,
                            swizzledSelector2,
                            method_getImplementation(originalMethod2),
                            method_getTypeEncoding(originalMethod2));
    }else {
        method_exchangeImplementations(originalMethod2, swizzledMethod2);
    }
    if (didAddMethod3) {
        class_replaceMethod(class,
                            swizzledSelector3,
                            method_getImplementation(originalMethod3),
                            method_getTypeEncoding(originalMethod3));
    }else {
        method_exchangeImplementations(originalMethod3, swizzledMethod3);
    }
    if (didAddMethod4) {
        class_replaceMethod(class,
                            swizzledSelector4,
                            method_getImplementation(originalMethod4),
                            method_getTypeEncoding(originalMethod4));
    }else {
        method_exchangeImplementations(originalMethod4, swizzledMethod4);
    }
});

}

  • (instancetype)YHBaseInit { id __self = [self YHBaseInit]; UIFont * font = [UIFont fontWithName:@"SimHei" size:self.titleLabel.font.pointSize]; if (font) { self.titleLabel.font=font; } return __self; }

-(instancetype)YHBaseInitWithFrame:(CGRect)rect{
id __self = [self YHBaseInitWithFrame:rect];
UIFont * font = [UIFont fontWithName:@"SimHei" size:self.titleLabel.font.pointSize];
if (font) {
self.titleLabel.font=font;
}
return __self;
}

-(void)YHBaseAwakeFromNib{
[self YHBaseAwakeFromNib];
UIFont * font = [UIFont fontWithName:@"SimHei" size:self.titleLabel.font.pointSize];
if (font) {
self.titleLabel.font=font;
}

}

-(instancetype)myButtonWithType:(UIButtonType)buttonType{
id __self = [self myButtonWithType:buttonType];
UIFont * font = [UIFont fontWithName:@"SimHei" size:self.titleLabel.font.pointSize];
if (font) {
self.titleLabel.font=font;
}
return __self;
}


1个回答

devmiao
devmiao   Ds   Rxr 2016.01.19 22:17
o0olike
o0olike buttonWithType:这个方法初始化button的时候依然不起做用、用init、initwithframe、xib里面的button都生效。
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!