首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >uilabel 改变字体全app改的(加属性要import 修改系统方法不需要import)

uilabel 改变字体全app改的(加属性要import 修改系统方法不需要import)

原创
作者头像
conanma
修改2021-05-11 14:38:30
4440
修改2021-05-11 14:38:30
举报
文章被收录于专栏:正则正则

#import <UIKit/UIKit.h>

@interface UILabel (ChangeFont)

@end

#import "UILabel+ChangeFont.h"

#import <objc/runtime.h>

@implementation UILabel (ChangeFont)

+ (void)load

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

        Class class = [self class];

SEL originalSelector = @selector(init);

SEL originalSelector2 = @selector(initWithFrame:);

SEL originalSelector3 = @selector(awakeFromNib);

SEL swizzledSelector = @selector(MCInit);

SEL swizzledSelector2 = @selector(MCInitWithFrame:);

SEL swizzledSelector3 = @selector(MCAwakeFromNib);

Method originalMethod = class_getInstanceMethod(class, originalSelector);

Method originalMethod2 = class_getInstanceMethod(class, originalSelector2);

Method originalMethod3 = class_getInstanceMethod(class, originalSelector3);

Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2);

Method swizzledMethod3 = class_getInstanceMethod(class, swizzledSelector3);

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));

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);

    }

    });

}

- (instancetype)MCInit

{

id __self = [self MCInit];

if (self) {

// 需要改变字体的大小,和一些其他的设置

self.font= [UIFont systemFontOfSize:10];

self.backgroundColor = [UIColor orangeColor];

    }

return __self;

}

-(instancetype)MCInitWithFrame:(CGRect)rect{

id __self = [self MCInitWithFrame:rect];

if (self) {

// 需要改变字体的大小,和一些其他的设置

self.font= [UIFont systemFontOfSize:10];

self.backgroundColor = [UIColor redColor];

    }

return __self;

}

-(void)MCAwakeFromNib{

    [self MCAwakeFromNib];

if (self) {

// 需要改变字体的大小,和一些其他的设置

self.font= [UIFont systemFontOfSize:10];

self.backgroundColor = [UIColor blueColor];

    }

}

修改字体(在设置的字体上+n号)

- (void)setconanFont:(UIFont *)font

{

NSLog(@"调用了");

    [self setconanFont:[UIFont systemFontOfSize:font.pointSize + 10]];

}

@end

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档