iOS字體多屏幕適配

寫在前面

最近公司有一個需求,自己要根據(jù)屏幕放大,而由于時間趕,項目里是代碼+xib混合開發(fā)的。一個個改太麻煩了,任務(wù)量太大,xib構(gòu)建的頁面還不好修改。
找了一些資料,大功告成,然后根據(jù)自己的理解一個demo。

Runtime的運用

這里運用的就是替換系統(tǒng)方法,具體參考我另一篇文章:Runtime實戰(zhàn)使用篇

  • 基于NSObject寫個y一個類別,一個是實體方法,一個是類方法.
//類方法
+ (BOOL)swizzleClassMethod:(SEL)originalSel with:(SEL)newSel {
    Class class = object_getClass(self);
    Method originalMethod = class_getInstanceMethod(class, originalSel);
    Method newMethod = class_getInstanceMethod(class, newSel);
    if (!originalMethod || !newMethod) return NO;
    method_exchangeImplementations(originalMethod, newMethod);
    return YES;
}

//實體方法
+ (BOOL)swizzleInstanceMethod:(SEL)originalSel with:(SEL)newSel {
    Method originalMethod = class_getInstanceMethod(self, originalSel);
    Method newMethod = class_getInstanceMethod(self, newSel);
    if (!originalMethod || !newMethod) return NO;

    class_addMethod(self,
                    originalSel,
                    class_getMethodImplementation(self, originalSel),
                    method_getTypeEncoding(originalMethod));
    class_addMethod(self,
                    newSel,
                    class_getMethodImplementation(self, newSel),
                    method_getTypeEncoding(newMethod));

    method_exchangeImplementations(class_getInstanceMethod(self, originalSel),
                                   class_getInstanceMethod(self, newSel));
    return YES;
}

  • 針對純代碼的,存代碼設(shè)置字體大小會調(diào)用systemFontOfSize:方法,我們在+(void)load交換方法處理下。

TB_UIScreen:指當(dāng)前屏幕的寬度,可以根據(jù)自己UI設(shè)置的屏幕修改。demo默認(rèn)值:320

+(void)load
{
    [self swizzleClassMethod:@selector(systemFontOfSize:) with:@selector(TB_systemFontOfSize:)];

    [self swizzleClassMethod:@selector(boldSystemFontOfSize:) with:@selector(TB_boldSystemFontOfSize:)];

}

+ (UIFont *)TB_systemFontOfSize:(CGFloat)pxSize{

    UIFont *font = [UIFont TB_systemFontOfSize:pxSize*[UIScreen mainScreen].bounds.size.width/TB_UIScreen];

    return font;
}

//粗體
+(UIFont*)TB_boldSystemFontOfSize:(CGFloat)pxSize
{
    UIFont *font = [UIFont TB_boldSystemFontOfSize:pxSize*[UIScreen mainScreen].bounds.size.width/TB_UIScreen];

    return font;
}


  • 針對Xib上的,我們知道xib創(chuàng)建的會調(diào)用initWithCoder:方法,所以將此方法進行處理一次,調(diào)用UIFont方法。

為此對UILabel,UIButton,UITextFile,UItextView進行了處理,就拿label來說吧!

+(void)load
{
    [[self class] swizzleInstanceMethod:@selector(initWithCoder:) with:@selector(TB_InitWithCoder:)];

}

- (id)TB_InitWithCoder:(NSCoder*)aDecode{

    [self TB_InitWithCoder:aDecode];

     CGFloat pt = self.font.pointSize;

    self.font = [UIFont systemFontOfSize:pt];//這個方法會進行字體轉(zhuǎn)換

    return self;
}

效果圖

iPhone 5s


iphone5s

iPhone 6

iphone6

iPhone 6p


iphone6 plus

下載地址

github下載地址:demo

如果覺得不錯的話給個star吧!

更多在我的blog:博客

支持pod : pod 'TBFontAdjust', '~> 1.0.0'

使用方法:根據(jù)當(dāng)前的UI圖,設(shè)置正確的TB_UIScreen即可。

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

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

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