UIFont - iOS 글꼴 맞춤

3617 단어
우선 런타임이 순수한 코드를 제어하는 글꼴을 써야 한다
+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method newMethod = class_getClassMethod([self class], @selector(adjustFont:));
        Method method = class_getClassMethod([self class], @selector(systemFontOfSize:));
        method_exchangeImplementations(newMethod, method);
        
        Method newBoldMethod = class_getClassMethod([self class], @selector(adjustBoldFont:));
        Method boldMethod = class_getClassMethod([self class], @selector(boldSystemFontOfSize:));
        method_exchangeImplementations(newBoldMethod, boldMethod);
        
    });
}

+ (UIFont *)adjustFont:(CGFloat)fontSize
{
    [self adjustFont:fontSize];
    
    UIFont *newFont = nil;
    
    if (iPhone6 || iPhoneX)
    {
        //iPhone 6|6S|7|7S|8
        newFont = [UIFont adjustFont:fontSize + 2];
    }
    else if (iPhone6Plus)
    {
        //iPhone 6P|7P|8P
        newFont = [UIFont adjustFont:fontSize + 4];
    }
    else
    {
        //iPhone 5|5S|5C|4|4S
        newFont = [UIFont adjustFont:fontSize];
    }
    
//    newFont = [UIFont adjustFont:fontSize * SizeScale];
    return newFont;
    
}

+ (UIFont *)adjustBoldFont:(CGFloat)fontSize {
    
    [self adjustBoldFont:fontSize];
    
    UIFont *newFont = nil;
    if (iPhone6 || iPhoneX)
    {
        //iPhone 6|6S|7|7S|8
        newFont = [UIFont adjustBoldFont:fontSize + 2];
    }
    else if (iPhone6Plus)
    {
        //iPhone 6P|7P|8P
        newFont = [UIFont adjustBoldFont:fontSize + 4];
    }
    else
    {
        //iPhone 5|5S|5C|4|4S
        newFont = [UIFont adjustBoldFont:fontSize];
    }
    return newFont;
    
}

그리고 nib 글꼴 컨트롤, 컨트롤 다 써주세요.
@implementation UILabel (FontSize)
+ (void)load {
    
    Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
    Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
    method_exchangeImplementations(imp, myImp);
    
    Method cmp = class_getInstanceMethod([self class], @selector(initWithFrame:));
    Method myCmp = class_getInstanceMethod([self class], @selector(myInitWithFrame:));
    method_exchangeImplementations(cmp, myCmp);
}

- (id)myInitWithCoder:(NSCoder*)aDecode {
    [self myInitWithCoder:aDecode];
    if (self.tag < TagNoScale) {
        CGFloat fontSize = self.font.pointSize;

        if (iPhone6 || iPhoneX)
        {
            //iPhone 6|6S|7|7S|8
            self.font = [self.font fontWithSize:fontSize + 2];
        }
        else if (iPhone6Plus)
        {
            //iPhone 6P|7P|8P
            self.font = [self.font fontWithSize:fontSize + 4];
        }
        else
        {
            //iPhone 5|5S|5C|4|4S
            self.font = [self.font fontWithSize:fontSize];
        }
    }
    return self;
}

- (id)myInitWithFrame:(CGRect)frame{
    [self myInitWithFrame:frame];
    if(self){
        if (self.tag < TagNoScale) {
            CGFloat fontSize = self.font.pointSize;
            if (iPhone6 || iPhoneX)
            {
                //iPhone 6|6S|7|7S|8
                self.font = [self.font fontWithSize:fontSize + 2];
            }
            else if (iPhone6Plus)
            {
                //iPhone 6P|7P|8P
                self.font = [self.font fontWithSize:fontSize + 4];
            }
            else
            {
                //iPhone 5|5S|5C|4|4S
                self.font = [self.font fontWithSize:fontSize];
            }
            
        }
    }
    return self;
}

@end

일을 끝내다
가끔 myInitWithFrame 붕괴...참고만

좋은 웹페이지 즐겨찾기