尽量告别背景图吧~

在开发中,我们会经常给一些UIkit添加背景图的需求,此时一般是UI给图来用,随着背景图的越来越多,导致app越来越大,而且有些相同的图可能出现冗余,还有可能代码删了,遗留的垃圾图。其实很多作为背景图都很简单,完全可以直接代码绘制,如果UI能给一些通用的规范,可以封装出来给其他人用,ok,进入正题。

1、创建一个UIiamge的category,然后添加方法:

/*

获得指定颜色 大小 边角度 边框颜色 边框宽度 的图片

@param color 图片颜色

@param targetSize 图片大小

@param cornerRadius 图片边框的角度

@param borderColor 图片边框的颜色

@param ldCustomButtonStyle borderWidth 边框宽度

@return 指定图片

*/

+ (UIImage *)ld_imageWithBackgroundColor:(UIColor *)color toSize:(CGSize)targetSize cornerRadius:(CGFloat)cornerRadius  borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth {

UIGraphicsBeginImageContextWithOptions(targetSize, cornerRadius == 0, [UIScreen mainScreen].scale);

CGRect targetRect = (CGRect){0, 0, targetSize.width, targetSize.height};

UIImage *finalImage = nil;

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);

if (cornerRadius == 0) {

if (borderWidth > 0) {

CGContextSetStrokeColorWithColor(context, borderColor.CGColor);

CGContextSetLineWidth(context, borderWidth);

CGContextFillRect(context, targetRect);

targetRect = CGRectMake(borderWidth / 2, borderWidth / 2, targetSize.width - borderWidth, targetSize.height - borderWidth);

CGContextStrokeRect(context, targetRect);

} else {

CGContextFillRect(context, targetRect);

}

} else {

targetRect = CGRectMake(borderWidth / 2, borderWidth / 2, targetSize.width - borderWidth, targetSize.height - borderWidth);

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:targetRect

byRoundingCorners:UIRectCornerAllCorners

cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

CGContextAddPath(UIGraphicsGetCurrentContext(), path.CGPath);

if (borderWidth > 0) {

CGContextSetStrokeColorWithColor(context, borderColor.CGColor);

CGContextSetLineWidth(context, borderWidth);

CGContextDrawPath(context, kCGPathFillStroke);

} else {

CGContextDrawPath(context, kCGPathFill);

}

}

finalImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return finalImage;

}


大家可以去学习一下core graphics ,上边的代码注释写的很清楚了,就是产生一个背景图,基本满足平时的背景图需求。

2、为UIkit控件设置背景图,这里以UIbutton为例:

假如UI给出了一个规范,有两种样式的button:



我们可以封装一个公共方法,给其他人用,创建一个UIbutton的Category:

typedef NS_ENUM(NSUInteger, LDCustomButtonStyle) {

LDCustomButtonDefaultStyle = 0 ,//默认样式 什么都没设置

LDCustomButtonRBgWTxtStyle = 1, //红底白字

LDCustomButtonWBgRTxtStyle = 2 ,//白底红字红边框

};

@interface UIButton (LDCustom)

/**

根据样式获得不同样式的按钮

@param ldCustomButtonStyle 按钮样式

@return 指定样式的按钮

*/

+ (instancetype )ld_createButtonWithStyle:(LDCustomButtonStyle )ldCustomButtonStyle;


可以看到我们的接口没有指定返回的按钮大小,怎么做到呢?

这里要用到,图片拉伸的方法(自己谷歌吧):

- (void )setBackgroundColor:(UIColor *)backgroundColor  cornerRadius:(CGFloat)cornerRadius borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth forState:(UIControlState)state {

UIImage *backgroundImg = [UIImage ld_imageWithBackgroundColor:backgroundColor  toSize:CGSizeMake(2*cornerRadius+1, 2*cornerRadius+1) cornerRadius:cornerRadius borderColor:borderColor borderWidth:borderWidth];

UIImage * stretchedImage = [backgroundImg resizableImageWithCapInsets:UIEdgeInsetsMake(cornerRadius, cornerRadius, cornerRadius ,cornerRadius) resizingMode:UIImageResizingModeStretch];

[self setBackgroundImage:stretchedImage forState:state];

}

首先创建一个size为(2*cornerRadius+1, 2*cornerRadius+1)大小的图片,然后指定拉伸区域这样就OK了,当button大于图片大小时,会被拉伸中间区域,不影响边框的展示。

最后我们再提供一个针对规范的接口给其他人用就好了:

+ (instancetype )ld_createButtonWithStyle:(LDCustomButtonStyle )ldCustomButtonStyle {

UIButton *customBtn = [UIButton buttonWithType:UIButtonTypeCustom];

switch (ldCustomButtonStyle) {

case LDCustomButtonDefaultStyle:

break;

case LDCustomButtonRBgWTxtStyle:

[customBtn setBackgroundColor:[UIColor ld_colorWithHex:0xfa4545]  cornerRadius:4.0 borderColor:[UIColor ld_colorWithHex:0xfa4545] borderWidth:0.5 forState:UIControlStateNormal];

[customBtn setTitleColor:[UIColor ld_colorWithHex:0xffffff] forState:UIControlStateNormal];

[customBtn setBackgroundColor:[UIColor ld_colorWithHex:0xc63737]  cornerRadius:4.0 borderColor:[UIColor ld_colorWithHex:0xc63737] borderWidth:0.5 forState:UIControlStateSelected];

[customBtn setTitleColor:[UIColor ld_colorWithHex:0xd55d5d] forState:UIControlStateSelected];

[customBtn setBackgroundColor:[UIColor ld_colorWithHex:0xc63737]  cornerRadius:4.0 borderColor:[UIColor ld_colorWithHex:0xc63737] borderWidth:0.5 forState:UIControlStateHighlighted];

[customBtn setTitleColor:[UIColor ld_colorWithHex:0xd55d5d] forState:UIControlStateHighlighted];

[customBtn setBackgroundColor:[UIColor ld_colorWithHex:0xfa4545] cornerRadius:4.0 borderColor:[UIColor ld_colorWithHex:0xfa4545] borderWidth:0.5 forState:UIControlStateDisabled];

[customBtn setTitleColor:[UIColor ld_colorWithHex:0xff9797] forState:UIControlStateDisabled];

break;

case LDCustomButtonWBgRTxtStyle:

[customBtn setBackgroundColor:[UIColor ld_colorWithHex:0xffffff] cornerRadius:4.0 borderColor:[UIColor ld_colorWithHex:0xfa4545] borderWidth:0.5 forState:UIControlStateNormal];

[customBtn setTitleColor:[UIColor ld_colorWithHex:0xfa4545] forState:UIControlStateNormal];

[customBtn setBackgroundColor:[UIColor ld_colorWithHex:0xfa4545]  cornerRadius:4.0 borderColor:[UIColor ld_colorWithHex:0xfa4545] borderWidth:0.5 forState:UIControlStateSelected];

[customBtn setTitleColor:[UIColor ld_colorWithHex:0xffffff] forState:UIControlStateSelected];

[customBtn setBackgroundColor:[UIColor ld_colorWithHex:0xfa4545]  cornerRadius:4.0 borderColor:[UIColor ld_colorWithHex:0xfa4545] borderWidth:0.5 forState:UIControlStateHighlighted];

[customBtn setTitleColor:[UIColor ld_colorWithHex:0xffffff] forState:UIControlStateHighlighted];

[customBtn setBackgroundColor:[UIColor ld_colorWithHex:0xfa4545]  cornerRadius:4.0 borderColor:[UIColor ld_colorWithHex:0xfa4545] borderWidth:0.5 forState:UIControlStateDisabled];

[customBtn setTitleColor:[UIColor ld_colorWithHex:0xff9797] forState:UIControlStateDisabled];

break;

default:

break;

}

return customBtn;

}

到此就可以了,以后用到规范的按钮,直接调用这个方法就好了~ 代码量也会明显简洁~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • // // JackDateAndDateView.m // ZHB // // Created by JackR...
    JackRen阅读 421评论 0 1
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 1,694评论 0 3
  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 833评论 0 0
  • 0210【动待花开】20171021 D11 拿出之前制作的字卡,今天和小助手一起认字,今天我们要学"妈妈"。我们...
    芝麻_mom阅读 102评论 0 0
  • 谁裂天河,坠星如雨。莲台倒、万点鲛珠,霓裳舞、千枝红羽。更哪堪,脂染花黄,粉褪雪缕。 雨过暗香织雾,菡萏滴露。莲蓬...
    江南烟雨阅读 231评论 0 1