1.设置placeholder颜色
[textField setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];
2.设置光标颜色
textField.tintColor= [UIColor greenColor];
3.设置获取光标位置
- (NSRange)selectedRange {
UITextPosition* beginning = self.beginningOfDocument;
UITextRange* selectedRange = self.selectedTextRange;
UITextPosition* selectionStart = selectedRange.start;
UITextPosition* selectionEnd = selectedRange.end;
const NSInteger location = [self offsetFromPosition:beginning toPosition:selectionStart];
const NSInteger length = [self offsetFromPosition:selectionStart toPosition:selectionEnd];
return NSMakeRange(location, length);
}
- (void)setSelectedRange:(NSRange)range {// 备注:UITextField必须为第一响应者才有效
UITextPosition* beginning = self.beginningOfDocument;
UITextPosition* startPosition = [self positionFromPosition:beginning offset:range.location];
UITextPosition* endPosition = [self positionFromPosition:beginning offset:range.location + range.length];
UITextRange* selectionRange = [self textRangeFromPosition:startPosition toPosition:endPosition];
[self setSelectedTextRange:selectionRange];
}
4.禁止输入编辑
//第1种方法
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(paste:))//禁止粘贴
return NO;
if (action == @selector(select:))// 禁止选择
return NO;
if (action == @selector(selectAll:))// 禁止全选
return NO;
return [super canPerformAction:action withSender:sender];
}
//第2种方法
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
5.监听数值变化的三种方法
#pragma mark - 直接添加监听方法
-(void)addTargetMethod{
[self.textField addTarget:self action:@selector(textFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
}
-(void)textFieldTextChange:(UITextField*)textField{
NSLog(@"textField - 输入框内容改变,当前内容为: %@",textField.text);
}
#pragma mark - NSNotificationCenter 添加监听方法
-(void)addNSNotificationCenter{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextNotificationChange:) name:UITextFieldTextDidChangeNotification object:self.textField];
}
-(void)textFieldTextNotificationChange:(NSNotification*)noti{
UITextField*currentTextField = (UITextField*)noti.object;
NSLog(@"textField - 输入框内容改变,当前内容为: %@",currentTextField.text);
}
#pragma mark - kvo 监听方法
-(void)addKVO{
[self.textField addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
self.textField.text=@"123";
}
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context{
if([keyPathisEqualToString:@"text"] && object ==self.textField) {
NSLog(@"textField - 输入框内容改变,当前内容为: %@",self.textField.text);
}else{
[superobserveValueForKeyPath:keyPathofObject:objectchange:changecontext:context];
}
}
6.限制只能输入小数点后一位,并且限制大小
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField == self.textField) {
//如果输入的是"." 判断之前已经有"."或者字符串为空
if ([string isEqualToString:@"."] && ([textField.text rangeOfString:@"."].location != NSNotFound || [textField.text isEqualToString:@""])) {
return NO;
}
//拼出输入完成的str,判断str的长度大于等于"."的位置)
NSMutableString *str = [[NSMutableString alloc] initWithString:textField.text];
[str insertString:string atIndex:range.location];
if (str.length >= [str rangeOfString:@"."].location + 3){
return NO;
}
// 涉及到字符转转小数的一定要 doubleValue floatValue转的值不准确
CGFloat inputValue = str.doubleValue;
if (inputValue > 100) {
return NO;
}
}
return YES;
}