• iOS开发系列之三


    // 初始化输入框并设置位置和大小
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 30)];
    // 设置输入框提示
    textField.placeholder = @"TextField Tip";
    // 输入框中预先输入的文字
    textField.text = @"预先输入的文字";
    // 设置输入框文本的字体
    textField.font = [UIFont fontWithName:@"Arial" size:20.0f];
    // 设置输入框字体颜色
    textField.textColor = [UIColor redColor];
    // 设置输入框的背景颜色
    textField.backgroundColor = [UIColor grayColor];
    // 设置输入框边框样式
    textField.borderStyle = UITextBorderStyleRoundedRect;
    
    // 边框样式有下面几种:
    //    enum {
    //        UITextBorderStyleNone,        无边框。默认
    //        UITextBorderStyleLine,        有线型边框
    //        UITextBorderStyleBezel,       有线型边框和阴影
    //        UITextBorderStyleRoundedRect  有圆角边框
    //    } UITextBorderStyle;
    
    // 设置输入框是否用于password
    textField.secureTextEntry = NO;
    // 设置是否有清除button。在什么时候显示。用于一次性删除输入框中的全部内容
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    // 清除button样式有下面几种:
    //    enum {
    //        UITextFieldViewModeNever,          从不出现
    //        UITextFieldViewModeWhileEditing,   编辑时出现
    //        UITextFieldViewModeUnlessEditing,  除了编辑外都出现
    //        UITextFieldViewModeAlways          一直出现
    //    } UITextFieldViewMode;
    
    // 设置自己主动纠错方式
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    
    // 自己主动纠错方式有下面几种:
    //    enum {
    //        UITextAutocorrectionTypeDefault,  默认
    //        UITextAutocorrectionTypeNo,       不自己主动纠错
    //        UITextAutocorrectionTypeYes,      自己主动纠错
    //    } UITextAutocorrectionType;
    
    // 设置自己主动大写方式
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    // 自己主动大写方式有下面几种:
    //    enum {
    //        UITextAutocapitalizationTypeNone,           不自己主动大写
    //        UITextAutocapitalizationTypeWords,          单词首字母大写
    //        UITextAutocapitalizationTypeSentences,      句子的首字母大写
    //        UITextAutocapitalizationTypeAllCharacters,  全部字母都大写
    //    } UITextAutocapitalizationType;
    
    // 设置再次编辑是否清空
    textField.clearsOnBeginEditing = YES;
    // 设置文本对齐方式
    textField.textAlignment = NSTextAlignmentLeft;
    
    // iOS7中文本对齐方式有下面几种:
    //    enum {
    //        NSTextAlignmentLeft      = 0,  左对齐。默认
    //        NSTextAlignmentCenter    = 1,  居中对齐
    //        NSTextAlignmentRight     = 2,  右对齐
    //        NSTextAlignmentJustified = 3,  在一个段落的最后一行自然对齐
    //        NSTextAlignmentNatural   = 4,  默认对齐方式
    //    } NSTextAlignment;
    
    // 设置字体大小是否自己主动适应输入框宽度。默认是保持原来大小。长文本滚动
    textField.adjustsFontSizeToFitWidth = YES;
    // 设置自己主动缩小显示的最小字体大小
    textField.minimumFontSize = 20;
    // 设置键盘的样式
    textField.keyboardType = UIKeyboardTypeNumberPad;
    
    // 键盘样式有下面几种:
    //    enum {
    //        UIKeyboardTypeDefault,                默认键盘。支持全部字符
    //        UIKeyboardTypeASCIICapable,           支持ASCII的默认键盘
    //        UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符
    //        UIKeyboardTypeURL,                    仅仅支持URL字符的URL键盘,支持.combutton
    //        UIKeyboardTypeNumberPad,              数字键盘
    //        UIKeyboardTypePhonePad,               电话键盘
    //        UIKeyboardTypeNamePhonePad,           支持输入人名的电话键盘
    //        UIKeyboardTypeEmailAddress,           电子邮件键盘
    //        UIKeyboardTypeDecimalPad,             有数字和小数点的数字键盘
    //        UIKeyboardTypeTwitter,                优化的键盘。方便输入@、#字符
    //        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
    //    } UIKeyboardType;
    
    // 设置return键样式
    textField.returnKeyType = UIReturnKeyDone;
    
    // return键有下面几种样式:
    //    enum {
    //        UIReturnKeyDefault,        默认,灰色button。标有Return
    //        UIReturnKeyGo,             标有Go的蓝色button
    //        UIReturnKeyGoogle,         标有Google的蓝色button,用于搜索
    //        UIReturnKeyJoin,           标有Join的蓝色button
    //        UIReturnKeyNext,           标有Next的蓝色button
    //        UIReturnKeyRoute,          标有Route的蓝色button
    //        UIReturnKeySearch,         标有Search的蓝色button
    //        UIReturnKeySend,           标有Send的蓝色button
    //        UIReturnKeyYahoo,          标有Yahoo的蓝色button
    //        UIReturnKeyYahoo,          标有Yahoo的蓝色button
    //        UIReturnKeyEmergencyCall,  紧急呼叫button
    //    } UIReturnKeyType;
     
    // 设置键盘外观
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
    
    // 键盘外观有一下两种:
    //    enum {
    //        UIKeyboardAppearanceDefault, 默认外观,浅灰色
    //        UIKeyboardAppearanceAlert。   深灰,石墨色
    //    } UIReturnKeyType;
    
    // 设置代理,用于实现协议
    textField.delegate = self;
     
    // 最右側加图片是下面代码,左側相似
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
    textField.rightView = image;
    textField.rightViewMode = UITextFieldViewModeAlways;
    
    // 把输入框加到视图中
    [self.view addSubview:textField];
    
    // 按return键收起键盘
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [text resignFirstResponder];
        return YES;
    }

    本文固定链接:http://www.itechzero.com/ios-development-series-three-uitextfield-usage-summary.html。转载请注明出处。

  • 相关阅读:
    【剑指offer】19 顺时针打印矩阵
    【剑指offer】18 二叉树的镜像
    【剑指offer】17 树的子结构
    【剑指offer】16 合并两个排序的链表
    【剑指offer】15 反转链表
    【剑指offer】14 链表中倒数第k个结点
    【剑指offer】13 调整数组顺序使奇数位于偶数前面
    【剑指offer】12 数值的整数次方
    【剑指offer】11 二进制中1的个数
    数据库-第九节:ORM模型迁移
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5072861.html
Copyright © 2020-2023  润新知