iOS 纯代码控件布局

    我们项目开发中,很多都喜欢用XIB去拖控件,这会使得我们后期去修改,更新,或者换了个一人在去看你的东西,容易造成看不懂,又不敢乱动的场面。所有个人觉得大家为了后来人,都习惯用纯代码开发,不管自己以后什么时候看,或者换了别人都可以至少看的懂。

    今天我就来说说我的一些纯代码开发中的小技巧,减少代码量。也许会帮助到大家,我就以简单UILabel为例。我们项目中用到的UILabel 一般就字体  大小 颜色 位置 背景颜色 还有位子 几个属性值是常用的。

   就这我们可以定义个类方法Helpr里。定义几个方法。例如:

//按钮 Label字体的
+(UIFont *)justFontwithSize:(CGFloat)size;
#pragma mark------Label
/****
 输入Label的位子 文字
*/
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(nullable NSString *)text;
/*****
 输入Label的位子 文字 文字颜色
 */
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(nullable NSString *)text textColor:(nullable UIColor *)textColor;
/*****
 输入Label的位子 文字 文字位置
 */
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(nullable NSString *)text index:(int)index;
/*****
 输入Label的位子 文字 文字位子 文字大小
 */
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(nullable NSString *)text index:(int)index size:(float)size;
/*****
 输入Label的位子 文字 文字位子 文字颜色
 */
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(nullable NSString *)text textColor:(nullable UIColor *)textColor index:(int)index;
/*****
 输入Label的位子 文字  背景颜色 文字颜色 文字位子
 */
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(nullable NSString *)text bgColor:(nullable UIColor *)bgColor textColor:(nullable UIColor *)textColor index:(int)index;
/*****
 输入Label的位子 文字  背景颜色 文字颜色 文字位子 文字大小
 */
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(nullable NSString *)text bgColor:(nullable UIColor *)bgColor textColor:(nullable UIColor *)textColor index:(int)index size:(float)size;
这里是.M文件的内容

//按钮 Label字体的
+(UIFont *)justFontwithSize:(CGFloat)size{
    return [UIFont systemFontOfSize:size];
}

#pragma mark------Label

+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(NSString *)text{
//给Label一个默认的字体颜色 
    return [self justLabelWithFrame:Frame text:text textColor:LABELTEXTCOLOR];
}
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(NSString *)text textColor:(UIColor *)textColor{
//<span style="font-family: Arial, Helvetica, sans-serif;">给Label一个默认的背景颜色 </span>
    return [self justLabelWithFrame:Frame text:text bgColor:LABELTEXTBGCOLOR textColor:textColor index:1 size:LABELTEXTSIZE];
}
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(NSString *)text index:(int)index{
//<span style="font-family: Arial, Helvetica, sans-serif;">给Label一个默认的字体大小</span>
    return [self justLabelWithFrame:Frame text:text bgColor:LABELTEXTBGCOLOR textColor:LABELTEXTCOLOR index:index size:LABELTEXTSIZE];
}
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(NSString *)text index:(int)index size:(float)size{
    return [self justLabelWithFrame:Frame text:text bgColor:LABELTEXTBGCOLOR textColor:LABELTEXTCOLOR index:index size:size];
}

+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(NSString *)text textColor:(UIColor *)textColor index:(int)index{
    return [self justLabelWithFrame:Frame text:text bgColor:LABELTEXTBGCOLOR textColor:textColor index:index size:LABELTEXTSIZE];
}
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(NSString *)text bgColor:(UIColor *)bgColor textColor:(UIColor *)textColor index:(int)index{
    return [self justLabelWithFrame:Frame text:text bgColor:bgColor textColor:textColor index:index size:LABELTEXTSIZE];
}
+(UILabel *)justLabelWithFrame:(CGRect)Frame text:(NSString *)text bgColor:(UIColor *)bgColor textColor:(UIColor *)textColor index:(int)index size:(float)size{
    UILabel *label=[[UILabel alloc]initWithFrame:Frame];
    if (!textColor) {
        textColor=LABELTEXTBGCOLOR;
    }
    if(!bgColor){
        bgColor=LABELTEXTBGCOLOR;
    }
    if (!size) {
        size=LABELTEXTSIZE;
    }
    label.textColor=textColor;
    label.backgroundColor=bgColor;
    if (index==0) {
        label.textAlignment=NSTextAlignmentLeft;
    }else if(index==1)
    {
        label.textAlignment=NSTextAlignmentCenter;
    }else if(index==2)
    {
        label.textAlignment=NSTextAlignmentRight;
    }
    label.text=text;
    label.font=[self justFontwithSize:size];
    label.tag=1000;
    return label;
}
这样的话我们去创建一个Label 基本上就是一行代码。 

  当然UIButton 已经常用的控件都可以这样小小的封装下,减少我们开发中的代码量。

当然这个也不能全部搞定适配的问题,我们在创建大小的时候1是可以根据屏幕的比例去创建位子大小,遇到位子大小不协调的,我们在根据机型去改变控件的大小位子。

//判断是否是iPhone4
#define isIPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
//判断是否是iPhone5
#define isIPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
//是否iPhone6
#define isIPhone6  ([UIScreen instancesRespondToSelector:@selector(nativeBounds)] ? CGSizeEqualToSize(CGSizeMake(375.00*2, 667.00*2),[[UIScreen mainScreen] nativeBounds].size) : NO)
//是否iPhone6plus
#define isIPhone6plus ([UIScreen instancesRespondToSelector:@selector(nativeBounds)] ? CGSizeEqualToSize(CGSizeMake(414.000000*3, 736.000000*3),[[UIScreen mainScreen] currentMode].size) : NO)
这是几个常用的机型判断。


版权声明:本文为sfgyygy原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。