How to focus UIButton background images programmatically and dynamically?

I am working on an application, or rather, with some reusable "frames", which I will gladly tell you when it works. In this application, the user should be able to choose from a list of color themes. Therefore, the application should be able to slightly tint the elements of its interface.

For buttons, all shades do not work. Correct tinted background images should be provided. But preparing one set of background images for each of them is just the second. It is not dynamic and flexible enough.

Ultimately, the solution can be reduced to a single monochrome (gray) gradient image for the selected and normal state and programmatically use this image using CoreGraphics or OpenGL. But honestly, I don’t know where to start. What should the gradient look like and how will I programmatically tint it in any given color?

Quite a lot relates to UISegmentedControls, a bit more complicated. :) Any general solution that covers UISegementedControls is also much appreciated.

+6
source share
6 answers

I made the UIImage category after the following entry, which I cannot find, which accepts the image and its hue looks like this:

- (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; } 

This will take an image and fill all areas with an alpha value with a given color.

+12
source

In iOS7, you can use the imagedWithRenderingMode: method in combination with the setTintColor: method setTintColor:

But be sure to use UIImageRenderingModeAlwaysTemplate , as it replaces all opaque colors with UIImage in UIImage color. The default is UIImageRenderingModeAutomatic .

UIImageRenderingModeAlwaysTemplate

 Always draw the image as a template image, ignoring its color information. 

Example:

  [myButton setTintColor:[UIColor colorWithRed:0 green:0.8196f blue:0.0039f alpha:1]]; UIImage * image = [myButton.currentBackgroundImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [myButton setBackgroundImage:image forState:UIControlStateNormal]; 
+41
source

I created the UIButton category using the code for hockey: https://github.com/filipstefansson/UITintedButton

Here are the new methods:

 -(void)setImageTintColor:(UIColor *)color; -(void)setBackgroundTintColor:(UIColor *)color forState:(UIControlState)state; +(void)tintButtonImages:(NSArray *)buttons withColor:(UIColor *)color; +(void)tintButtonBackgrounds:(NSArray *)buttons withColor:(UIColor *)color forState:(UIControlState)state; 
+4
source

Here is a partial answer to your question. This is a helper method that I wrote that grayscale image:

 // baseImage is the grey scale image. color is the desired tint color + (UIImage *)tintImage:(UIImage *)baseImage withColor:(UIColor *)color { UIGraphicsBeginImageContextWithOptions(baseImage.size, NO, baseImage.scale); 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); [color set]; CGContextFillRect(ctx, area); CGContextRestoreGState(ctx); CGContextSetBlendMode(ctx, kCGBlendModeOverlay); CGContextDrawImage(ctx, area, baseImage.CGImage); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // If the original image was stretchable, make the new image stretchable if (baseImage.leftCapWidth || baseImage.topCapHeight) { newImage = [newImage stretchableImageWithLeftCapWidth:baseImage.leftCapWidth topCapHeight:baseImage.topCapHeight]; } return newImage; } 
+1
source

I am contacting you with a SO question, which seems to be what you are looking for, and it got an answer.

How to set up transparent PNG image on iPhone?

Good luck

Just a small detail in the title question ... you mean "How" and not "Hot" right?

0
source

I modify some of the things from the skates:

 +(UIImage *)getTintedImage:(UIImage*)image withColor:(UIColor *)tintColor { UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(context, 0, image.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); // draw alpha-mask CGContextSetBlendMode(context, kCGBlendModeNormal); CGContextDrawImage(context, rect, image.CGImage); // draw tint color, preserving alpha values of original image CGContextSetBlendMode(context, kCGBlendModeSourceIn); [tintColor setFill]; CGContextFillRect(context, rect); UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return coloredImage; } 
0
source

All Articles