修改光标颜色、placeholder 颜色,手机号输入格式化
-
修改光标颜色
//修改textField光标颜色, [textField setTintColor:[UIColor redColor]]; //设置UITextField的光标颜色 // [[UITextField appearance] setTintColor:[UIColor redColor];
-
修改placeholder 颜色
通过设置UITextField的attributedPlaceholder属性,修改它的颜色 。因为经常用到,创建UITextField 的分类,将其封装。
/** 设置textfield的placeholder及其颜色 */
-(void)dh_setPlaceholder:(NSString *)placeholder andPlaceholderColor:(UIColor *)placeholderColor {
NSDictionary *attributes = @{
NSForegroundColorAttributeName:placeholderColor,
NSFontAttributeName:self.font
};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:placeholder attributes:attributes];
self.attributedPlaceholder = attrString;
}
-
手机号输入格式化
在用户输入电话号码时,自动将其格式化为123 4567 8910。
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
把textField中位置为range的字符串替换为string字符串;此函数在textField内容被修改时调用;使用这个方法来验证使用时用户输入的类型。
@param textField textField
@param range UITextField控件中光标选中的字符串,即被替换的字符串;range.length为0时,表示在位置range.location插入string。
@param string 替换字符串;string.length为0时,表示删除。
@return YES,表示修改生效;NO,表示不做修改,textField的内容不变。
实现代码(ps:一下代码均实现在UITextField的分类中):
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { //在输入或删除前的光标位置 NSRange currentRange = [self cursorPosition]; //输入或删除前的内容 NSString *previousContent = textField.text; // 限制只能输入数字 NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"]; string = [string stringByReplacingOccurrencesOfString:@" " withString:@""]; if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) { return NO; } //把textField中位置为range的字符串替换为string字符串 NSString *handleText = [previousContent stringByReplacingCharactersInRange:range withString:string]; //纯数字的字符串 NSString *onlyDigitStr = [handleText stringByReplacingOccurrencesOfString:@" " withString:@""]; NSMutableString *temMstr = [NSMutableString new]; for (NSInteger i = 0; i < onlyDigitStr.length; i++) { if (i == 3 || i == 7) { [temMstr appendString:@" "]; } unichar digitchar = [onlyDigitStr characterAtIndex:i]; [temMstr appendString:[NSString stringWithCharacters:&digitchar length:1]]; } if (temMstr.length >= 14) { return NO; } [textField setText:temMstr]; /* //不做任何处理时,显示的内容 NSString *text = [previousContent stringByReplacingCharactersInRange:range withString:string]; text = [text stringByReplacingOccurrencesOfString:@" " withString:@""]; NSMutableString *temString = [NSMutableString stringWithString:text]; [temString insertString:@" " atIndex:0]; text = temString; NSString *newString = @""; while (text.length > 0) { NSString *subString = [text substringToIndex:MIN(text.length, 4)]; newString = [newString stringByAppendingString:subString]; if (subString.length == 4) { newString = [newString stringByAppendingString:@" "]; } text = [text substringFromIndex:MIN(text.length, 4)]; } newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]]; if (newString.length >= 14) { return NO; } [textField setText:newString]; */ if (string.length == 0) { //删除 currentRange.location --; if (currentRange.location == 4 || currentRange.location == 9) { currentRange.location --; } } else { currentRange.location += string.length; if (currentRange.location == 4 || currentRange.location == 9) { currentRange.location ++; } } [self setCursorPosition:currentRange]; return NO; } /** 光标位置 */ - (NSRange)cursorPosition { 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)setCursorPosition:(NSRange) range { UITextPosition* beginning = self.beginningOfDocument; UITextPosition* startPosition = [self positionFromPosition:beginning offset:range.location]; UITextPosition* endPosition = [self positionFromPosition:beginning offset:range.location + range.length]; UITextRange* cursorPosition = [self textRangeFromPosition:startPosition toPosition:endPosition]; [self setSelectedTextRange:cursorPosition]; }