iOS Different Font Sizes within Single Size Class for Different Devices

前端 未结 7 1562
死守一世寂寞
死守一世寂寞 2020-12-01 05:32

In iOS 8, we can design a different UI layout for each size class. The issue I\'m facing is, I\'ve designed a layout for Compact Width and Regular Height (size class for all

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 06:23

    Instead of writing code for each and every label, do just extend your label class with Your custom label class as below and it will automatically scale based on device resolution scaling factor:

    #define  SCALE_FACTOR_H ( [UIScreen mainScreen].bounds.size.height / 568 )
    
    
    CustomLabel.h
    
    #import 
    
    @interface CustomLabel : UILabel
    
    @end
    
    
    CustomLabel.m
    #import "CustomLabel.h"
    
    @implementation CustomLabel
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    - (id)initWithCoder:(NSCoder *)aDecoder {
        if( (self = [super initWithCoder:aDecoder]) ){
            [self layoutIfNeeded];
            [self configurefont];
        }
        return self;
    }
    
    - (void) configurefont {
        CGFloat newFontSize = (self.font.pointSize * SCALE_FACTOR_H);
        self.font = [UIFont fontWithName:self.font.fontName size:newFontSize];
    }
    @end
    

提交回复
热议问题