尽量告别背景图吧~

作者: ITxiansheng | 来源:发表于2016-11-15 22:38 被阅读151次

在开发中,我们会经常给一些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;

}

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

相关文章

  • 尽量告别背景图吧~

    在开发中,我们会经常给一些UIkit添加背景图的需求,此时一般是UI给图来用,随着背景图的越来越多,导致app越来...

  • 尽量吧

    最近感觉自己的身体越来越差。一方面是睡得太晚。一方面是太操心,太劳神。我也终于有一些理解父亲母亲那些年的不容易。她...

  • 虽然难,尽量吧

    周六的早晨,克服了不想上班的强烈欲望,与床做了一番激烈的拉锯战后,还是起来去医院接班。没错,周六,我值班,医...

  • 尽量做个好人吧

    如果只有一世,尽量做个好人吧。 没有什么值得你贬低自己。一个先后?一个争执?一点金币?一点地位?还有更多,更多的你...

  • 告别吧

    可能刚开始我们都是真的,只是结局会让人遗憾。我不知道为什么会走成这样,一次次伤害自己,伤害别人,可我是真的没...

  • 告别吧

    告别吧,请优雅地挥起你的手臂 送我远行 我木讷、乏味、还有贫穷 我决绝的告别,你也告别吧 我会把我所有的自卑丢弃在...

  • PPT教程:PPT怎么设置背景图片

    PPT怎么设置背景图片?很多朋友不知道,那么我就从几个方面来回答你吧!假设我们需要添加这张图片为PPT背景图: 一...

  • 尽量吧!毕竟生活不易

    闭上眼睛,回味今天晚上的环湖骑行,吹着风,聊着天真的很舒服,很惬意,好想一辈子就这样简单的过着,可我知道这是不可能...

  • 我尽量简简吧

    听完Bobo的晨读我打开了衣帽间的门,看看三面的衣橱和窗台下面摞着的超大整理箱,我想“极简”这个词对我来说比割肉还...

  • 算告别吧

    来到简书也一年多快两年了吧,这期间我看文章不多,写文章更是少之又少,不过也是学到了很多,在这期间面对改变也坦然了许...

网友评论

    本文标题:尽量告别背景图吧~

    本文链接:https://www.haomeiwen.com/subject/tiulpttx.html