Uitextfield Clearbuttonmode Color

UITextField clearButtonMode color

You'll need to create your own clear button image in this case. I would suggest taking a screenshot of the clear button and editing in photoshop.

You can take that image and create a UIButton with the image dimensions. From there you can set it as the UITextField's rightView. Like so:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"clear_button.png"] forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0f, 0.0f, 15.0f, 15.0f)]; // Required for iOS7
theTextField.rightView = button;
theTextField.rightViewMode = UITextFieldViewModeWhileEditing;

I typed that without syntax checking and what not so you'll want to check it out before running it. You'll also want to replace clear_button.png with whatever your image name is.

You'll also need to write your own method to clear the text field.

How to change the tint color of the clear button on a UITextField

Here you go!

A TintTextField.

Using no custom image, or added buttons etc.

Image of UITextField with white tint color for cancel button

class TintTextField: UITextField {

var tintedClearImage: UIImage?

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupTintColor()
}

override init(frame: CGRect) {
super.init(frame: frame)
self.setupTintColor()
}

func setupTintColor() {
self.borderStyle = UITextField.BorderStyle.roundedRect
self.layer.cornerRadius = 8.0
self.layer.masksToBounds = true
self.layer.borderColor = self.tintColor.cgColor
self.layer.borderWidth = 1.5
self.backgroundColor = .clear
self.textColor = self.tintColor
}

override func layoutSubviews() {
super.layoutSubviews()
self.tintClearImage()
}

private func tintClearImage() {
for view in subviews {
if view is UIButton {
let button = view as! UIButton
if let image = button.image(for: .highlighted) {
if self.tintedClearImage == nil {
tintedClearImage = self.tintImage(image: image, color: self.tintColor)
}
button.setImage(self.tintedClearImage, for: .normal)
button.setImage(self.tintedClearImage, for: .highlighted)
}
}
}
}

private func tintImage(image: UIImage, color: UIColor) -> UIImage {
let size = image.size

UIGraphicsBeginImageContextWithOptions(size, false, image.scale)
let context = UIGraphicsGetCurrentContext()
image.draw(at: .zero, blendMode: CGBlendMode.normal, alpha: 1.0)

context?.setFillColor(color.cgColor)
context?.setBlendMode(CGBlendMode.sourceIn)
context?.setAlpha(1.0)

let rect = CGRect(x: CGPoint.zero.x, y: CGPoint.zero.y, width: image.size.width, height: image.size.height)
UIGraphicsGetCurrentContext()?.fill(rect)
let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return tintedImage ?? UIImage()
}
}

How can I change the color of a UITextField clearButton?

You should create your own UIButton (with image, you'll need a png of you desiderated button), instance it and put this button in the rightView of your UITextField.

CGFloat myWidth = 26.0f;
CGFloat myHeight = 30.0f;
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, myWidth, myHeight)];
[myButton setImage:[UIImage imageNamed:@"myButtonImage"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"myButtonImage"] forState:UIControlStateHighlighted];

[myButton addTarget:self action:@selector(doClear:) forControlEvents:UIControlEventTouchUpInside];

myTextField.rightView = myButton;
myTextField.rightViewMode = UITextFieldViewModeUnlessEditing;

You could need to refine alignments using (these are example values):

myButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);

Then, you will need to implement method -(void) doClear(id)sender; that will clear your textfield.

Titanium UiTextField Clear button is not at all visible in Dark Mode for iOS v13

it looks like it is a very light grey (when you set it to a dark backgroundColor you'll see the X again.
But it looks like it is native behavior:
https://developer.apple.com/forums/thread/124973
and you'll need to create a custom textfield image (https://stackoverflow.com/a/10274246/5193915) but that means inside the SDK.

As a quick workaround I would just create an image and place it on top of the TextField and move it to the right corner.

UITextField Custom Clear Button

As mentioned already, in your code the clearClicked method should not override as UITextField doesn't have a clearClicked method to override.

Anyway, I updated the code to work when using it with storyboards. Added the awakeFromNib method which calls the initialisation code.

class CustomTextField: UITextField {

override open func awakeFromNib() {
super.awakeFromNib()
self.initialize()
}

override init(frame: CGRect) {
super.init(frame: frame)
self.initialize()
}

func initialize() {
let clearButton = UIButton(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
clearButton.setImage(UIImage(named: "Glyph/16x16/Clear")!, for: [])

self.rightView = clearButton
clearButton.addTarget(self, action: #selector(clearClicked), for: .touchUpInside)

self.clearButtonMode = .never
self.rightViewMode = .whileEditing
}

@objc func clearClicked(sender:UIButton)
{
self.text = ""
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}

Customize UITextField Clear Button Color to White Color

From all your answers, I come to this conclusion,

UIButton *btnClear = [self.txtEmail valueForKey:@"_clearButton"];
UIImage *imageNormal = [btnClear imageForState:UIControlStateNormal];
UIGraphicsBeginImageContextWithOptions(imageNormal.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();

CGRect rect = (CGRect){ CGPointZero, imageNormal.size };
CGContextSetBlendMode(context, kCGBlendModeNormal);
[imageNormal drawInRect:rect];

CGContextSetBlendMode(context, kCGBlendModeSourceIn);
[[UIColor whiteColor] setFill];
CGContextFillRect(context, rect);

UIImage *imageTinted = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[btnClear setImage:imageTinted forState:UIControlStateNormal];


Related Topics



Leave a reply



Submit