如何以编程方式和动态地为 UIButton 的背景图像着色?

How to tint the background images of an UIButton programmatically and dynamically?(如何以编程方式和动态地为 UIButton 的背景图像着色?)
本文介绍了如何以编程方式和动态地为 UIButton 的背景图像着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序 - 或者更确切地说是一些可重复使用的框架",一旦它工作,我很乐意分享.在这个应用程序中,用户应该能够从颜色主题列表中进行选择.因此,应用程序必须能够以某种相当动态的方式为其 UI 元素着色.

I am working on an app - or rather on some re-usable "framework" which I am happy to share once it works. Within this app the user should be able to choose from a list of color themes. Therefore the app must be able to tint its UI elements in some rather dynamic way.

对于按钮,所有着色都不起作用.必须在此处提供适当着色的背景图像.但是为每个人准备一组背景图像只是第二好的.它不够动态和灵活.

For Buttons all the tinting does not work. Properly tinted background images must be supplied here. But preparing one set of background images for each them is just second best. It is not dynamic and flexible enough.

最终,解决方案可能归结为为选定的正常状态提供一个单色(灰色)渐变图像,并使用 CoreGraphics 或 OpenGL 以编程方式为该图像着色.但坦率地说,我不知道从哪里开始.渐变应该是什么样子,然后我将如何以任何给定的颜色以编程方式对其进行着色?

In the end a solution may come down to providing one monochrome (grey) gradiented image for the selected and normal state and tint that image programmatically using CoreGraphics or OpenGL. But frankly, I do not know where to start there. How should the gradient look like and how would I then programmatically tint that in any given color?

几乎适用于 UISegmentedControls,只是稍微复杂一些.:) 任何涵盖 UISegementedControls 的通用解决方案都受到高度赞赏.

Pretty much applies to UISegmentedControls, just a bit more complicated. :) Any generic solution that covers UISegementedControls too is highliy appreciated.

推荐答案

我在另一个我找不到的帖子之后创建了一个 UIImage 类别,该类别采用图像并将其着色如下:

I made a UIImage category following another post that i cant find that takes the image and tints it as follows:

- (UIImage *)tintedImageWithColor:(UIColor *)tintColor {
    UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);
    CGContextRef context = UIGraphicsGetCurrentContext();


    CGContextTranslateCTM(context, 0, self.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);

    // draw alpha-mask
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGContextDrawImage(context, rect, self.CGImage);

    // draw tint color, preserving alpha values of original image
    CGContextSetBlendMode(context, kCGBlendModeSourceIn);
    [tintColor setFill];
    CGContextFillRect(context, rect);

    UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return coloredImage;
}

这将获取图像并使用给定颜色的 alpha 值填充所有区域.

This will take the image and fill in all of the areas with an alpha value with the given color.

这篇关于如何以编程方式和动态地为 UIButton 的背景图像着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)