UITextField

1 键盘风格
textField.keyboardtype = UIKeyboardTypeNumberPad;
typedef 
enum {  
 UIKeyboardTypeDefault,                
// 默认键盘:支持所有字符 
 UIKeyboardTypeASCIICapable,           
// 支持ASCII的默认键盘 
UIKeyboardTypeNumbersAndPunctuation,  
// 标准电话键盘,支持+*#等符号 
 UIKeyboardTypeURL,                    
// URL键盘,有.com按钮;只支持URL字符 
UIKeyboardTypeNumberPad,              
//数字键盘 
UIKeyboardTypePhonePad,               
// 电话键盘 
UIKeyboardTypeNamePhonePad,           
// 电话键盘,也支持输入人名字 
 UIKeyboardTypeEmailAddress,           
// 用于输入电子邮件地址的键盘 

} UIKeyboardType; 

2 键盘外观
textView.keyboardAppearance=UIKeyboardAppearanceDefault; 

typedef 
enum
 {  
  UIKeyboardAppearanceDefault,    
// 默认外观:浅灰色 
  UIKeyboardAppearanceAlert,      
//深灰/石墨色 
} UIKeyboardAppearance;

3 回车键
textView.returnKeyType=UIReturnKeyGo;

typedef NS_ENUM(NSInteger, UIReturnKeyType) {
    UIReturnKeyDefault,  // 默认 灰色按钮,标有Return
    UIReturnKeyGo,     // 标有Go的蓝色按钮
    UIReturnKeyGoogle, // 标有Google的蓝色按钮,用语搜索
    UIReturnKeyJoin, // 标有Join的蓝色按钮
    UIReturnKeyNext, // 标有Next的蓝色按钮
    UIReturnKeyRoute, // 标有Route的蓝色按钮
    UIReturnKeySearch, // 标有Search的蓝色按钮
    UIReturnKeySend, // 标有Send的蓝色按钮
    UIReturnKeyYahoo, // 标有Yahoo的蓝色按钮
    UIReturnKeyYahoo, // 标有Yahoo的蓝色按钮
    UIReturnKeyEmergencyCall,  // 紧急呼叫按钮
};
4 自动大写

//首字母是否大写
  text.autocapitalizationType = UITextAutocapitalizationTypeNone;
 
typedef enum {
    UITextAutocapitalizationTypeNone,  // 不自动大写
    UITextAutocapitalizationTypeWords,  // 单词首字母大写
    UITextAutocapitalizationTypeSentences,  //  句子的首字母大写
    UITextAutocapitalizationTypeAllCharacters,  // 所有字母都大写
} UITextAutocapitalizationType;



5 自动更正

textfield.autocorrectionType = UITextAutocorrectionTypeYes; // 自动更正

6 安全输入文本  一般是用于密码的输入

textfield.secureTextEntry=YES;

7 设置边框样式,只有设置了才会显示边框样式 
  text.borderStyle = UITextBorderStyleRoundedRect;
 typedef enum {
    UITextBorderStyleNone, 
    UITextBorderStyleLine,
    UITextBorderStyleBezel,
    UITextBorderStyleRoundedRect  
  } UITextBorderStyle;

8  输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容
  text.clearButtonMode = UITextFieldViewModeAlways;
 
typedef enum {
    UITextFieldViewModeNever, // 从不出现
    UITextFieldViewModeWhileEditing,  // 编辑时出现
    UITextFieldViewModeUnlessEditing, // 除了编辑外都出现
    UITextFieldViewModeAlways  // 一直出现
} UITextFieldViewMode;
PS:这个也可以用来显示 textfield的leftView和rightView的显示方式。
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
    text.rightView=image;
    text.rightViewMode = UITextFieldViewModeAlways; 

并且清除按钮跟再次编辑就清空有时候是可以替换的  
 text.clearsOnBeginEditing = YES; 
9 是否纠错
  text.autocorrectionType = UITextAutocorrectionTypeNo;
 
typedef enum {
    UITextAutocorrectionTypeDefault, // 默认
    UITextAutocorrectionTypeNo,  // 不自动纠错
    UITextAutocorrectionTypeYes, // 自动纠错
} UITextAutocorrectionType;
 
10  // === 判断文字 尤其是中文
方法1 在优先进行的方法里面加入观察者
 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textChanged:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:_searchTF];
解释一下[NSNotificationCenter defaultCenter]addObserver的参数:
observer
     用来处理消息的对象
selector
     选择器,也就是我们自己编写的方法名称,这个方法必须有且仅有一个NSNotification类型的参数
name
     通知名称。
object
     发送通知的对象,也就是我们要处理的UITextField

其次实现观察监控的函数 监控textfield的变化
-(void)textChanged:(NSNotification *)notification
{
            if ([str rangeOfString:_searchTF.text].location != NSNotFound) {
               // code
         
}

方法2 利用textfield的delegate的方法  数字和英文字母的判断
=================
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSMutableString *strReplace;
    // --- 根据编码
    if (textField == self.firstCode.customerTF) {
        if (strReplace != nil) {
            strReplace = nil;
        }
        strReplace = [NSMutableString stringWithString:[self.firstCode.customerTF.text stringByAppendingString:string]];
        if ([strReplace isEqualToString:textField.text]) {
            NSRange rang = NSMakeRange(strReplace.length-1, 1);
            [strReplace replaceCharactersInRange:rang withString:@""];
        }
        
// strReplace 即为得到的变化的内容,可以进行其他的操作
        
    }
}
方法3  实时监控输入的汉字的方法

++++++++++ 实时的判断中文的方法 ++++++++++++++
- (void)textFieldDidChange:(NSNotification *)note
{
    //可以用note.object来获取产生该消息的UITextField
    if (_activeTextField.text.length > 0) {
        int utfCode = 0;
        void *buffer = &utfCode;
        NSRange range = NSMakeRange(_activeTextField.text.length - 1, 1);
        NSString *word = [_activeTextField.text substringWithRange:range];
        BOOL b = [word getBytes:buffer maxLength:2 usedLength:NULL encoding:NSUTF16LittleEndianStringEncoding options:NSStringEncodingConversionExternalRepresentation range:range remainingRange:NULL];
        if (b && (utfCode >= 0x4e00 && utfCode <= 0x9fa5)) {
            NSLog(@"it is chinese,%@", word);
        }
    }   
}
++++++++++++++++++++++++++++++++
在代码中,检查当前输入的字符是否为汉字。
getBytes是NSString的实例方法,参数如下:
buffer
    获取字符的字节数据
maxLength
    buffer的最大长度
usedLength
    实际写入的长度,不需要的话可以传递NULL
encoding
    字符编码常数,不同编码方式转换后的字节长是不一样的,这里我用了UTF16 Little-Endian,maxLength为2字节,如果使用Unicode,则需要4字节。
options
    编码转换的选项,有两个值,分别是NSStringEncodingConversionAllowLossy和NSStringEncodingConversionExternalRepresentation
range
    获取的字符串中的字符范围,这里我设置的是字符串的最后一个字符。
remainingRange
    建议获取的范围,可以传递NULL。
返回值
    成功返回YES,否则返回NO
unicode中文编码范围是0x4e00~0x9fa5,注意encoding参数不同的话,获得的字节数据是有区别的,详情可参考unicode编码知识。
================
11  一些关于视图上的设定
//内容的垂直对齐方式  UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment
  text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
 
//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动 
  textFied.adjustsFontSizeToFitWidth = YES;
 
//设置自动缩小显示的最小字体大小
  text.minimumFontSize = 20;
原文地址:https://www.cnblogs.com/isItOk/p/4720232.html