- 本文章适用于使用xib或者storyboard开发过程中,控件根据自己的宏定义的比例处理文字(font)等比,话不多说,先看效果,再上代码。
使用方式
创建分类UIButton+XibScaleAdapter分类,仅UIButton的font等比适配的分类
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIButton (XibScaleAdapter)
@property(nonatomic, assign) IBInspectable BOOL adapterFont;
@end
NS_ASSUME_NONNULL_END
#import "UIButton+XibScaleAdapter.h"
#define C_WIDTH(WIDTH) WIDTH * [UIScreen mainScreen].bounds.size.width/375.0
@implementation UIButton (XibScaleAdapter)
- (void)setAdapterFont:(BOOL)adapterFont{
if (adapterFont) {
UIFontDescriptor *ctFont = self.titleLabel.font.fontDescriptor;
NSNumber *fontString = [ctFont objectForKey:@"NSFontSizeAttribute"];
self.titleLabel.font = [UIFont fontWithName:self.titleLabel.font.familyName size:C_WIDTH([fontString floatValue])];
}else{
self.titleLabel.font = self.titleLabel.font;
}
}
- (BOOL)adapterFont{
return self.adapterFont;
}
@end
创建分类UILabel+XibScaleAdapter分类,仅UILabel的font等比适配的分类
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UILabel (XibScaleAdapter)
@property(nonatomic, assign) IBInspectable BOOL adapterFont;
@end
NS_ASSUME_NONNULL_END
#import "UILabel+XibScaleAdapter.h"
#define C_WIDTH(WIDTH) WIDTH * [UIScreen mainScreen].bounds.size.width/375.0
@implementation UILabel (XibScaleAdapter)
- (void)setAdapterFont:(BOOL)adapterFont{
if (adapterFont) {
UIFontDescriptor *ctFont = self.font.fontDescriptor;
NSNumber *fontString = [ctFont objectForKey:@"NSFontSizeAttribute"];
self.font = [UIFont fontWithName:self.font.familyName size:C_WIDTH([fontString floatValue])];
NSLog(@"%@",self.font);
}else{
self.font = self.font;
}
}
- (BOOL)adapterFont{
return self.adapterFont;
}
@end
创建分类UITextField+XibScaleAdapter分类,仅UITextField的font等比适配的分类
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UITextField (XibScaleAdapter)
@property(nonatomic, assign) IBInspectable BOOL adapterFont;
@end
NS_ASSUME_NONNULL_END
#import "UITextField+XibScaleAdapter.h"
#define C_WIDTH(WIDTH) WIDTH * [UIScreen mainScreen].bounds.size.width/375.0
@implementation UITextField (XibScaleAdapter)
- (void)setAdapterFont:(BOOL)adapterFont{
if (adapterFont) {
UIFontDescriptor *ctFont = self.font.fontDescriptor;
NSNumber *fontString = [ctFont objectForKey:@"NSFontSizeAttribute"];
self.font = [UIFont fontWithName:self.font.familyName size:C_WIDTH([fontString floatValue])];
NSLog(@"%@",self.font);
}else{
self.font = self.font;
}
}
- (BOOL)adapterFont{
return self.adapterFont;
}
@end
创建分类UITextView+XibScaleAdapterr分类,仅UITextView的font等比适配的分类
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UITextView (XibScaleAdapter)
@property(nonatomic, assign) IBInspectable BOOL adapterFont;
@end
NS_ASSUME_NONNULL_END
#import "UITextView+XibScaleAdapter.h"
#define C_WIDTH(WIDTH) WIDTH * [UIScreen mainScreen].bounds.size.width/375.0
@implementation UITextView (XibScaleAdapter)
- (void)setAdapterFont:(BOOL)adapterFont{
if (adapterFont) {
UIFontDescriptor *ctFont = self.font.fontDescriptor;
NSNumber *fontString = [ctFont objectForKey:@"NSFontSizeAttribute"];
self.font = [UIFont fontWithName:self.font.familyName size:C_WIDTH([fontString floatValue])];
NSLog(@"%@",self.font);
}else{
self.font = self.font;
}
}
- (BOOL)adapterFont{
return self.adapterFont;
}
@end
以上是整体使用、实现、效果,由于在代码里面定义的等比适配基准机型是8(宽度375),基准屏宽度的定义取决于UI出的尺寸图是按什么机型出的,UI按什么机型出,基准屏幕的尺寸定义就对应什么机型的宽度就好。
由于在实际开发过程中,有的页面需要等比适配,有的则不需要,所以在xib中做成了开关的属性,默认情况下是关闭的(不做等比适配)
xib的控件适配在这里
© 版权声明
文章版权归作者所有,未经允许请勿转载。如内容涉嫌侵权,请在本页底部进入<联系我们>进行举报投诉!
THE END


![[西门子PLC]S7-1500读写汇川伺服IS620F/810F驱动参数 - 宋马](https://pic.songma.com/blogimg/20250425/609c2b24b84c4ba68f403806ccc07a39.jpg)


















暂无评论内容