ios给按钮图片根据不同的主题更改颜色
2016-12-21 11:02
274 查看
有时候在设计程序的时候,会涉及到主题模式的设计,根据不同的主体颜色,所对应的各个界面不同的按钮(导航栏、状态栏等),都需要对应的去改变颜色。
首先:用ps将按钮上图片的背景颜色改成白色的,如果图片本身有颜色,将无法通过代码的方式对其着色(很重要)。
核心代码:
只需要把对应的图片和所需要的颜色传进来,就可以得到想要颜色的图片了,但这里还有一个问题,你可能为按钮图片准备好了@3x的图片,但是还过于模糊,这时,需要修改一些这个方法:
在相同的地方进行替换,对图片的大小乘以baseImage.scale,就可以自动识别你传入图片是@2x还是@3x了,这样得到的图片就是清楚的图片了
首先:用ps将按钮上图片的背景颜色改成白色的,如果图片本身有颜色,将无法通过代码的方式对其着色(很重要)。
核心代码:
+(UIImage *)colorizeImage:(UIImage *)baseImage withColor:(UIColor *)theColor { UIGraphicsBeginImageContext(baseImage.size); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height); CGContextScaleCTM(ctx, 1, -1); CGContextTranslateCTM(ctx, 0, -area.size.height); CGContextSaveGState(ctx); CGContextClipToMask(ctx, area, baseImage.CGImage); [theColor set]; CGContextFillRect(ctx, area); CGContextRestoreGState(ctx); CGContextSetBlendMode(ctx, kCGBlendModeMultiply); CGContextDrawImage(ctx, area, baseImage.CGImage); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
只需要把对应的图片和所需要的颜色传进来,就可以得到想要颜色的图片了,但这里还有一个问题,你可能为按钮图片准备好了@3x的图片,但是还过于模糊,这时,需要修改一些这个方法:
UIGraphicsBeginImageContext(CGSizeMake(baseImage.size.width * baseImage.scale, baseImage.size.height * baseImage.scale)); CGContextRef ctx = UIGraphicsGetCurrentContext(); CGRect area = CGRectMake(0, 0, baseImage.size.width* baseImage.scale, baseImage.size.height* baseImage.scale);
在相同的地方进行替换,对图片的大小乘以baseImage.scale,就可以自动识别你传入图片是@2x还是@3x了,这样得到的图片就是清楚的图片了
相关文章推荐
- ios设置导航栏背景图片、返回按钮背景、标题颜色等等
- iOS 根据颜色创建个图片
- iOS 中根据线条的角度不同随机产生颜色
- iOS 根据颜色返回图片
- iOS 设置一行颜色不同的NSString 、剪切图片、设置TableView的自定义header
- ios应用在不更改版本号的情况下去改变不同的图片
- iOS中根据网络环境显示不同图片
- iOS 根据颜色获取图片
- IOS更改UITabBarItem的图片及字体颜色
- iOS根据不同需求截取图片
- 【iOS开发-39】为什么按钮点击时背景图片颜色灰蒙蒙的?与按钮的Type属性有关
- 根据主题颜色修改图片的颜色
- IOS设置导航栏全局的背景图片、导航栏title文字颜色大小、导航栏左右按钮的颜色。
- iOS开发小方法:根据UIColor一个颜色生成一张图片
- ios 更改全局UINavigationBar的背景图片以及通知栏颜色
- iOS 根据不同的系统版本加载不同图片方法
- 85.如何更改iOS上的UISearchBar组件的内部背景UITextField颜色 修改UISearchBar删除按钮ClearButton
- iOS 获取十六进制颜色显示RGB,根据颜色画图片
- iOS 在一个屏幕中根据不同的按钮创建不同的 view
- Android 更改纯色背景图片颜色,可实现一张背景圆形图片展示不同颜色