美文网首页
iOS开发各种设备字体适配

iOS开发各种设备字体适配

作者: lczalh | 来源:发表于2017-12-30 16:28 被阅读20次

    调用

    [self.title setFont:kFont(15)];
    
    //粗体
    [self.title setFont: kboldFont(15)];
    

    定义宏

    #define kWidth [UIScreen mainScreen].bounds.size.width
    #define SizeScale (kWidth != 414 ? 1 : 1.2)
    #define kFont(value) [UIFont systemFontOfSize:value * SizeScale]
    #define kboldFont(value)  [UIFont boldSystemFontOfSize:value * SizeScale]
    

    UIButton+font.h

    #import <UIKit/UIKit.h>
    #import <objc/runtime.h>
    
    /**
     *  button
     */
    @interface UIButton (MyFont)
    
    @end
    
    
    /**
     *  Label
     */
    @interface UILabel (myFont)
    
    @end
    
    /**
     *  TextField
     */
    
    @interface UITextField (myFont)
    
    @end
    
    /**
     *  TextView
     */
    @interface UITextView (myFont)
    
    @end
    
    

    UIButton+font.m

    #import "UIButton+MyFont.h"
    
    //不同设备的屏幕比例(当然倍数可以自己控制)
    
    @implementation UIButton (MyFont)
    
    + (void)load{
        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值设置成333跳过
            if(self.titleLabel.tag != 333){
                CGFloat fontSize = self.titleLabel.font.pointSize;
                self.titleLabel.font = [UIFont systemFontOfSize:fontSize * SizeScale];
            }
        }
        return self;
    }
    
    @end
    
    
    @implementation UILabel (myFont)
    
    + (void)load{
        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值设置成333跳过
            if(self.tag != 333){
                CGFloat fontSize = self.font.pointSize;
                self.font = [UIFont systemFontOfSize:fontSize * SizeScale];
            }
        }
        return self;
    }
    
    @end
    
    @implementation UITextField (myFont)
    
    + (void)load{
        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值设置成333跳过
            if(self.tag != 333){
                CGFloat fontSize = self.font.pointSize;
                self.font = [UIFont systemFontOfSize:fontSize * SizeScale];
            }
        }
        return self;
    }
    
    @end
    
    @implementation UITextView (myFont)
    
    + (void)load{
        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值设置成333跳过
            if(self.tag != 333){
                CGFloat fontSize = self.font.pointSize;
                self.font = [UIFont systemFontOfSize:fontSize * SizeScale];
            }
        }
        return self;
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS开发各种设备字体适配

          本文链接:https://www.haomeiwen.com/subject/wwaugxtx.html