iOS字体适配
废话不多,端上代码:
首先先定义这三个字体,可以给该页面或者cell上分成至少三种的选择
{
CGFloat bigFont;
CGFloat normalFont;
CGFloat smallFont;
}
以tablecell来举栗子:
通过必走方法中将不同设备下的三种字体对着UI进行初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{
if(self= [superinitWithStyle:stylereuseIdentifier:reuseIdentifier]) {
NSString *_phoneType = [DMDeviceInfomation phoneType];
if(iPhone4s||iPhone4|| [_phoneTypeisEqualToString:@"iPhone 4s"] ||iPad3_4_mini2_air||iPad1_2_mini||iPadPro|| [_phoneTypehasPrefix:@"iPad"]){
bigFont=24;
normalFont=13;
smallFont=10;
}elseif(iPhone5||iPhone5s||[_phoneTypeisEqualToString:@"iPhone 5"] || [_phoneTypeisEqualToString:@"iPhone 5c"]|| [_phoneTypeisEqualToString:@"iPhone 5s"] || [_phoneTypeisEqualToString:@"iPhone SE"]){
bigFont=24;
normalFont=13;
smallFont=10;
}elseif(iPhone6|| [_phoneTypeisEqualToString:@"iPhone 6"] || [_phoneTypeisEqualToString:@"iPhone 6s"]){
bigFont=25;
normalFont=14;
smallFont=12;
}else{
bigFont=25;
normalFont=14;
smallFont=12;
}
}
return self;
}
然后在懒加载中将配置好的字体大小代进去
-(UILabel*)timeLabel{
if (!_timeLabel) {
_timeLabel= [[UILabelalloc]init];
_timeLabel.textColor=HEXCOLOR(0x9b9b9b);
_timeLabel.textAlignment = NSTextAlignmentLeft;
_timeLabel.font = [UIFont systemFontOfSize:normalFont];
_timeLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_timeLabel];
}
return _timeLabel;
}
这就完成了设备适配字体咯@
网友评论