Skip to content

Instantly share code, notes, and snippets.

@jonathan-beebe
Last active March 14, 2018 17:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathan-beebe/a9f2cd3349685ab09757 to your computer and use it in GitHub Desktop.
Save jonathan-beebe/a9f2cd3349685ab09757 to your computer and use it in GitHub Desktop.
Tinting a UITextField’s clear button
#import "UITextField+TintClearButton.h"
@implementation MyCustomTextFieldClass
- (void)layoutSubviews
{
[super layoutSubviews];
[self tintClearImage];
}
@end
// Based of the solution posted here http://stackoverflow.com/a/30532855/123781
@import UIKit;
/**
* Apple does not allow the text field clear button image to be tinted. This hack picks out the clear
* button and tints it to be the same as the recievers `tintColor` property.
*/
@interface UITextField (TintClearButton)
/**
* Tell the reciever that we want the clear button to be tinted.
*/
- (void)tintClearImage;
@end
#import "UITextField+TintClearButton.h"
@import ObjectiveC.runtime;
UIImage* UITFC_tintImage(UIImage* image, UIColor* color)
{
CGSize size = image.size;
UIGraphicsBeginImageContextWithOptions(size, false, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:1.0];
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
CGContextSetAlpha(context, 1.0);
CGRect rect = CGRectMake(
CGPointZero.x,
CGPointZero.y,
image.size.width,
image.size.height);
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
UIImage* tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
@implementation UITextField (TintClearButton)
- (void)tintClearImage
{
for (UIView* view in self.subviews) {
if (![view isKindOfClass:[UIButton class]])
continue;
UIButton* button = (UIButton*)view;
[button setImage:[self UITFC_normalClearImageForButton:button] forState:UIControlStateNormal];
[button setImage:[self UITFC_highlightClearImageForButton:button] forState:UIControlStateHighlighted];
}
}
- (UIImage*)UITFC_normalClearImageForButton:(UIButton*)button
{
UIImage* image = [button imageForState:UIControlStateNormal];
return [self UITFC_getTintImage:image key:@"UITFC_tinted_clear_image_normal"];
}
- (UIImage*)UITFC_highlightClearImageForButton:(UIButton*)button
{
UIImage* image = [button imageForState:UIControlStateHighlighted];
return [self UITFC_getTintImage:image key:@"UITFC_tinted_clear_image_highlight"];
}
- (UIImage*)UITFC_getTintImage:(UIImage*)image key:(NSString*)key
{
UIImage* tintedImage = objc_getAssociatedObject(self, (__bridge const void*)(key));
if (tintedImage == nil) {
tintedImage = UITFC_tintImage(image, self.tintColor);
objc_setAssociatedObject(self, (__bridge const void*)(key), tintedImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return tintedImage;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment