修改光標顏色、placeholder 顏色,手機號輸入格式化
-
修改光標顏色
//修改textField光標顏色碱妆, [textField setTintColor:[UIColor redColor]]; //設置UITextField的光標顏色 // [[UITextField appearance] setTintColor:[UIColor redColor];
-
修改placeholder 顏色
通過設置UITextField的attributedPlaceholder屬性唇牧,修改它的顏色 。因為經常用到咨跌,創(chuàng)建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的內容不變。
實現(xiàn)代碼(ps:一下代碼均實現(xiàn)在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]; }