OC之UITextField

SONG / 文 发表于2018-01-26 10:43 次阅读 textfiled

一、控件初始化

   UITextField *textField = [[UITextField alloc]init];
    textField.frame  =CGRectMake(100, 100, 300, 40);

    //设置背景颜色->使用了背景图片颜色会被屏蔽
    textField.backgroundColor  = [UIColor whiteColor];
    //设置背景
   textField.background =  [UIImage imageNamed: @"pic.png"];
    //设置水印提示
    textField.placeholder =@"今天没吃早饭";
    //设置输入文字颜色
    textField.textColor = [UIColor redColor];
    //设置输入文字字体和大小
    textField.font = [UIFont fontWithName:@"Arial" size:18];
    textField.text = @"123213";

二、设置清除按钮

UITextFieldViewModeNever, 重不出现

UITextFieldViewModeWhileEditing, 编辑时出现

UITextFieldViewModeUnlessEditing, 除了编辑外都出现

UITextFieldViewModeAlways  一直出现

//设置输入前是否清空控件 textField.clearsOnBeginEditing = YES;

 textField.clearButtonMode = UITextFieldViewModeWhileEditing;
  //设置输入前是否清空控件
    textField.clearsOnBeginEditing  = YES;

三、设置边框样式

    UITextBorderStyleNone,

    UITextBorderStyleLine,

    UITextBorderStyleBezel,

    UITextBorderStyleRoundedRect
textField.borderStyle = UITextBorderStyleBezel;

四、设置自动纠错

typedef enum {

UITextAutocorrectionTypeDefault, 默认

UITextAutocorrectionTypeNo,  不自动纠错

UITextAutocorrectionTypeYes, 自动纠错

} UITextAutocorrectionType;

    textField.autocorrectionType = UITextAutocorrectionTypeNo;

五、设置内容对齐方式 typedef NS_ENUM(NSInteger,UIControlContentVerticalAlignment) {

    UIControlContentVerticalAlignmentCenter  = 0,
    UIControlContentVerticalAlignmentTop     = 1,
    UIControlContentVerticalAlignmentBottom  = 2,
    UIControlContentVerticalAlignmentFill    = 3,
};
typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {
    UIControlContentHorizontalAlignmentCenter = 0,
    UIControlContentHorizontalAlignmentLeft   = 1,
    UIControlContentHorizontalAlignmentRight  = 2,
    UIControlContentHorizontalAlignmentFill   = 3,
    UIControlContentHorizontalAlignmentLeading  API_AVAILABLE(ios(11.0), tvos(11.0)) = 4,
    UIControlContentHorizontalAlignmentTrailing API_AVAILABLE(ios(11.0), tvos(11.0)) = 5,
};
    textField.contentHorizontalAlignment  = UIControlContentHorizontalAlignmentLeft;
    //这种横向6.0已经过时
    textField.textAlignment = UITextAlignmentLeft;

六、设置首字母大写 typedef enum {

UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写

} UITextAutocapitalizationType;

//设置首字母是否大写
    textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

七、设置保护和自动缩放

//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
    textField.adjustsFontSizeToFitWidth = YES;
//使输入文字隐藏,用于密码等保密性输入
    textField.secureTextEntry = YES;

八、设置键盘return typedef enum {

UIReturnKeyDefault, 默认 灰色按钮,标有Return
UIReturnKeyGo,     标有Go的蓝色按钮
UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin,标有Join的蓝色按钮
UIReturnKeyNext,标有Next的蓝色按钮
UIReturnKeyRoute,标有Route的蓝色按钮
UIReturnKeySearch,标有Search的蓝色按钮
UIReturnKeySend,标有Send的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮

} UIReturnKeyType;

textField.returnKeyType =UIReturnKeyDone;

九、设置键盘外观

typedef enum {

UIKeyboardAppearanceDefault, 默认外观,浅灰色

UIKeyboardAppearanceAlert,   深灰 石墨色

} UIReturnKeyType;

textField.keyboardAppearance  = UIKeyboardAppearanceLight;

十、图片最右/左侧加图片

 UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
    textField.rightView=image;
    textField.rightViewMode = UITextFieldViewModeAlways;

十一、限制只能输入特定的字符

(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSCharacterSet *cs;
//invertedSet方法是去反字符,把所有的除了NUMBERS 里的字符都找出来(包含去空格功能)

    cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];

// componentsJoinedByString 用于根据一个字符串来将数组连接成一个新的字符串。

    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串

    BOOL canChange = [string isEqualToString:filtered];

    return canChange;
//这样写了以后,输入非数字时不能输入

}

十二、设置键盘类型

typedef enum {

UIKeyboardTypeDefault,      默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad,             数字键盘
UIKeyboardTypePhonePad,   电话键盘
UIKeyboardTypeNamePhonePad,  电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress,  用于输入电子 邮件地址的键盘    
UIKeyboardTypeDecimalPad,    数字键盘 有数字和小数点
UIKeyboardTypeTwitter,       优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, 

} UIKeyboardType;

纯属自己的学习资料,有错请指出。

收藏 赞 (0) 踩 (0)