xcode 如何更改 UITextField clearButton 的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9914925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change the color of a UITextField clearButton?
提问by sergiocg90
I would like to change the color of the clearButton that appaers when you are writting on a UItextField. Any suggestion? I am not an advanced interface developer and I have no idea of how to do it.
我想更改在 UItextField 上书写时出现的 clearButton 的颜色。有什么建议吗?我不是高级界面开发人员,也不知道该怎么做。
回答by Giorgio Marziani de Paolis
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.
您应该创建自己的 UIButton(使用图像,您需要一个想要按钮的 png),实例化它并将此按钮放在 UITextField 的 rightView 中。
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.
然后,您将需要实现方法 -(void) doClear(id)sender; 这将清除您的文本字段。
回答by Guillaume Ramey
Swift 4
斯威夫特 4
I used Giorgio's answer and this is what I went with :
我使用了 Giorgio 的回答,这就是我所用的:
let height = textField.bounds.height / 3
let width = height + 10
let rect = CGRect(x: 0, y: 0, width: width, height: height)
let myButton = UIButton(frame: rect)
myButton.setImage(UIImage(named: "clear button white"), for: .normal)
textField.rightView = myButton
textField.rightViewMode = .always
myButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
The normal one : enter image description here
正常一: 在此处输入图像描述
This one : enter image description here
这一个: 在此处输入图像描述
回答by Coody Chou
I will recommend use custom image and ignore to change native UITextField's clearButton because Apple may change subviews and your app will crash when apple update iOS version.
我会建议使用自定义图像并忽略更改原生 UITextField 的 clearButton,因为 Apple 可能会更改子视图,并且当 Apple 更新 iOS 版本时,您的应用程序将崩溃。
I wrote a category can add Image or custom button easily.
我写了一个类别可以轻松添加图像或自定义按钮。
UITextField+ClearButton.h
UITextField+ClearButton.h
#import <UIKit/UIKit.h>
@interface UITextField (ClearButton)
/**
* @brief Initialize Custon clear button
*/
-(void)initialCustomClearButton;
-(void)setRightMarginDistance:(CGFloat )tempRightMargin;
// you can only use this methods
-(void)setClearButtonImage:(UIImage *)tempClearButtonImage;
-(void)setClearButton:(UIButton *)tempClearButton;
#pragma mark -
-(void)editTextField:(id)sender;
@end
UITextField+ClearButton.m
UITextField+ClearButton.m
#import "UITextField+ClearButton.h"
#include <objc/runtime.h>
#pragma makr - Dynamic Property
////////////////////////////////
#define Dynamic_Property( ObjectType , property , setProperty )\
Dynamic_Property_Getter( ObjectType , property)\
Dynamic_Property_Setter( ObjectType , property , setProperty , OBJC_ASSOCIATION_RETAIN_NONATOMIC )
//////// Getter ////////
#define Dynamic_Property_Getter( ObjectType , property )\
-(ObjectType)property\
{\
return objc_getAssociatedObject( self, @selector(property) );\
}
//////// Setter ////////
#define Dynamic_Property_Setter( ObjectType , property , setProperty , associationFlag) \
-(void)setProperty(ObjectType)property\
{\
objc_setAssociatedObject(self, @selector(property), property, associationFlag);\
}
////////////////////////////////
static CGFloat const kXSGTextFieldClearButtonRightMargin = 6.0f;
@implementation UITextField (ClearButton)
Dynamic_Property(UIButton*,clearBtn,setClearBtn:)
Dynamic_Property(UIImage*, clearButtonImg, setClearButtonImg:)
Dynamic_Property(UIImageView*, clearButtonImageView, setClearButtonImageView:)
Dynamic_Property(NSNumber*, isShow, setIsShow:)
Dynamic_Property(NSNumber*, customClearButtonMode, setCustomClearButtonMode:)
Dynamic_Property(NSNumber*, rightMargin , setRightMargin:)
-(void)initialCustomClearButton{
self.isShow = @NO;// 目前沒用到
self.clearButtonImageView = nil;
// 設定 Clear Button Mode
self.customClearButtonMode = @(self.clearButtonMode);
self.rightMargin = @(kXSGTextFieldClearButtonRightMargin);
self.clearButtonMode = UITextFieldViewModeNever;
[self resetClearButton];
[self addTarget:self action:@selector(editTextField:) forControlEvents:(UIControlEventEditingChanged)];
}
-(void)setRightMarginDistance:(CGFloat )tempRightMargin{
if( tempRightMargin < 0 ){
NSLog(@"Can't set right margin negative number !!");
}
else{
self.rightMargin = @(tempRightMargin);
[self resetClearButton];
}
}
-(void)setClearButtonImage:(UIImage *)tempClearButtonImage{
if( tempClearButtonImage == nil ){
NSLog(@"Can't set Clear Button Image to nil !!");
}
else{
self.clearButtonImg = tempClearButtonImage;
if( self.clearButtonImageView == nil ){
self.clearButtonImageView = [[UIImageView alloc] initWithImage:self.clearButtonImg];
}
else{
[self.clearButtonImageView setImage:self.clearButtonImg];
}
// 調整位置
[self resetImagePosition];
[self.clearBtn addSubview:self.clearButtonImageView];
[self editTextField:self];
}
}
-(void)setClearButton:(UIButton *)tempClearButton{
if( self.clearBtn != nil ){
[self.clearBtn removeTarget:self action:@selector(pressedClearBtn:) forControlEvents:(UIControlEventTouchUpInside)];
}
self.clearBtn = tempClearButton;
[self.clearBtn addSubview:self.clearButtonImageView];
// 設定 frame 調整位置
[self resetClearButton];
[self resetImagePosition];
}
#pragma mark - Private
-(BOOL)checkTextField{
BOOL isOK = YES;
if( self.text == nil || [self.text isEqualToString:@""] ){
isOK = NO;
}
return isOK;
}
-(void)resetClearButton{
if( self.clearBtn == nil ){
self.clearBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - self.frame.size.height - [self.rightMargin floatValue],
0,
self.frame.size.height,
self.frame.size.height)];
}
else{
[self.clearBtn setFrame:CGRectMake(self.frame.size.width - self.clearBtn.frame.size.width - [self.rightMargin floatValue],
0,
self.clearBtn.frame.size.width,
self.clearBtn.frame.size.height)];
[self.clearBtn removeTarget:self action:@selector(pressedClearBtn:) forControlEvents:(UIControlEventTouchUpInside)];
}
[self.clearBtn addTarget:self action:@selector(pressedClearBtn:) forControlEvents:(UIControlEventTouchUpInside)];
self.rightView = self.clearBtn;
self.rightViewMode = UITextFieldViewModeAlways;
[self editTextField:self];
}
-(void)resetImagePosition{
// 調整位置
[self.clearButtonImageView setFrame:CGRectMake((self.clearBtn.frame.size.width - self.clearButtonImg.size.width)*0.5,
(self.clearBtn.frame.size.height - self.clearButtonImg.size.height)*0.5,
self.clearButtonImg.size.width,
self.clearButtonImg.size.height)];
}
-(void)pressedClearBtn:(id)sender{
self.text = @"";
// 隱藏圖片、 disable button
[self editTextField:self];
}
#pragma mark - 加入監聽
-(void)editTextField:(id)sender{
if( sender == self ){
if( [self checkTextField] ){
// 顯示圖片、 enable button
[self.clearBtn setEnabled:YES];
[self.clearBtn setHidden:NO];
[self.clearButtonImageView setHidden:NO];
self.isShow = @YES;
}
else{
// 隱藏圖片、 disable button
[self.clearBtn setEnabled:NO];
[self.clearBtn setHidden:YES];
[self.clearButtonImageView setHidden:YES];
self.isShow = @NO;
}
}
}
@end
How to use
如何使用
UITextField *_textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320 , 40)];
[_textField initialCustomClearButton];
// [UIImage imageNamed:@"clear"] is an image of your clear button.
[_textField setClearButtonImage:[UIImage imageNamed:@"clear"]];
and Done!
并做了!
PS: detail is here: UITextField+ClearButton.hand UITextField+ClearButton.m
PS:详细信息在这里:UITextField+ClearButton.h和UITextField+ClearButton.m