ios开发布局

使用xib或storyboard等比适配文字大小(按设置的宏定义

2022-03-29  本文已影响0人  心猿意码_

使用方式


Mar-28-2022 11-22-53.gif
创建分类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的控件适配在这里

上一篇下一篇

猜你喜欢

热点阅读