iOS开发之字号适配实现

一个iOS开发项目无外乎就是纯代码布局、xib或SB布局。那么如何实现两个方式的字号适配呢?

字号适配------纯代码

定义一个宏定义如下:

#define SizeScale (SCREEN_WIDTH != 414 ?1 :1.2)

#define kFont(value) [UIFont systemFontOfSize:value * SizeScale]

宏中的1.2是在plus下的大小放大比例。

纯代码中设置字号通过使用这个宏来实现整体适配

字号适配------xib或SB

字号适配无外乎就是设置UIButton、UILabel、UITextView、UITextField的字号
通过创建这几个的类目来实现(Runtime方式的黑魔法method swizzling)
废话不多说,直接上代码:
.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


.m


#import "UIButton+MyFont.h"


//不同设备的屏幕比例(当然倍数可以自己控制)


@implementation UIButton (MyFont)


+ (void)load{

    Method imp = class_getInstanceMethod([selfclass], @selector(initWithCoder:));

    Method myImp = class_getInstanceMethod([selfclass], @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 = [UIFontsystemFontOfSize:fontSize * SizeScale];

        }

    }

    return self;

}


@end



@implementation UILabel (myFont)


+ (void)load{

    Method imp = class_getInstanceMethod([selfclass], @selector(initWithCoder:));

    Method myImp = class_getInstanceMethod([selfclass], @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 = [UIFontsystemFontOfSize:fontSize * SizeScale];

        }

    }

    return self;

}


@end


@implementation UITextField (myFont)


+ (void)load{

    Method imp = class_getInstanceMethod([selfclass], @selector(initWithCoder:));

    Method myImp = class_getInstanceMethod([selfclass], @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 = [UIFontsystemFontOfSize:fontSize * SizeScale];

        }

    }

    return self;

}


@end


@implementation UITextView (myFont)


+ (void)load{

    Method imp = class_getInstanceMethod([selfclass], @selector(initWithCoder:));

    Method myImp = class_getInstanceMethod([selfclass], @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 = [UIFontsystemFontOfSize:fontSize * SizeScale];

        }

    }

    return self;

}


@end


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
小程序可以通过以下方式实现iOS的输入法自适配: 1. 输入框的布局和样式适配:确保输入框在iOS系统中正常显示,并且在弹出输入法时不会被覆盖。可以通过设置输入框的位置、大小、边距等样式属性来达到适配的效果。 2. 输入法事件的适配:针对iOS系统的输入法事件特性进行适配,确保在iOS系统中触发正常。比如,在iOS系统中,输入法的“完成”按钮可能是通过触摸屏幕上的“完成”按钮或者按下键盘上的“Return”键来触发,需要针对这些不同的触发方式进行处理。 3. 自定义输入组件的适配:如果小程序中使用了自定义的输入组件,需要对iOS系统的输入法进行适配,确保输入法能够正常弹出并进行编辑。可以通过监听输入框的focus、blur等事件,并且针对不同的事件进行处理来实现适配。 4. 特殊输入方式的适配:如果小程序中使用了一些特殊的输入方式,如手写输入、语音输入等,需要对iOS系统的输入法进行适配,确保这些输入方式在iOS系统中能够正常使用。可以通过对这些输入方式的特性进行了解,并在小程序中进行相应的处理来实现适配。 总之,要实现iOS的输入法自适配,需要在开发小程序时充分考虑iOS系统的输入法特性,并进行相应的适配处理。同时,需要进行充分的测试,确保小程序在iOS系统中的输入体验良好。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sailip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值