1. 判断输入的是否全为数字
NSScanner* scan = [NSScannerscannerWithString:self.countTextField.text];
int val;
NSString *string = [NSStringstringWithFormat:@"%d",[scanscanInt:&val] && [scan isAtEnd]];
NSLog(@"%d",[scanscanInt:&val] && [scanisAtEnd]);
if ([string isEqualToString:@"0"]) {
// 0代表不全为数字
}else {
// 1代表输入的全为数字
}
2. 修改UITextField中placeholder的字体颜色
(1)
NSMutableDictionary *dict = [NSMutableDictionarydictionary];
dict[NSForegroundColorAttributeName] =Color(201,197,189);
NSAttributedString *attribute = [[NSAttributedStringalloc]initWithString:self.emailText.placeholderattributes:dict];
[self.emailTextsetAttributedPlaceholder:attribute];
(2)有了placeholderLabel.textColor
这个属性,在storyboard/xib中利用KVC设置placeholder颜色,如图
3. textField 不顶格输入(自定义的textField总是顶格输入,是否影响美观呢?是否曾用空格解决过这个问题?)
tel.leftView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,8,0)];
tel.leftViewMode =UITextFieldViewModeAlways;
4. textField 密文输入
password.secureTextEntry =YES;
5. 限制texfield输入位数(例如以下,最多输入4位)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.authorizationPassword) {
if (string.length ==0) returnYES;
NSInteger existedLength = textField.text.length;
NSInteger selectedLength = range.length;
NSInteger replaceLength = string.length;
if (existedLength - selectedLength + replaceLength >4) {
return NO;
}
}
return YES;
}
6.UITextField无法改变高度处理
在IB中直接拉一个TextField到xib中,调整高度
发现高度直接锁定一个数字,灰着,不能修改,解决方法:
(1)ib里面就是锁定的
更改textField的border样式,就可以在ib更改高度
(2)在代码里面更改高度
在viewIdLoad方法加上
CGRect rect = txtNum.frame;
rect.size.height = 50;
txtNum.frame = rect;
7.控制textField是否可被编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([self.canEditingisEqualToString:@"1"]) {
return YES;
}
return NO;
}
8. iOS改变UITextField中光标颜色
// _textField.tintColor = [UIColor redColor]; 改变光标的颜色 同时复制粘贴的颜色也变了 然后我想到了下面的情况
下面如果你想要禁用复制粘贴的功能
如果想禁止复制粘贴功能那么就重写
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(cut:)){
return NO;
}
else if(action == @selector(copy:)){
return YES;
}
else if(action == @selector(paste:)){
return NO;
}
else if(action == @selector(select:)){
return NO;
}
else if(action == @selector(selectAll:)){
return NO;
}
else
{
return [super canPerformAction:action withSender:sender];
}
}
9.设置字符间间距
NSDictionary *attrsDictionary =@{
NSFontAttributeName: self.textField.font,
NSKernAttributeName:[NSNumber numberWithFloat:30.0f]//这里修改字符间距
};
self.textField.attributedText=[[NSAttributedString alloc]initWithString:self.textField.text attributes:attrsDictionary];
10.隐藏键盘上方提示区域
_usernameTextField.inputAccessoryView = [[UIView alloc] init];