iOS對(duì)不同的屏幕進(jìn)行字體適配

1.在不同的手機(jī)上對(duì)應(yīng)的字體大小可能不同,一般的來(lái)說(shuō)在 iphone 4 5 6的手機(jī)上的字體是一樣大小,在6P上的字體是4 5 6上的1.5倍,

下面進(jìn)行l(wèi)abel和button的字體適配

1.先創(chuàng)建label的延展

#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@interface UILabel (DXFont)

@end



#import "UILabel+DXFont.h"
@implementation UILabel (DXFont)
+ (void)load{
//利用running time運(yùn)行池的方法在程序啟動(dòng)的時(shí)候把兩個(gè)方法替換 適用Xib建立的label
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
method_exchangeImplementations(imp, myImp);  //交換方法
}

- (id)myInitWithCoder:(NSCoder*)aDecode{
[self myInitWithCoder:aDecode];
if (self) {
    //部分不像改變字體的 把tag值設(shè)置成LabelFontSize值的跳過(guò)
    if (IS_IPHONE_6P) {
        
        if(self.tag != LabelFontSize) {
            CGFloat fontSize = self.font.pointSize;
            self.font = [UIFont systemFontOfSize:fontSize*1.5];
        }   
    }
    }
return self;
}

2.button的實(shí)現(xiàn)

#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@interface UIButton (DXFont)

@end


#import "UIButton+DXFont.h"
@implementation UIButton (DXFont)
+ (void)load{

//利用running time運(yùn)行池的方法在程序啟動(dòng)的時(shí)候把兩個(gè)方法替換 適用Xib建立的label
Method imp = class_getInstanceMethod([self class], @selector(initWithCoder:));
Method myImp = class_getInstanceMethod([self class], @selector(myInitWithCoder:));
method_exchangeImplementations(imp, myImp);  //交換方法
}

- (id)myInitWithCoder:(NSCoder*)aDecode{
[self myInitWithCoder:aDecode];
if (self) {
    //部分不像改變字體的 把tag值設(shè)置成2016跳過(guò)
    if (IS_IPHONE_6P) {
        if(self.tag != 2016) {
            CGFloat fontSize = self.titleLabel.font.pointSize;
              self.titleLabel.font = [UIFont systemFontOfSize:fontSize*1.5];
        }
    }
}
return self;
}

如果要適配根據(jù)不同的屏幕使用不同的尺寸把后面的1.5 換成 下面定義的宏就可以啦

//不同設(shè)備的屏幕比例(當(dāng)然倍數(shù)可以自己控制)
#define SizeScale ((IPHONE_HEIGHT > 568) ? IPHONE_HEIGHT/568 : 1)

參考文檔:http://www.itdecent.cn/p/5815e81abb52

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容