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.
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
Firebase Create User with Email, Password, Display Name and Photo Url
Tab Bar Background Is Missing on iOS 7.1 After Presenting and Dismissing a View Controller
Detecting If User Has in Call Status Bar
Uiwindow with Wrong Size When Using Landscape Orientation
Swift How to Change Uialertcontroller's Title Color
How to Wait for a Function to End on iOS/Swift, Before Starting the Second One
Uitableview Invoke Swipe Actions Programmatically
What Is Correct Format of Bundle Identifier in iOS
How to Test for the Class of a Variable in Swift
How to Increase Width of Textfield According to Typed Text
Why Are Animations on Bounds of an Uilabel Only Working When Increasing the Size
Uilabel: Background Dependent Color
Check If an Auto-Renewable Subscription Is Still Valid
Restkit and Saving to Coredata as Nsmanagedobject
How to Change Space Between Cells in Uicollectionview
Change the Color of a Link in an Nsmutableattributedstring
Passing Data Between 2 Uiviewcontroller Using Delegate and Protocol